Implemented persisting AppSettings

This commit is contained in:
dankito 2024-09-17 02:36:49 +02:00
parent 09a1a41c20
commit debae8b7ca
8 changed files with 85 additions and 1 deletions

View File

@ -8,9 +8,15 @@ import net.codinux.banking.dataaccess.entities.BankAccountEntity
import net.codinux.banking.dataaccess.entities.HoldingEntity
import net.codinux.banking.dataaccess.entities.BankAccessEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.settings.AppSettings
interface BankingRepository {
fun getAppSettings(): AppSettings?
suspend fun saveAppSettings(settings: AppSettings)
fun getAllBanks(): List<BankAccessEntity>
suspend fun persistBank(bank: BankAccess): BankAccessEntity

View File

@ -8,10 +8,12 @@ import net.codinux.banking.dataaccess.entities.BankAccountEntity
import net.codinux.banking.dataaccess.entities.HoldingEntity
import net.codinux.banking.dataaccess.entities.BankAccessEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.settings.AppSettings
class InMemoryBankingRepository(
banks: Collection<BankAccess> = emptyList(),
transactions: Collection<AccountTransaction> = emptyList()
transactions: Collection<AccountTransaction> = emptyList(),
private var appSettings: AppSettings = AppSettings()
) : BankingRepository {
private var nextId = 0L // TODO: make thread-safe
@ -21,6 +23,13 @@ class InMemoryBankingRepository(
private val transactions = transactions.map { map(it) }.toMutableList()
override fun getAppSettings(): AppSettings? = appSettings
override suspend fun saveAppSettings(settings: AppSettings) {
this.appSettings = settings
}
override fun getAllBanks(): List<BankAccessEntity> = banks.toList()
override suspend fun persistBank(bank: BankAccess): BankAccessEntity {

View File

@ -8,6 +8,8 @@ import net.codinux.banking.client.model.securitiesaccount.Holding
import net.codinux.banking.client.model.tan.*
import net.codinux.banking.dataaccess.entities.*
import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.settings.AppAuthenticationMethod
import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.log.logger
import kotlin.enums.EnumEntries
import kotlin.js.JsName
@ -19,6 +21,8 @@ open class SqliteBankingRepository(
private val database = BankmeisterDb(sqlDriver)
private val settingsQueries = database.settingsQueries
private val bankQueries = database.bankQueries
private val accountTransactionQueries = database.accountTransactionQueries
@ -26,6 +30,16 @@ open class SqliteBankingRepository(
private val log by logger()
override fun getAppSettings(): AppSettings? =
settingsQueries.getAppSettings { _, authenticationMethod, hashedPassword, updateAccountsOnAppStart, updateAccountsIfNoUpdatedForHours ->
AppSettings(mapToEnum(authenticationMethod, AppAuthenticationMethod.entries), hashedPassword, updateAccountsOnAppStart, mapToInt(updateAccountsIfNoUpdatedForHours))
}.executeAsOneOrNull()
override suspend fun saveAppSettings(settings: AppSettings) {
settingsQueries.upsertAppSettings(mapEnum(settings.authenticationMethod), settings.hashedPassword, settings.updateAccountsOnAppStart, mapInt(settings.updateAccountsIfNoUpdatedForHours))
}
override fun getAllBanks(): List<BankAccessEntity> {
val bankAccounts = getAllBankAccounts().groupBy { it.bankId }
val tanMethods = getAllTanMethods().groupBy { it.bankId }.mapValues { it.value.toMutableList() }

View File

@ -0,0 +1,7 @@
package net.codinux.banking.ui.model.settings
enum class AppAuthenticationMethod {
None,
Password,
Biometric
}

View File

@ -0,0 +1,11 @@
package net.codinux.banking.ui.model.settings
class AppSettings(
var authenticationMethod: AppAuthenticationMethod = AppAuthenticationMethod.None,
var hashedPassword: String? = null,
var updateAccountsOnAppStart: Boolean = false,
var updateAccountsIfNoUpdatedForHours: Int = 6
) {
override fun toString() = "$authenticationMethod"
}

View File

@ -22,6 +22,7 @@ import net.codinux.banking.ui.model.BankInfo
import net.codinux.banking.ui.model.error.*
import net.codinux.banking.ui.model.events.AccountTransactionsRetrievedEvent
import net.codinux.banking.ui.model.events.TransferredMoneyEvent
import net.codinux.banking.ui.model.settings.AppSettings
import net.codinux.banking.ui.state.UiState
import net.codinux.csv.reader.CsvReader
import net.codinux.log.logger
@ -45,6 +46,14 @@ class BankingService(
suspend fun init() {
try {
var appSettings = getAppSettings()
if (appSettings == null) {
appSettings = AppSettings()
saveAppSettings(appSettings)
}
uiState.appSettings.value = appSettings
uiState.banks.value = getAllBanks()
uiState.transactions.value = getAllAccountTransactionsAsViewModel()
@ -55,6 +64,11 @@ class BankingService(
}
fun getAppSettings() = bankingRepository.getAppSettings()
suspend fun saveAppSettings(settings: AppSettings) = bankingRepository.saveAppSettings(settings)
fun getAllBanks() = bankingRepository.getAllBanks()
fun getAllAccountTransactions() = bankingRepository.getAllAccountTransactions()

View File

@ -15,9 +15,13 @@ import net.codinux.banking.ui.model.error.BankingClientError
import net.codinux.banking.ui.model.error.ErroneousAction
import net.codinux.banking.ui.model.events.AccountTransactionsRetrievedEvent
import net.codinux.banking.ui.model.events.TransferredMoneyEvent
import net.codinux.banking.ui.model.settings.AppSettings
class UiState : ViewModel() {
val appSettings = MutableStateFlow(AppSettings())
val banks = MutableStateFlow<List<BankAccessEntity>>(emptyList())
val transactions = MutableStateFlow<List<AccountTransactionViewModel>>(emptyList())

View File

@ -0,0 +1,19 @@
import kotlin.Boolean;
CREATE TABLE IF NOT EXISTS AppSettings (
id INTEGER PRIMARY KEY,
authenticationMethod TEXT NOT NULL,
hashedPassword TEXT,
updateAccountsOnAppStart INTEGER AS Boolean NOT NULL,
updateAccountsIfNoUpdatedForHours INTEGER NOT NULL
);
getAppSettings:
SELECT * FROM AppSettings WHERE id = 1;
upsertAppSettings:
INSERT OR REPLACE INTO AppSettings(id, authenticationMethod, hashedPassword, updateAccountsOnAppStart, updateAccountsIfNoUpdatedForHours)
VALUES (1, ?, ?, ?, ?);