From e60dbf6add88309c2c33a9970768dd1c1abbed1d Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:54:31 +0300 Subject: [PATCH] multiplatform: reduce variables that is controlling running chat (#3021) * multiplatform: reduce variables that is controlling running chat * removed second variable --- .../common/views/database/DatabaseView.kt | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/database/DatabaseView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/database/DatabaseView.kt index 4cce028872..9ecd7dae31 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/database/DatabaseView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/database/DatabaseView.kt @@ -39,7 +39,6 @@ fun DatabaseView( showSettingsModal: (@Composable (ChatModel) -> Unit) -> (() -> Unit) ) { val progressIndicator = remember { mutableStateOf(false) } - val runChat = remember { m.chatRunning } val prefs = m.controller.appPrefs val useKeychain = remember { mutableStateOf(prefs.storeDBPassphrase.get()) } val chatArchiveName = remember { mutableStateOf(prefs.chatArchiveName.get()) } @@ -60,16 +59,13 @@ fun DatabaseView( importArchiveAlert(m, to, appFilesCountAndSize, progressIndicator) } } - LaunchedEffect(m.chatRunning) { - runChat.value = m.chatRunning.value ?: true - } val chatItemTTL = remember { mutableStateOf(m.chatItemTTL.value) } Box( Modifier.fillMaxSize(), ) { DatabaseLayout( progressIndicator.value, - runChat.value != false, + remember { m.chatRunning }.value != false, m.chatDbChanged.value, useKeychain.value, m.chatDbEncrypted.value, @@ -84,8 +80,8 @@ fun DatabaseView( chatItemTTL, m.currentUser.value, m.users, - startChat = { startChat(m, runChat, chatLastStart, m.chatDbChanged) }, - stopChatAlert = { stopChatAlert(m, runChat) }, + startChat = { startChat(m, chatLastStart, m.chatDbChanged) }, + stopChatAlert = { stopChatAlert(m) }, exportArchive = { exportArchive(m, progressIndicator, chatArchiveName, chatArchiveTime, chatArchiveFile, saveArchiveLauncher) }, deleteChatAlert = { deleteChatAlert(m, progressIndicator) }, deleteAppFilesAndMedia = { deleteFilesAndMediaAlert(appFilesCountAndSize) }, @@ -339,7 +335,7 @@ fun chatArchiveTitle(chatArchiveTime: Instant, chatLastStart: Instant): String { return stringResource(if (chatArchiveTime < chatLastStart) MR.strings.old_database_archive else MR.strings.new_database_archive) } -private fun startChat(m: ChatModel, runChat: MutableState, chatLastStart: MutableState, chatDbChanged: MutableState) { +private fun startChat(m: ChatModel, chatLastStart: MutableState, chatDbChanged: MutableState) { withApi { try { if (chatDbChanged.value) { @@ -356,7 +352,6 @@ private fun startChat(m: ChatModel, runChat: MutableState, chatLastSta return@withApi } else { m.controller.apiStartChat() - runChat.value = true m.chatRunning.value = true } val ts = Clock.System.now() @@ -364,19 +359,19 @@ private fun startChat(m: ChatModel, runChat: MutableState, chatLastSta chatLastStart.value = ts platform.androidChatStartedAfterBeingOff() } catch (e: Error) { - runChat.value = false + m.chatRunning.value = false AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error_starting_chat), e.toString()) } } } -private fun stopChatAlert(m: ChatModel, runChat: MutableState) { +private fun stopChatAlert(m: ChatModel) { AlertManager.shared.showAlertDialog( title = generalGetString(MR.strings.stop_chat_question), text = generalGetString(MR.strings.stop_chat_to_export_import_or_delete_chat_database), confirmText = generalGetString(MR.strings.stop_chat_confirmation), - onConfirm = { authStopChat(m, runChat) }, - onDismiss = { runChat.value = true } + onConfirm = { authStopChat(m) }, + onDismiss = { m.chatRunning.value = true } ) } @@ -387,7 +382,7 @@ private fun exportProhibitedAlert() { ) } -private fun authStopChat(m: ChatModel, runChat: MutableState) { +private fun authStopChat(m: ChatModel) { if (m.controller.appPrefs.performLA.get()) { authenticate( generalGetString(MR.strings.auth_stop_chat), @@ -395,30 +390,29 @@ private fun authStopChat(m: ChatModel, runChat: MutableState) { completed = { laResult -> when (laResult) { LAResult.Success, is LAResult.Unavailable -> { - stopChat(m, runChat) + stopChat(m) } is LAResult.Error -> { - runChat.value = true + m.chatRunning.value = true } is LAResult.Failed -> { - runChat.value = true + m.chatRunning.value = true } } } ) } else { - stopChat(m, runChat) + stopChat(m) } } -private fun stopChat(m: ChatModel, runChat: MutableState) { +private fun stopChat(m: ChatModel) { withApi { try { - runChat.value = false stopChatAsync(m) platform.androidChatStopped() } catch (e: Error) { - runChat.value = true + m.chatRunning.value = true AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error_stopping_chat), e.toString()) } }