Compare commits
7 Commits
463e9b6258
...
v0.1.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
37d8e8cc74
|
|||
|
f3f83b7ca9
|
|||
|
552f604200
|
|||
|
f01357987c
|
|||
|
b4229c29c4
|
|||
|
d05d93a0d7
|
|||
|
37e125945b
|
20
README.md
Normal file
20
README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# ShAp-Planner
|
||||
|
||||
ShAp-Planner is a **self-hosted app** for managing finances, tasks, and data within shared households.
|
||||
The app is fully open source, lightweight, and can run on small devices like Raspberry Pi or older computers.
|
||||
|
||||
**[Backend](https://git.miaurizius.de/MiauRizius/shap-planner-backend):** Go
|
||||
**[Frontend](https://git.miaurizius.de/MiauRizius/shap-planner-android):** Android (Kotlin)
|
||||
**[License](https://git.miaurizius.de/MiauRizius/shap-planner-android/src/branch/main/LICENSE):** [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/)
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
You can either build the app from source or download the apk _(will be available soon)_
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
This work is marked <a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0 1.0</a>
|
||||
@@ -1,9 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
@@ -12,19 +9,16 @@
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:usesCleartextTraffic="true"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
android:theme="@style/Theme.ShapPlanner">
|
||||
<activity
|
||||
android:name=".activities.MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.ShapPlanner">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -12,13 +12,14 @@ class TokenStorage(context: Context) {
|
||||
|
||||
private val prefs = EncryptedSharedPreferences.create(
|
||||
context,
|
||||
"wg_token_prefs",
|
||||
"wg_token_prefs2",
|
||||
masterKey,
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
||||
)
|
||||
|
||||
fun saveTokens(accountId: String, accessToken: String, refreshToken: String) {
|
||||
// println("Account ID: ${accountId}\nAToken: ${accessToken}\nRToken: ${refreshToken}")
|
||||
prefs.edit()
|
||||
.putString("access_$accountId", accessToken)
|
||||
.putString("refresh_$accountId", refreshToken)
|
||||
|
||||
@@ -4,282 +4,61 @@ import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.miaurizius.shap_planner.TokenStorage
|
||||
import de.miaurizius.shap_planner.UserPreferences
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
import de.miaurizius.shap_planner.network.SessionState
|
||||
import de.miaurizius.shap_planner.room.AppDatabase
|
||||
import de.miaurizius.shap_planner.ui.AppContent
|
||||
import de.miaurizius.shap_planner.ui.theme.ShapPlannerTheme
|
||||
import de.miaurizius.shap_planner.viewmodels.LoginViewModel
|
||||
import de.miaurizius.shap_planner.viewmodels.MainViewModel
|
||||
import java.util.UUID
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// enableEdgeToEdge()
|
||||
|
||||
val prefs = UserPreferences(this)
|
||||
val loginViewModel = LoginViewModel(prefs, applicationContext)
|
||||
|
||||
val database = AppDatabase.getDatabase(applicationContext)
|
||||
val dao = database.accountDao()
|
||||
|
||||
val tokenStorage = TokenStorage(applicationContext)
|
||||
|
||||
val mainViewModel = MainViewModel(dao, tokenStorage)
|
||||
|
||||
|
||||
setContent {
|
||||
ShapPlannerTheme {
|
||||
val isLoggedIn by loginViewModel.isLoggedIn.collectAsState()
|
||||
val accountList by mainViewModel.accounts.collectAsState()
|
||||
val selectedAccount = mainViewModel.selectedAccount
|
||||
var showLoginForNewAccount by remember { mutableStateOf(false) }
|
||||
val showLoginForNewAccount = remember { mutableStateOf(false) }
|
||||
val expenses by mainViewModel.expenses.collectAsState()
|
||||
|
||||
when {
|
||||
BackHandler(enabled = showLoginForNewAccount.value && accountList.isNotEmpty()) {
|
||||
showLoginForNewAccount.value = false
|
||||
}
|
||||
|
||||
showLoginForNewAccount -> {
|
||||
LoginScreen(
|
||||
onLogin = { serverUrl, username, password ->
|
||||
loginViewModel.login(serverUrl, username, password, mainViewModel)
|
||||
showLoginForNewAccount = false
|
||||
AppContent(
|
||||
accountList = accountList,
|
||||
selectedAccount = selectedAccount,
|
||||
showLoginForNewAccount = showLoginForNewAccount.value,
|
||||
onLogin = { server, user, pass ->
|
||||
loginViewModel.login(server, user, pass, mainViewModel)
|
||||
showLoginForNewAccount.value = false
|
||||
},
|
||||
onBack = {
|
||||
showLoginForNewAccount = false
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
accountList.isEmpty() -> {
|
||||
LoginScreen(
|
||||
onLogin = { serverUrl, username, password ->
|
||||
loginViewModel.login(serverUrl, username, password, mainViewModel)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
selectedAccount != null -> {
|
||||
DashboardScreen(
|
||||
account = selectedAccount,
|
||||
onBack = { mainViewModel.logoutFromAccount() },
|
||||
onDelete = { mainViewModel.deleteAccount(selectedAccount) },
|
||||
onSelectAccount = { mainViewModel.selectAccount(it) },
|
||||
onLogoutAccount = { mainViewModel.logoutFromAccount() },
|
||||
onAddAccountClick = { showLoginForNewAccount.value = true },
|
||||
onDeleteAccount = { mainViewModel.deleteAccount(selectedAccount!!) },
|
||||
sessionState = mainViewModel.sessionState,
|
||||
onValidate = { mainViewModel.validateSession(selectedAccount) },
|
||||
onSessionInvalid = { mainViewModel.logoutFromAccount() }
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
AccountSelectionScreen(
|
||||
accounts = accountList,
|
||||
onAccountClick = { account ->
|
||||
mainViewModel.selectAccount(account)
|
||||
},
|
||||
onAddAccountClick = {
|
||||
showLoginForNewAccount = true
|
||||
}
|
||||
onValidateSession = { mainViewModel.validateSession(selectedAccount!!) },
|
||||
onSessionInvalid = { mainViewModel.logoutFromAccount() },
|
||||
expenses = expenses,
|
||||
onExpenseClick = { expense -> println("Clicked: ${expense.title}") },
|
||||
viewModel = mainViewModel
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AccountSelectionScreen(accounts: List<Account>, onAccountClick: (Account) -> Unit, onAddAccountClick: () -> Unit) {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
.statusBarsPadding()
|
||||
.navigationBarsPadding(),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
item {
|
||||
Text("Wähle einen Account", style = MaterialTheme.typography.headlineSmall)
|
||||
}
|
||||
|
||||
items(accounts) { account ->
|
||||
Card(modifier = Modifier.fillMaxWidth().clickable{ onAccountClick(account) }) {
|
||||
Row(modifier = Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(modifier = Modifier.size(40.dp).background(Color.Gray, shape = CircleShape))
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
Column {
|
||||
Text(text = account.name, fontWeight = FontWeight.Bold)
|
||||
Text(text = account.wgName, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Button(
|
||||
onClick = onAddAccountClick,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Anderen Account hinzufügen")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LoginScreen(onLogin: (String, String, String) -> Unit, onBack: (() -> Unit)? = null) {
|
||||
|
||||
if (onBack != null) {
|
||||
BackHandler {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
|
||||
var serverUrl by remember { mutableStateOf("") }
|
||||
var username by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
|
||||
Column(modifier = Modifier.padding(16.dp).statusBarsPadding().navigationBarsPadding()) {
|
||||
Text("Bitte anmelden")
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Home-Server
|
||||
TextField(
|
||||
value = serverUrl,
|
||||
onValueChange = { serverUrl = it },
|
||||
label = { Text("Server-URL") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Username
|
||||
TextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
label = { Text("Nutzername") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Password
|
||||
TextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
label = { Text("Passwort") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Button(onClick = { if(serverUrl.isNotEmpty() && username.isNotEmpty() && password.isNotEmpty()) onLogin(
|
||||
serverUrl,
|
||||
username,
|
||||
password
|
||||
) }) {
|
||||
Text("Login")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DashboardScreen(
|
||||
account: Account,
|
||||
onBack: () -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
sessionState: SessionState,
|
||||
onValidate: () -> Unit,
|
||||
onSessionInvalid: () -> Unit) {
|
||||
|
||||
LaunchedEffect(Unit) { onValidate() }
|
||||
|
||||
when (sessionState) {
|
||||
SessionState.Loading -> {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
}
|
||||
SessionState.Valid -> {
|
||||
BackHandler {
|
||||
onBack()
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
.statusBarsPadding()
|
||||
.navigationBarsPadding()
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column {
|
||||
Text(text = "Hallo, ${account.name}!", style = MaterialTheme.typography.headlineMedium)
|
||||
Text(text = "WG: ${account.wgName}", style = MaterialTheme.typography.bodyLarge, color = Color.Gray)
|
||||
}
|
||||
Button(onClick = onBack) {
|
||||
Text("Wechseln")
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(5.dp))
|
||||
|
||||
Button(onClick = onDelete) {
|
||||
Text("Löschen")
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.surfaceVariant, shape = MaterialTheme.shapes.medium),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text("Hier kommen bald deine WG-Kosten hin 🚀")
|
||||
}
|
||||
}
|
||||
}
|
||||
SessionState.Invalid -> {
|
||||
LaunchedEffect(Unit) {
|
||||
onSessionInvalid()
|
||||
}
|
||||
}
|
||||
is SessionState.Error -> {
|
||||
Text("Server error")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,11 +12,13 @@ import java.util.UUID
|
||||
@Entity(tableName = "expenses")
|
||||
data class Expense (
|
||||
val id: UUID,
|
||||
val amt: Double,
|
||||
val desc: String,
|
||||
|
||||
val payerId: UUID,
|
||||
val debtors: List<User>
|
||||
val payer_id: UUID,
|
||||
val amount: Int,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val attachments: List<String>,
|
||||
val created_at: Int,
|
||||
val last_updated_at: Int
|
||||
)
|
||||
|
||||
@Dao
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.miaurizius.shap_planner.entities
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(tableName = "expense_shares")
|
||||
data class ExpenseShare(
|
||||
val id: UUID,
|
||||
val expense_id: UUID,
|
||||
val user_id: UUID,
|
||||
val share_cents: Int
|
||||
)
|
||||
|
||||
@Dao
|
||||
interface ExpenseShareDao {
|
||||
@Query("SELECT * FROM expense_shares")
|
||||
fun getAllShares(): Flow<List<ExpenseShare>>
|
||||
|
||||
@Query("SELECT * FROM expense_shares WHERE id = :shareId")
|
||||
fun getShareById(shareId: UUID)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertShare(share: ExpenseShare)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteShare(share: ExpenseShare)
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import java.util.UUID
|
||||
data class User (
|
||||
val id: UUID,
|
||||
val name: String,
|
||||
val avatar_url: String?
|
||||
)
|
||||
|
||||
@Dao
|
||||
|
||||
@@ -2,25 +2,34 @@ package de.miaurizius.shap_planner.network
|
||||
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.DELETE
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.Headers
|
||||
import retrofit2.http.POST
|
||||
|
||||
data class LoginRequest(val username: String, val password: String)
|
||||
data class LoginUser(val id: String, val username: String, val role: String, val avatarUrl: String?)
|
||||
data class LoginResponse(val access_token: String, val refresh_token: String, val user: LoginUser, val wgName: String)
|
||||
|
||||
data class RefreshRequest(val refreshToken: String)
|
||||
data class RefreshResponse(val accessToken: String, val refreshToken: String)
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Query
|
||||
import java.util.UUID
|
||||
|
||||
interface APIService {
|
||||
// Account
|
||||
@POST("api/login")
|
||||
suspend fun login(@Body req: LoginRequest): Response<LoginResponse>
|
||||
|
||||
@POST("api/refresh")
|
||||
suspend fun refresh(@Body req: RefreshRequest): Response<RefreshResponse>
|
||||
|
||||
@GET("api/ping")
|
||||
suspend fun ping(@Header("Authorization") token: String): Response<Map<String, String>>
|
||||
|
||||
// Expenses
|
||||
@GET("api/expenses")
|
||||
suspend fun expenseGet(@Header("Authorization") token: String): Response<ExpenseResponse>
|
||||
@POST("api/expenses")
|
||||
suspend fun expenseCreate(@Header("Authorization") token: String)
|
||||
@PUT("api/expenses")
|
||||
suspend fun expenseUpdate(@Header("Authorization") token: String)
|
||||
@DELETE("api/expenses")
|
||||
suspend fun expenseDelete(@Header("Authorization") token: String)
|
||||
|
||||
// User
|
||||
@GET("api/userinfo")
|
||||
suspend fun userinfo(@Header("Authorization") token: String, @Query("id") userId: UUID)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.miaurizius.shap_planner.network
|
||||
|
||||
import de.miaurizius.shap_planner.entities.Expense
|
||||
|
||||
// Login
|
||||
data class LoginRequest(val username: String, val password: String)
|
||||
data class LoginUser(val id: String, val username: String, val role: String, val avatarUrl: String?)
|
||||
data class LoginResponse(val access_token: String, val refresh_token: String, val user: LoginUser, val wgName: String)
|
||||
|
||||
// Refresh-Tokens
|
||||
data class RefreshRequest(val refresh_token: String)
|
||||
data class RefreshResponse(val access_token: String, val refresh_token: String)
|
||||
|
||||
// Expenses
|
||||
data class ExpenseResponse(val expenses: List<Expense>)
|
||||
@@ -8,7 +8,7 @@ import retrofit2.converter.gson.GsonConverterFactory
|
||||
object RetrofitProvider {
|
||||
|
||||
fun create(serverUrl: String): APIService {
|
||||
val base = if (serverUrl.endsWith("/")) serverUrl else "$serverUrl/"
|
||||
val base = if (serverUrl.endsWith("/")) "https://$serverUrl" else "https://$serverUrl/"
|
||||
|
||||
val logger = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import androidx.room.RoomDatabase
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
import de.miaurizius.shap_planner.entities.AccountDao
|
||||
|
||||
@Database(entities = [Account::class], version = 3)
|
||||
@Database(entities = [Account::class], version = 4)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun accountDao(): AccountDao
|
||||
companion object {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.miaurizius.shap_planner.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
import de.miaurizius.shap_planner.entities.Expense
|
||||
import de.miaurizius.shap_planner.network.SessionState
|
||||
import de.miaurizius.shap_planner.ui.screens.AccountSelectionScreen
|
||||
import de.miaurizius.shap_planner.ui.screens.DashboardScreen
|
||||
import de.miaurizius.shap_planner.ui.screens.LoginScreen
|
||||
import de.miaurizius.shap_planner.viewmodels.MainViewModel
|
||||
|
||||
@Composable
|
||||
fun AppContent(
|
||||
// Login
|
||||
accountList: List<Account>,
|
||||
selectedAccount: Account?,
|
||||
showLoginForNewAccount: Boolean,
|
||||
onLogin: (String, String, String) -> Unit,
|
||||
|
||||
// Expenses
|
||||
expenses: List<Expense>,
|
||||
onExpenseClick: (Expense) -> Unit,
|
||||
|
||||
// Account
|
||||
onSelectAccount: (Account) -> Unit,
|
||||
onLogoutAccount: () -> Unit,
|
||||
onAddAccountClick: () -> Unit,
|
||||
onDeleteAccount: () -> Unit,
|
||||
|
||||
// Session
|
||||
sessionState: SessionState,
|
||||
onValidateSession: () -> Unit,
|
||||
onSessionInvalid: () -> Unit,
|
||||
|
||||
//Important
|
||||
viewModel: MainViewModel
|
||||
) {
|
||||
when {
|
||||
showLoginForNewAccount -> LoginScreen(onLogin)
|
||||
accountList.isEmpty() -> LoginScreen(onLogin)
|
||||
selectedAccount != null -> DashboardScreen(
|
||||
// Data and regarding Methods
|
||||
account = selectedAccount,
|
||||
expenses = expenses,
|
||||
onExpenseClick = onExpenseClick,
|
||||
|
||||
// Default Methods
|
||||
mainViewModel = viewModel,
|
||||
onBack = onLogoutAccount,
|
||||
onDelete = onDeleteAccount,
|
||||
sessionState = sessionState,
|
||||
onValidate = onValidateSession,
|
||||
onSessionInvalid = onSessionInvalid
|
||||
)
|
||||
else -> AccountSelectionScreen(
|
||||
accounts = accountList,
|
||||
onAccountClick = onSelectAccount,
|
||||
onAddAccountClick = onAddAccountClick
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package de.miaurizius.shap_planner.ui.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
|
||||
@Composable
|
||||
fun AccountSelectionScreen(accounts: List<Account>, onAccountClick: (Account) -> Unit, onAddAccountClick: () -> Unit) {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
.statusBarsPadding()
|
||||
.navigationBarsPadding(),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
item {
|
||||
Text("Wähle einen Account", style = MaterialTheme.typography.headlineSmall)
|
||||
}
|
||||
|
||||
items(accounts) { account ->
|
||||
Card(modifier = Modifier.fillMaxWidth().clickable{ onAccountClick(account) }) {
|
||||
Row(modifier = Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(modifier = Modifier.size(40.dp).background(Color.Gray, shape = CircleShape))
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
Column {
|
||||
Text(text = account.name, fontWeight = FontWeight.Bold)
|
||||
Text(text = account.wgName, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Button(
|
||||
onClick = onAddAccountClick,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Anderen Account hinzufügen")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package de.miaurizius.shap_planner.ui.screens
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
import de.miaurizius.shap_planner.entities.Expense
|
||||
import de.miaurizius.shap_planner.network.SessionState
|
||||
import de.miaurizius.shap_planner.viewmodels.MainViewModel
|
||||
|
||||
@Composable
|
||||
fun DashboardScreen(
|
||||
// Data and regarding Methods
|
||||
account: Account,
|
||||
expenses: List<Expense>,
|
||||
onExpenseClick: (Expense) -> Unit,
|
||||
|
||||
// Default Methods
|
||||
mainViewModel: MainViewModel,
|
||||
onBack: () -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
sessionState: SessionState,
|
||||
onValidate: () -> Unit,
|
||||
onSessionInvalid: () -> Unit) {
|
||||
|
||||
LaunchedEffect(Unit) { onValidate() }
|
||||
mainViewModel.loadExpenses(account)
|
||||
when (sessionState) {
|
||||
SessionState.Loading -> {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
}
|
||||
|
||||
SessionState.Valid -> {
|
||||
BackHandler {
|
||||
onBack()
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
.statusBarsPadding()
|
||||
.navigationBarsPadding()
|
||||
) {
|
||||
// Header
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column {
|
||||
Text(
|
||||
text = "Hallo, ${account.name}!",
|
||||
style = MaterialTheme.typography.headlineMedium
|
||||
)
|
||||
Text(
|
||||
text = "WG: ${account.wgName}",
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = Color.Gray
|
||||
)
|
||||
}
|
||||
Button(onClick = onBack) {
|
||||
Text("Wechseln")
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(5.dp))
|
||||
|
||||
Button(onClick = onDelete) {
|
||||
Text("Löschen")
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
|
||||
Text("WG-Kosten", style = MaterialTheme.typography.titleLarge)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.surfaceVariant, shape = MaterialTheme.shapes.medium),
|
||||
contentPadding = PaddingValues(8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
items(expenses) { expense ->
|
||||
ExpenseItem(expense = expense, onClick = { onExpenseClick(expense) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SessionState.Invalid -> {
|
||||
LaunchedEffect(Unit) {
|
||||
onSessionInvalid()
|
||||
}
|
||||
}
|
||||
|
||||
is SessionState.Error -> {
|
||||
Text("Server error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ExpenseItem(expense: Expense, onClick: () -> Unit) {
|
||||
Surface(modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable{onClick()},
|
||||
shape = MaterialTheme.shapes.small,
|
||||
color = MaterialTheme.colorScheme.surface) {
|
||||
Row(modifier = Modifier.padding(16.dp), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(text = expense.title, style = MaterialTheme.typography.bodyLarge)
|
||||
Text(text = expense.amount.toString()+"€", style = MaterialTheme.typography.bodyLarge, fontWeight = FontWeight.Bold)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package de.miaurizius.shap_planner.ui.screens
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.statusBarsPadding
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@Composable
|
||||
fun LoginScreen(onLogin: (String, String, String) -> Unit, onBack: (() -> Unit)? = null) {
|
||||
|
||||
if (onBack != null) {
|
||||
BackHandler {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
|
||||
var serverUrl by remember { mutableStateOf("") }
|
||||
var username by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
|
||||
Column(modifier = Modifier.padding(16.dp).statusBarsPadding().navigationBarsPadding()) {
|
||||
Text("Bitte anmelden")
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Home-Server
|
||||
TextField(
|
||||
value = serverUrl,
|
||||
onValueChange = { serverUrl = it },
|
||||
label = { Text("Server-Domain") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Username
|
||||
TextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
label = { Text("Nutzername") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
//Password
|
||||
TextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
label = { Text("Passwort") }
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Button(onClick = { if(serverUrl.isNotEmpty() && username.isNotEmpty() && password.isNotEmpty()) onLogin(
|
||||
serverUrl,
|
||||
username,
|
||||
password
|
||||
) }) {
|
||||
Text("Login")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Locale
|
||||
import java.util.Locale.getDefault
|
||||
import java.util.UUID
|
||||
|
||||
|
||||
@@ -8,9 +8,11 @@ import androidx.lifecycle.viewModelScope
|
||||
import de.miaurizius.shap_planner.TokenStorage
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
import de.miaurizius.shap_planner.entities.AccountDao
|
||||
import de.miaurizius.shap_planner.entities.Expense
|
||||
import de.miaurizius.shap_planner.network.RefreshRequest
|
||||
import de.miaurizius.shap_planner.network.RetrofitProvider
|
||||
import de.miaurizius.shap_planner.network.SessionState
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
@@ -19,12 +21,32 @@ import kotlin.collections.emptyList
|
||||
|
||||
class MainViewModel(private val accountDao: AccountDao, private val tokenStorage: TokenStorage) : ViewModel() {
|
||||
|
||||
var selectedAccount by mutableStateOf<Account?>(null)
|
||||
private set
|
||||
val accounts: StateFlow<List<Account>> = accountDao.getAllAccounts()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
var sessionState by mutableStateOf<SessionState>(SessionState.Loading)
|
||||
private set
|
||||
|
||||
private val _expenses = MutableStateFlow<List<Expense>>(emptyList())
|
||||
val expenses: StateFlow<List<Expense>> = _expenses
|
||||
|
||||
fun loadExpenses(account: Account) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val api = RetrofitProvider.create(account.serverUrl)
|
||||
val accessToken = tokenStorage.getAccess(account.id.toString())
|
||||
|
||||
val response = api.expenseGet("Bearer $accessToken")
|
||||
if (response.isSuccessful) {
|
||||
_expenses.value = response.body()?.expenses ?: emptyList()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_expenses.value = emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun validateSession(account: Account) {
|
||||
viewModelScope.launch {
|
||||
sessionState = SessionState.Loading
|
||||
@@ -38,7 +60,6 @@ class MainViewModel(private val accountDao: AccountDao, private val tokenStorage
|
||||
return@launch
|
||||
}
|
||||
|
||||
println("Testing with AT $accessToken")
|
||||
val pingResponse = api.ping("Bearer $accessToken")
|
||||
|
||||
if(pingResponse.isSuccessful) {
|
||||
@@ -47,7 +68,6 @@ class MainViewModel(private val accountDao: AccountDao, private val tokenStorage
|
||||
}
|
||||
|
||||
if(pingResponse.code() == 401) {
|
||||
println("Testing with RT $refreshToken")
|
||||
val refreshResponse = api.refresh(RefreshRequest(refreshToken))
|
||||
|
||||
if(refreshResponse.isSuccessful) {
|
||||
@@ -55,11 +75,16 @@ class MainViewModel(private val accountDao: AccountDao, private val tokenStorage
|
||||
|
||||
tokenStorage.saveTokens(
|
||||
account.id.toString(),
|
||||
newTokens.accessToken,
|
||||
newTokens.accessToken
|
||||
newTokens.access_token,
|
||||
newTokens.refresh_token
|
||||
)
|
||||
|
||||
sessionState = SessionState.Valid
|
||||
|
||||
// Fetch data
|
||||
loadExpenses(account)
|
||||
println("All data fetched")
|
||||
|
||||
return@launch
|
||||
} else {
|
||||
sessionState = SessionState.Invalid
|
||||
@@ -75,7 +100,6 @@ class MainViewModel(private val accountDao: AccountDao, private val tokenStorage
|
||||
accountDao.insertAccount(account)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAccount(account: Account) {
|
||||
viewModelScope.launch {
|
||||
accountDao.deleteAccount(account)
|
||||
@@ -83,14 +107,9 @@ class MainViewModel(private val accountDao: AccountDao, private val tokenStorage
|
||||
selectedAccount = null
|
||||
}
|
||||
}
|
||||
|
||||
var selectedAccount by mutableStateOf<Account?>(null)
|
||||
private set
|
||||
|
||||
fun selectAccount(account: Account) {
|
||||
selectedAccount = account
|
||||
}
|
||||
|
||||
fun logoutFromAccount() {
|
||||
selectedAccount = null
|
||||
}
|
||||
|
||||
8
app/src/main/res/xml/network_security_config.xml
Normal file
8
app/src/main/res/xml/network_security_config.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<network-security-config>
|
||||
<debug-overrides>
|
||||
<trust-anchors>
|
||||
<certificates src="user" />
|
||||
</trust-anchors>
|
||||
</debug-overrides>
|
||||
</network-security-config>
|
||||
Reference in New Issue
Block a user