Implemented saving UiSettings

This commit is contained in:
dankito 2024-09-17 03:39:08 +02:00
parent de36a403df
commit 2d472d1683
10 changed files with 91 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import net.codinux.banking.dataaccess.entities.HoldingEntity
import net.codinux.banking.dataaccess.entities.BankAccessEntity import net.codinux.banking.dataaccess.entities.BankAccessEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.settings.AppSettings import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.banking.ui.settings.UiSettings
interface BankingRepository { interface BankingRepository {
@ -16,6 +17,10 @@ interface BankingRepository {
suspend fun saveAppSettings(settings: AppSettings) suspend fun saveAppSettings(settings: AppSettings)
fun getUiSettings(settings: UiSettings)
suspend fun saveUiSettings(settings: UiSettings)
fun getAllBanks(): List<BankAccessEntity> fun getAllBanks(): List<BankAccessEntity>

View File

@ -9,6 +9,7 @@ import net.codinux.banking.dataaccess.entities.HoldingEntity
import net.codinux.banking.dataaccess.entities.BankAccessEntity import net.codinux.banking.dataaccess.entities.BankAccessEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.settings.AppSettings import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.banking.ui.settings.UiSettings
class InMemoryBankingRepository( class InMemoryBankingRepository(
banks: Collection<BankAccess> = emptyList(), banks: Collection<BankAccess> = emptyList(),
@ -22,6 +23,8 @@ class InMemoryBankingRepository(
private val transactions = transactions.map { map(it) }.toMutableList() private val transactions = transactions.map { map(it) }.toMutableList()
private lateinit var uiSettings: UiSettings
override fun getAppSettings(): AppSettings? = appSettings override fun getAppSettings(): AppSettings? = appSettings
@ -29,6 +32,14 @@ class InMemoryBankingRepository(
this.appSettings = settings this.appSettings = settings
} }
override fun getUiSettings(settings: UiSettings) {
this.uiSettings = settings
}
override suspend fun saveUiSettings(settings: UiSettings) {
this.uiSettings = settings
}
override fun getAllBanks(): List<BankAccessEntity> = banks.toList() override fun getAllBanks(): List<BankAccessEntity> = banks.toList()

View File

@ -8,8 +8,10 @@ import net.codinux.banking.client.model.securitiesaccount.Holding
import net.codinux.banking.client.model.tan.* import net.codinux.banking.client.model.tan.*
import net.codinux.banking.dataaccess.entities.* import net.codinux.banking.dataaccess.entities.*
import net.codinux.banking.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.TransactionsGrouping
import net.codinux.banking.ui.model.settings.AppAuthenticationMethod import net.codinux.banking.ui.model.settings.AppAuthenticationMethod
import net.codinux.banking.ui.model.settings.AppSettings import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.banking.ui.settings.UiSettings
import net.codinux.log.logger import net.codinux.log.logger
import kotlin.enums.EnumEntries import kotlin.enums.EnumEntries
import kotlin.js.JsName import kotlin.js.JsName
@ -40,6 +42,21 @@ open class SqliteBankingRepository(
} }
override fun getUiSettings(settings: UiSettings) {
settingsQueries.getUiSettings { _, transactionsGrouping, showBalance, showBankIcons, showColoredAmounts, showTransactionsInAlternatingColors ->
settings.transactionsGrouping.value = mapToEnum(transactionsGrouping, TransactionsGrouping.entries)
settings.showBalance.value = showBalance
settings.showBankIcons.value = showBankIcons
settings.showColoredAmounts.value = showColoredAmounts
settings.showTransactionsInAlternatingColors.value = showTransactionsInAlternatingColors
}.executeAsOneOrNull()
}
override suspend fun saveUiSettings(settings: UiSettings) {
settingsQueries.upsertUiSettings(mapEnum(settings.transactionsGrouping.value), settings.showBalance.value, settings.showBankIcons.value, settings.showColoredAmounts.value, settings.showTransactionsInAlternatingColors.value)
}
override fun getAllBanks(): List<BankAccessEntity> { override fun getAllBanks(): List<BankAccessEntity> {
val bankAccounts = getAllBankAccounts().groupBy { it.bankId } val bankAccounts = getAllBankAccounts().groupBy { it.bankId }
val tanMethods = getAllTanMethods().groupBy { it.bankId }.mapValues { it.value.toMutableList() } val tanMethods = getAllTanMethods().groupBy { it.bankId }.mapValues { it.value.toMutableList() }

View File

@ -23,7 +23,7 @@ fun UiSettings(modifier: Modifier, textColor: Color = Color.Unspecified) {
val transactionsGrouping by uiSettings.transactionsGrouping.collectAsState() val transactionsGrouping by uiSettings.transactionsGrouping.collectAsState()
val zebraStripes by uiSettings.zebraStripes.collectAsState() val showTransactionsInAlternatingColors by uiSettings.showTransactionsInAlternatingColors.collectAsState()
val showBankIcons by uiSettings.showBankIcons.collectAsState() val showBankIcons by uiSettings.showBankIcons.collectAsState()
@ -33,7 +33,7 @@ fun UiSettings(modifier: Modifier, textColor: Color = Color.Unspecified) {
Column(modifier) { Column(modifier) {
BooleanOption("Kontostand anzeigen", showBalance, textColor = textColor) { uiSettings.showBalance.value = it } BooleanOption("Kontostand anzeigen", showBalance, textColor = textColor) { uiSettings.showBalance.value = it }
BooleanOption("Zebra Stripes", zebraStripes, textColor = textColor) { uiSettings.zebraStripes.value = it } BooleanOption("Umsätze in alternierenden Farben anzeigen", showTransactionsInAlternatingColors, textColor = textColor) { uiSettings.showTransactionsInAlternatingColors.value = it }
BooleanOption("Bank Icons anzeigen", showBankIcons, textColor = textColor) { uiSettings.showBankIcons.value = it } BooleanOption("Bank Icons anzeigen", showBankIcons, textColor = textColor) { uiSettings.showBankIcons.value = it }

View File

@ -9,7 +9,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import net.codinux.banking.client.model.Amount import net.codinux.banking.client.model.Amount
@ -32,11 +31,11 @@ fun HoldingListItem(holding: Holding, isOddItem: Boolean = false, isNotLastItem:
// TODO: also regard showBalance? // TODO: also regard showBalance?
val showColoredAmounts by uiSettings.showColoredAmounts.collectAsState() val showColoredAmounts by uiSettings.showColoredAmounts.collectAsState()
val zebraStripes by uiSettings.zebraStripes.collectAsState() val showTransactionsInAlternatingColors by uiSettings.showTransactionsInAlternatingColors.collectAsState()
val showBankIcons by uiSettings.showBankIcons.collectAsState() val showBankIcons by uiSettings.showBankIcons.collectAsState()
val backgroundColor = if (zebraStripes && isOddItem) Colors.ZebraStripesColor else Color.White val backgroundColor = if (showTransactionsInAlternatingColors && isOddItem) Colors.ZebraStripesColor else Color.White
val currency = holding.currency ?: fallbackCurrency val currency = holding.currency ?: fallbackCurrency

View File

@ -28,13 +28,13 @@ private val formatUtil = DI.formatUtil
@Composable @Composable
fun TransactionListItem(bank: BankAccess?, transaction: AccountTransactionViewModel, itemIndex: Int, countItems: Int) { fun TransactionListItem(bank: BankAccess?, transaction: AccountTransactionViewModel, itemIndex: Int, countItems: Int) {
val zebraStripes by uiSettings.zebraStripes.collectAsState() val showTransactionsInAlternatingColors by uiSettings.showTransactionsInAlternatingColors.collectAsState()
val showBankIcons by uiSettings.showBankIcons.collectAsState() val showBankIcons by uiSettings.showBankIcons.collectAsState()
val showColoredAmounts by uiSettings.showColoredAmounts.collectAsState() val showColoredAmounts by uiSettings.showColoredAmounts.collectAsState()
val backgroundColor = if (zebraStripes && itemIndex % 2 == 1) Colors.ZebraStripesColor else Color.White val backgroundColor = if (showTransactionsInAlternatingColors && itemIndex % 2 == 1) Colors.ZebraStripesColor else Color.White
val bottomPadding = 56.dp val bottomPadding = 56.dp

View File

@ -36,7 +36,7 @@ object DI {
var bankingRepository: BankingRepository = InMemoryBankingRepository(emptyList()) var bankingRepository: BankingRepository = InMemoryBankingRepository(emptyList())
val bankingService by lazy { BankingService(uiState, bankingRepository, bankFinder) } val bankingService by lazy { BankingService(uiState, uiSettings, bankingRepository, bankFinder) }
fun setRepository(sqlDriver: SqlDriver) = setRepository(SqliteBankingRepository(sqlDriver)) fun setRepository(sqlDriver: SqlDriver) = setRepository(SqliteBankingRepository(sqlDriver))

View File

@ -1,7 +1,9 @@
package net.codinux.banking.ui.service package net.codinux.banking.ui.service
import androidx.lifecycle.viewModelScope
import bankmeister.composeapp.generated.resources.Res import bankmeister.composeapp.generated.resources.Res
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import net.codinux.banking.client.SimpleBankingClientCallback import net.codinux.banking.client.SimpleBankingClientCallback
import net.codinux.banking.client.fints4k.FinTs4kBankingClient import net.codinux.banking.client.fints4k.FinTs4kBankingClient
@ -23,6 +25,7 @@ import net.codinux.banking.ui.model.error.*
import net.codinux.banking.ui.model.events.AccountTransactionsRetrievedEvent import net.codinux.banking.ui.model.events.AccountTransactionsRetrievedEvent
import net.codinux.banking.ui.model.events.TransferredMoneyEvent import net.codinux.banking.ui.model.events.TransferredMoneyEvent
import net.codinux.banking.ui.model.settings.AppSettings import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.banking.ui.settings.UiSettings
import net.codinux.banking.ui.state.UiState import net.codinux.banking.ui.state.UiState
import net.codinux.csv.reader.CsvReader import net.codinux.csv.reader.CsvReader
import net.codinux.log.logger import net.codinux.log.logger
@ -31,6 +34,7 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
class BankingService( class BankingService(
private val uiState: UiState, private val uiState: UiState,
private val uiSettings: UiSettings,
private val bankingRepository: BankingRepository, private val bankingRepository: BankingRepository,
private val bankFinder: BankFinder private val bankFinder: BankFinder
) { ) {
@ -53,6 +57,10 @@ class BankingService(
} }
uiState.appSettings.value = appSettings uiState.appSettings.value = appSettings
bankingRepository.getUiSettings(uiSettings)
updateOnChanges(uiSettings)
uiState.banks.value = getAllBanks() uiState.banks.value = getAllBanks()
@ -68,6 +76,8 @@ class BankingService(
suspend fun saveAppSettings(settings: AppSettings) = bankingRepository.saveAppSettings(settings) suspend fun saveAppSettings(settings: AppSettings) = bankingRepository.saveAppSettings(settings)
suspend fun saveUiSettings(settings: UiSettings) = bankingRepository.saveUiSettings(settings)
fun getAllBanks() = bankingRepository.getAllBanks() fun getAllBanks() = bankingRepository.getAllBanks()
@ -281,6 +291,24 @@ class BankingService(
} }
private suspend fun updateOnChanges(uiSettings: UiSettings) {
updateOnChanges(uiSettings, uiSettings.transactionsGrouping)
updateOnChanges(uiSettings, uiSettings.showBalance)
updateOnChanges(uiSettings, uiSettings.showBankIcons)
updateOnChanges(uiSettings, uiSettings.showColoredAmounts)
updateOnChanges(uiSettings, uiSettings.showTransactionsInAlternatingColors)
}
private suspend fun updateOnChanges(uiSettings: UiSettings, state: MutableStateFlow<*>) {
uiSettings.viewModelScope.launch(Dispatchers.Unconfined) {
state.collect {
saveUiSettings(uiSettings)
}
}
}
private suspend fun readTransactionsFromCsv(): List<AccountTransaction> { private suspend fun readTransactionsFromCsv(): List<AccountTransaction> {
val csv = Res.readBytes("files/transactions.csv").decodeToString() val csv = Res.readBytes("files/transactions.csv").decodeToString()
val csvReader = CsvReader(hasHeaderRow = true, reuseRowInstance = true, skipEmptyRows = true).read(csv) val csvReader = CsvReader(hasHeaderRow = true, reuseRowInstance = true, skipEmptyRows = true).read(csv)

View File

@ -10,7 +10,7 @@ class UiSettings : ViewModel() {
val transactionsGrouping = MutableStateFlow(TransactionsGrouping.Month) val transactionsGrouping = MutableStateFlow(TransactionsGrouping.Month)
val zebraStripes = MutableStateFlow(true) val showTransactionsInAlternatingColors = MutableStateFlow(true)
val showBankIcons = MutableStateFlow(true) val showBankIcons = MutableStateFlow(true)

View File

@ -1,5 +1,6 @@
import kotlin.Boolean; import kotlin.Boolean;
CREATE TABLE IF NOT EXISTS AppSettings ( CREATE TABLE IF NOT EXISTS AppSettings (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -17,3 +18,24 @@ SELECT * FROM AppSettings WHERE id = 1;
upsertAppSettings: upsertAppSettings:
INSERT OR REPLACE INTO AppSettings(id, authenticationMethod, hashedPassword, updateAccountsOnAppStart, updateAccountsIfNoUpdatedForHours) INSERT OR REPLACE INTO AppSettings(id, authenticationMethod, hashedPassword, updateAccountsOnAppStart, updateAccountsIfNoUpdatedForHours)
VALUES (1, ?, ?, ?, ?); VALUES (1, ?, ?, ?, ?);
CREATE TABLE IF NOT EXISTS UiSettings (
id INTEGER PRIMARY KEY,
transactionsGrouping TEXT NOT NULL,
showBalance INTEGER AS Boolean NOT NULL,
showBankIcons INTEGER AS Boolean NOT NULL,
showColoredAmounts INTEGER AS Boolean NOT NULL,
showTransactionsInAlternatingColors INTEGER AS Boolean NOT NULL
);
getUiSettings:
SELECT * FROM UiSettings WHERE id = 1;
upsertUiSettings:
INSERT OR REPLACE INTO UiSettings(id, transactionsGrouping, showBalance, showBankIcons, showColoredAmounts, showTransactionsInAlternatingColors)
VALUES (1, ?, ?, ?, ?, ?);