From 2b3eebb7a299367a244d6722be53526b23f9f167 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Sat, 27 Jan 2024 06:08:48 +0700 Subject: [PATCH] android, desktop: show different text when database migrates (#3762) * android, desktop: show different text when database migrates * one more check * refactor --------- Co-authored-by: Evgeny Poberezkin --- .../kotlin/chat/simplex/common/App.kt | 1 + .../chat/simplex/common/model/ChatModel.kt | 1 + .../chat/simplex/common/platform/Core.kt | 18 ++++++++++++++++-- .../common/views/helpers/DefaultProgressBar.kt | 3 ++- .../commonMain/resources/MR/base/strings.xml | 1 + 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/App.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/App.kt index bfff3bf9fb..57959af4c6 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/App.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/App.kt @@ -108,6 +108,7 @@ fun MainScreen() { val localUserCreated = chatModel.localUserCreated.value var showInitializationView by remember { mutableStateOf(false) } when { + chatModel.dbMigrationInProgress.value -> DefaultProgressView(stringResource(MR.strings.database_migration_in_progress)) chatModel.chatDbStatus.value == null && showInitializationView -> DefaultProgressView(stringResource(MR.strings.opening_database)) showChatDatabaseError -> { // Prevent showing keyboard on Android when: passcode enabled and database password not saved diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index 1643ddcee5..54d4a95ffb 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -48,6 +48,7 @@ object ChatModel { val chatDbEncrypted = mutableStateOf(false) val chatDbStatus = mutableStateOf(null) val ctrlInitInProgress = mutableStateOf(false) + val dbMigrationInProgress = mutableStateOf(false) val chats = mutableStateListOf() // map of connections network statuses, key is agent connection id val networkStatuses = mutableStateMapOf() diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt index 96af131a21..63fcb90bbe 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt @@ -59,10 +59,23 @@ suspend fun initChatController(useKey: String? = null, confirmMigrations: Migrat chatModel.ctrlInitInProgress.value = true val dbKey = useKey ?: DatabaseUtils.useDatabaseKey() val confirm = confirmMigrations ?: if (appPreferences.developerTools.get() && appPreferences.confirmDBUpgrades.get()) MigrationConfirmation.Error else MigrationConfirmation.YesUp - val migrated: Array = chatMigrateInit(dbAbsolutePrefixPath, dbKey, confirm.value) - val res: DBMigrationResult = kotlin.runCatching { + var migrated: Array = chatMigrateInit(dbAbsolutePrefixPath, dbKey, MigrationConfirmation.Error.value) + var res: DBMigrationResult = runCatching { json.decodeFromString(migrated[0] as String) }.getOrElse { DBMigrationResult.Unknown(migrated[0] as String) } + val rerunMigration = res is DBMigrationResult.ErrorMigration && when (res.migrationError) { + // we don't allow to run down migrations without confirmation in UI, so currently it won't be YesUpDown + is MigrationError.Upgrade -> confirm == MigrationConfirmation.YesUp || confirm == MigrationConfirmation.YesUpDown + is MigrationError.Downgrade -> confirm == MigrationConfirmation.YesUpDown + is MigrationError.Error -> false + } + if (rerunMigration) { + chatModel.dbMigrationInProgress.value = true + migrated = chatMigrateInit(dbAbsolutePrefixPath, dbKey, confirm.value) + res = runCatching { + json.decodeFromString(migrated[0] as String) + }.getOrElse { DBMigrationResult.Unknown(migrated[0] as String) } + } val ctrl = if (res is DBMigrationResult.OK) { migrated[1] as Long } else null @@ -120,6 +133,7 @@ suspend fun initChatController(useKey: String? = null, confirmMigrations: Migrat } } finally { chatModel.ctrlInitInProgress.value = false + chatModel.dbMigrationInProgress.value = false } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/DefaultProgressBar.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/DefaultProgressBar.kt index ec2500ab2e..104a01150f 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/DefaultProgressBar.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/DefaultProgressBar.kt @@ -5,6 +5,7 @@ import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import chat.simplex.common.ui.theme.DEFAULT_PADDING @@ -20,7 +21,7 @@ fun DefaultProgressView(description: String?) { strokeWidth = 2.5.dp ) if (description != null) { - Text(description) + Text(description, textAlign = TextAlign.Center) } } } diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml index 6fa106120c..a511e2e13d 100644 --- a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml +++ b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml @@ -16,6 +16,7 @@ Opening database… + Database migration is in progress.\nIt may take a few minutes. Invalid file path You shared an invalid file path. Report the issue to the app developers. View crashed