diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/BankingRepository.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/BankingRepository.kt index 6da1896..99af25d 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/BankingRepository.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/BankingRepository.kt @@ -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 suspend fun persistBank(bank: BankAccess): BankAccessEntity diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/InMemoryBankingRepository.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/InMemoryBankingRepository.kt index bd8d9e5..e174e8d 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/InMemoryBankingRepository.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/InMemoryBankingRepository.kt @@ -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 = emptyList(), - transactions: Collection = emptyList() + transactions: Collection = 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 = banks.toList() override suspend fun persistBank(bank: BankAccess): BankAccessEntity { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt index c7d83e6..3a86cd9 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt @@ -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 { val bankAccounts = getAllBankAccounts().groupBy { it.bankId } val tanMethods = getAllTanMethods().groupBy { it.bankId }.mapValues { it.value.toMutableList() } diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppAuthenticationMethod.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppAuthenticationMethod.kt new file mode 100644 index 0000000..f104610 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppAuthenticationMethod.kt @@ -0,0 +1,7 @@ +package net.codinux.banking.ui.model.settings + +enum class AppAuthenticationMethod { + None, + Password, + Biometric +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppSettings.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppSettings.kt new file mode 100644 index 0000000..f581aed --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/settings/AppSettings.kt @@ -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" +} \ No newline at end of file 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 5221fa7..6f70167 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 @@ -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() diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt index 4e788bf..4c15b5d 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt @@ -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>(emptyList()) val transactions = MutableStateFlow>(emptyList()) diff --git a/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/Settings.sq b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/Settings.sq new file mode 100644 index 0000000..b618a6e --- /dev/null +++ b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/Settings.sq @@ -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, ?, ?, ?, ?);