From 3c2eb3d4d76e933523938790d65edb2a2afc635c Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 20 Sep 2024 22:10:24 +0200 Subject: [PATCH] Implemented updating BankAccess properties --- .../banking/persistence/BankingRepository.kt | 2 ++ .../persistence/InMemoryBankingRepository.kt | 4 +++ .../persistence/SqliteBankingRepository.kt | 16 +++++++++ .../net/codinux/banking/persistence/Bank.sq | 16 +++++++++ .../banking/ui/screens/BankSettingsScreen.kt | 24 +++++++++++--- .../banking/ui/screens/FullscreenViewBase.kt | 19 +++++++---- .../banking/ui/service/BankingService.kt | 33 ++++++++++++++++--- 7 files changed, 97 insertions(+), 17 deletions(-) diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt index aa81757..9e0eb7a 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt @@ -26,6 +26,8 @@ interface BankingRepository { suspend fun persistBank(bank: BankAccess): BankAccessEntity + suspend fun updateBank(bank: BankAccessEntity, loginName: String, password: String, bankName: String?) + suspend fun deleteBank(bank: BankAccessEntity) diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt index 3057ee5..2de7e3a 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt @@ -48,6 +48,10 @@ class InMemoryBankingRepository( return entity } + override suspend fun updateBank(bank: BankAccessEntity, loginName: String, password: String, bankName: String?) { + // no-op + } + override suspend fun deleteBank(bank: BankAccessEntity) { this.banks.remove(bank) } diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt index 5369d40..c6f1601 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt @@ -125,6 +125,22 @@ open class SqliteBankingRepository : BankingRepository { } } + override suspend fun updateBank(bank: BankAccessEntity, loginName: String, password: String, userSetDisplayName: String?) { + bankQueries.transaction { + if (bank.loginName != loginName) { + bankQueries.updateBankLoginName(loginName, bank.id) + } + + if (bank.password != password) { + bankQueries.updateBankPassword(password, bank.id) + } + + if (bank.userSetDisplayName != userSetDisplayName) { + bankQueries.updateBankUserSetDisplayName(userSetDisplayName, bank.id) + } + } + } + override suspend fun deleteBank(bank: BankAccessEntity) { bankQueries.transaction { accountTransactionQueries.deleteTransactionsByBankId(bankId = bank.id) diff --git a/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq b/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq index ed5ec98..200aebe 100644 --- a/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq +++ b/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq @@ -79,6 +79,22 @@ SELECT BankAccess.* FROM BankAccess; +updateBankLoginName: +UPDATE BankAccess +SET loginName = ? +WHERE id = ?; + +updateBankPassword: +UPDATE BankAccess +SET password = ? +WHERE id = ?; + +updateBankUserSetDisplayName: +UPDATE BankAccess +SET userSetDisplayName = ? +WHERE id = ?; + + deleteBank { DELETE FROM TanMethod WHERE bankId = :bankId; diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/BankSettingsScreen.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/BankSettingsScreen.kt index 11afcb6..cfecdc1 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/BankSettingsScreen.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/BankSettingsScreen.kt @@ -28,15 +28,22 @@ fun BankSettingsScreen(bank: BankAccessEntity, onClosed: () -> Unit) { var showDeleteBankAccessConfirmationDialog by remember { mutableStateOf(false) } - val coroutineScope = rememberCoroutineScope() - - val hasDataChanged by remember(enteredBankName) { + val hasDataChanged by remember(enteredBankName, enteredLoginName, enteredPassword) { mutableStateOf( (enteredBankName != bank.bankName && (bank.userSetDisplayName == null || enteredBankName != bank.userSetDisplayName)) - || enteredLoginName != bank.loginName || enteredPassword != bank.password + || (enteredLoginName != bank.loginName && enteredLoginName.isNotBlank()) + || (enteredPassword != bank.password && enteredPassword.isNotBlank()) ) } + val coroutineScope = rememberCoroutineScope() + + + fun saveChanges() { + coroutineScope.launch { + DI.bankingService.updateBank(bank, enteredLoginName, enteredPassword, enteredBankName.takeUnless { it.isBlank() }) + } + } if (showDeleteBankAccessConfirmationDialog) { ConfirmDialog( @@ -53,7 +60,14 @@ fun BankSettingsScreen(bank: BankAccessEntity, onClosed: () -> Unit) { } - FullscreenViewBase(bank.displayName, onClosed = onClosed) { + FullscreenViewBase( + bank.displayName, + confirmButtonTitle = "Speichern", + confirmButtonEnabled = hasDataChanged, + showDismissButton = true, + onConfirm = { saveChanges() }, + onClosed = onClosed + ) { Column(Modifier.fillMaxSize().verticalScroll().padding(8.dp)) { Column { OutlinedTextField( diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/FullscreenViewBase.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/FullscreenViewBase.kt index 5dccdf1..ede9e38 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/FullscreenViewBase.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/screens/FullscreenViewBase.kt @@ -23,7 +23,10 @@ fun FullscreenViewBase( title: String, confirmButtonTitle: String = "OK", confirmButtonEnabled: Boolean = true, + dismissButtonTitle: String = "Abbrechen", + showDismissButton: Boolean = false, showButtonBar: Boolean = true, + onConfirm: (() -> Unit)? = null, onClosed: () -> Unit, content: @Composable () -> Unit ) { @@ -49,16 +52,18 @@ fun FullscreenViewBase( if (showButtonBar) { Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { -// TextButton(onClick = onClosed, Modifier.width(Style.DialogButtonWidth)) { -// Text("Abbrechen", color = Colors.CodinuxSecondaryColor) -// } -// -// Spacer(Modifier.width(8.dp)) + if (showDismissButton) { + TextButton(onClick = onClosed, Modifier.weight(1f)) { + Text(dismissButtonTitle, color = Colors.CodinuxSecondaryColor) + } + + Spacer(Modifier.width(8.dp)) + } TextButton( - modifier = Modifier.fillMaxWidth(), + modifier = Modifier.weight(1f), enabled = confirmButtonEnabled, - onClick = { /* onConfirm?.invoke() ?: */ onClosed() } + onClick = { onConfirm?.invoke(); onClosed() } ) { Text(confirmButtonTitle, color = Colors.CodinuxSecondaryColor, textAlign = TextAlign.Center) } diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index 90fc667..7e93fcb 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -72,7 +72,7 @@ class BankingService( updateOnChanges(uiSettings) - uiState.banks.value = getAllBanks() + updateBanksInUi(getAllBanks()) uiState.transactions.value = getAllAccountTransactionsAsViewModel() uiState.holdings.value = uiState.banks.value.flatMap { it.accounts }.flatMap { it.holdings } @@ -93,13 +93,33 @@ class BankingService( fun getAllBanks() = bankingRepository.getAllBanks() + suspend fun updateBank(bank: BankAccessEntity, loginName: String, password: String, bankName: String?) { + try { + bankingRepository.updateBank(bank, loginName, password, bankName) + + if (bank.loginName != loginName) { + bank.loginName = loginName + } + if (bank.password != password) { + bank.password = password + } + if (bank.userSetDisplayName != bankName) { + bank.userSetDisplayName = bankName + } + + updateBanksInUi(uiState.banks.value.toList()) // update displayed banks to reflect new value(s) + } catch (e: Throwable) { +// showAndLogError("Could not update bank $bank", e) + } + } + suspend fun deleteBank(bank: BankAccessEntity) { try { bankingRepository.deleteBank(bank) uiState.transactions.value = uiState.transactions.value.filterNot { it.bankId == bank.id } uiState.holdings.value = uiState.holdings.value.filterNot { it.bankId == bank.id } - uiState.banks.value = uiState.banks.value.toMutableList().also { it.remove(bank) } + updateBanksInUi(uiState.banks.value.toMutableList().also { it.remove(bank) }) } catch (e: Throwable) { log.error(e) { "Could not delete bank ${bank.displayName}" } } @@ -157,9 +177,7 @@ class BankingService( log.info { "Saved bank $newBankEntity with ${newBankEntity.accounts.flatMap { it.bookedTransactions }.size} transactions" } - val banks = uiState.banks.value.toMutableList() - banks.add(newBankEntity) - uiState.banks.value = banks + updateBanksInUi(uiState.banks.value.toMutableList().also { it.add(newBankEntity) }) updateTransactionsInUi(newBankEntity.accounts.flatMap { it.bookedTransactions }) updateHoldingsInUi(newBankEntity.accounts.flatMap { it.holdings }, emptyList()) @@ -264,6 +282,11 @@ class BankingService( } } + + private fun updateBanksInUi(banks: List) { + uiState.banks.value = banks + } + private fun updateTransactionsInUi(addedTransactions: List): List { val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }