Implemented updating BankAccess properties

This commit is contained in:
dankito 2024-09-20 22:10:24 +02:00
parent a88ddbdd16
commit 3c2eb3d4d7
7 changed files with 97 additions and 17 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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;

View File

@ -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(

View File

@ -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)
}

View File

@ -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<BankAccessEntity>) {
uiState.banks.value = banks
}
private fun updateTransactionsInUi(addedTransactions: List<AccountTransactionEntity>): List<AccountTransactionViewModel> {
val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }