diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/StateHandler.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/StateHandler.kt index 8b9527c..cdb51f4 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/StateHandler.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/StateHandler.kt @@ -3,15 +3,23 @@ package net.codinux.banking.ui.composables import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import net.codinux.banking.ui.dialogs.ApplicationErrorDialog import net.codinux.banking.ui.dialogs.BankingClientErrorDialog import net.codinux.banking.ui.state.UiState @Composable fun StateHandler(uiState: UiState) { + val applicationError by uiState.applicationErrorOccurred.collectAsState() val bankingClientError by uiState.bankingClientErrorOccurred.collectAsState() - bankingClientError?.let { - BankingClientErrorDialog(it) { + applicationError?.let { error -> + ApplicationErrorDialog(error) { + uiState.applicationErrorOccurred.value = null + } + } + + bankingClientError?.let { error -> + BankingClientErrorDialog(error) { uiState.bankingClientErrorOccurred.value = null } } diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/Internationalization.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/Internationalization.kt new file mode 100644 index 0000000..33cae92 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/Internationalization.kt @@ -0,0 +1,9 @@ +package net.codinux.banking.ui.config + +object Internationalization { + + const val ErrorAddAccount = "Konto konnte nicht hinzugefügt werden" + + const val ErrorUpdateAccountTransactions = "Umsätze konnten nicht aktualisiert werden" + +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/ApplicationErrorDialog.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/ApplicationErrorDialog.kt new file mode 100644 index 0000000..5130b65 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/ApplicationErrorDialog.kt @@ -0,0 +1,18 @@ +package net.codinux.banking.ui.dialogs + +import androidx.compose.runtime.Composable +import net.codinux.banking.ui.config.Internationalization +import net.codinux.banking.ui.model.error.ApplicationError +import net.codinux.banking.ui.model.error.ErroneousAction + +@Composable +fun ApplicationErrorDialog(error: ApplicationError, onDismiss: (() -> Unit)? = null) { + val title = when (error.erroneousAction) { + ErroneousAction.AddAccount -> Internationalization.ErrorAddAccount + ErroneousAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions + } + + // add exception stacktrace? + + ErrorDialog(error.errorMessage, title, onDismiss = onDismiss) +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/BankingClientErrorDialog.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/BankingClientErrorDialog.kt index db5d72d..841eef9 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/BankingClientErrorDialog.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/BankingClientErrorDialog.kt @@ -1,15 +1,16 @@ package net.codinux.banking.ui.dialogs import androidx.compose.runtime.Composable -import net.codinux.banking.ui.model.BankingClientAction -import net.codinux.banking.ui.model.BankingClientError +import net.codinux.banking.ui.config.Internationalization +import net.codinux.banking.ui.model.error.BankingClientAction +import net.codinux.banking.ui.model.error.BankingClientError import net.codinux.banking.ui.model.Config @Composable fun BankingClientErrorDialog(error: BankingClientError, onDismiss: (() -> Unit)? = null) { val title = when (error.erroneousAction) { - BankingClientAction.AddAccount -> "Konto konnte nicht hinzugefügt werden" - BankingClientAction.UpdateAccountTransactions -> "Umsätze konnten nicht aktualisiert werden" + BankingClientAction.AddAccount -> Internationalization.ErrorAddAccount + BankingClientAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions } val text = if (error.error.internalError != null) { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ApplicationError.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ApplicationError.kt new file mode 100644 index 0000000..92756ea --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ApplicationError.kt @@ -0,0 +1,9 @@ +package net.codinux.banking.ui.model.error + +data class ApplicationError( + val erroneousAction: ErroneousAction, + val errorMessage: String, + val exception: Throwable? = null +) { + override fun toString() = "$erroneousAction $errorMessage" +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientAction.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientAction.kt similarity index 64% rename from composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientAction.kt rename to composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientAction.kt index 8ff5d95..bf62809 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientAction.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientAction.kt @@ -1,4 +1,4 @@ -package net.codinux.banking.ui.model +package net.codinux.banking.ui.model.error enum class BankingClientAction { AddAccount, diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientError.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientError.kt similarity index 83% rename from composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientError.kt rename to composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientError.kt index c0dc745..aa32a28 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/BankingClientError.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/BankingClientError.kt @@ -1,4 +1,4 @@ -package net.codinux.banking.ui.model +package net.codinux.banking.ui.model.error import net.codinux.banking.client.model.response.Error diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ErroneousAction.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ErroneousAction.kt new file mode 100644 index 0000000..9ca90ee --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/model/error/ErroneousAction.kt @@ -0,0 +1,6 @@ +package net.codinux.banking.ui.model.error + +enum class ErroneousAction { + AddAccount, + UpdateAccountTransactions +} \ 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 05b1939..6c5003c 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 @@ -12,8 +12,9 @@ import net.codinux.banking.client.model.response.ResponseType import net.codinux.banking.fints.config.FinTsClientConfiguration import net.codinux.banking.fints.config.FinTsClientOptions import net.codinux.banking.ui.model.BankInfo -import net.codinux.banking.ui.model.BankingClientAction -import net.codinux.banking.ui.model.BankingClientError +import net.codinux.banking.ui.model.error.BankingClientAction +import net.codinux.banking.ui.model.error.BankingClientError +import net.codinux.banking.ui.model.error.ErroneousAction import net.codinux.banking.ui.state.UiState import net.codinux.csv.reader.CsvReader import net.codinux.log.Log @@ -64,7 +65,7 @@ class BankingService( } catch (e: Throwable) { log.error(e) { "Could not add account for ${bank.name} $loginName" } - // TODO: handle internal error + uiState.applicationErrorOccurred(ErroneousAction.AddAccount, e) return false } 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 c6baf3c..abda1e7 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 @@ -3,12 +3,26 @@ package net.codinux.banking.ui.state import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableStateFlow import net.codinux.banking.client.model.AccountTransaction -import net.codinux.banking.ui.model.BankingClientError +import net.codinux.banking.ui.model.error.ApplicationError +import net.codinux.banking.ui.model.error.BankingClientError +import net.codinux.banking.ui.model.error.ErroneousAction class UiState : ViewModel() { val transactions = MutableStateFlow>(emptyList()) + val applicationErrorOccurred = MutableStateFlow(null) + val bankingClientErrorOccurred = MutableStateFlow(null) + + fun applicationErrorOccurred(erroneousAction: ErroneousAction, exception: Throwable, errorMessage: String? = null) { + val message = errorMessage + ?: exception.message ?: exception::class.simpleName // TODO: find a better way to get error message from exception + + if (message != null) { + applicationErrorOccurred.value = ApplicationError(erroneousAction, message, exception) + } + } + } \ No newline at end of file