From d549b96e7bcd36ea4593e64b26fe05151248925a Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 20 Sep 2024 02:46:37 +0200 Subject: [PATCH] Ensuring BankingRepository / SqliteDriver gets created only once and catching errors --- .../commonMain/kotlin/net/codinux/banking/ui/App.kt | 11 +++++++++-- .../codinux/banking/ui/dialogs/AddAccountDialog.kt | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt index c99509d..aee372b 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/App.kt @@ -14,6 +14,7 @@ import net.codinux.banking.ui.config.DI import net.codinux.banking.ui.model.settings.AppAuthenticationMethod import net.codinux.banking.ui.screens.LoginScreen import net.codinux.banking.ui.screens.MainScreen +import net.codinux.log.Log import net.codinux.log.LoggerFactory import org.jetbrains.compose.ui.tooling.preview.Preview @@ -26,8 +27,6 @@ private val typography = Typography( fun App(repository: BankingRepository? = null) { LoggerFactory.defaultLoggerName = "net.codinux.banking.ui.Bankmeister" - DI.setRepository(repository ?: SqliteBankingRepository()) - val colors = MaterialTheme.colors.copy(primary = Colors.Primary, primaryVariant = Colors.PrimaryDark, onPrimary = Color.White, secondary = Colors.Accent, secondaryVariant = Colors.Accent, onSecondary = Color.White) @@ -40,6 +39,14 @@ fun App(repository: BankingRepository? = null) { val coroutineScope = rememberCoroutineScope() + try { + if (isInitialized == false) { + DI.setRepository(repository ?: SqliteBankingRepository()) + } + } catch (e: Throwable) { + Log.error(e) { "Could not set repository" } + } + MaterialTheme(colors = colors, typography = typography) { if (isLoggedIn == false) { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt index 238a72b..df1c245 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt @@ -23,6 +23,7 @@ import net.codinux.banking.ui.config.DI import net.codinux.banking.ui.forms.* import net.codinux.banking.ui.forms.OutlinedTextField import net.codinux.banking.ui.model.BankInfo +import net.codinux.log.Log private val bankingService = DI.bankingService @@ -69,7 +70,12 @@ fun AddAccountDialog( isAddingAccount = true addAccountJob = coroutineScope.launch(Dispatchers.IOorDefault) { - val successful = DI.bankingService.addAccount(bank, loginName, password, retrieveAllTransactions) + val successful = try { + DI.bankingService.addAccount(bank, loginName, password, retrieveAllTransactions) + } catch (e: Throwable) { + Log.error(e) { "Could not add account for $bank" } + false + } addAccountJob = null