From 8085e5b85cd0807e7970e72a808442c7992717b3 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Mon, 3 Apr 2023 22:11:27 +0300 Subject: [PATCH] android: change active user after chat started (#2141) --- .../java/chat/simplex/app/MainActivity.kt | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt b/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt index 1748581f93..0c147eba16 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt @@ -402,7 +402,8 @@ fun processNotificationIntent(intent: Intent?, chatModel: ChatModel) { Log.d(TAG, "processNotificationIntent: OpenChatAction $chatId") if (chatId != null) { withBGApi { - if (userId != null && userId != chatModel.currentUser.value?.userId) { + awaitChatStartedIfNeeded(chatModel) + if (userId != null && userId != chatModel.currentUser.value?.userId && chatModel.currentUser.value != null) { chatModel.controller.changeActiveUser(userId, null) } val cInfo = chatModel.getChat(chatId)?.chatInfo @@ -414,7 +415,8 @@ fun processNotificationIntent(intent: Intent?, chatModel: ChatModel) { NtfManager.ShowChatsAction -> { Log.d(TAG, "processNotificationIntent: ShowChatsAction") withBGApi { - if (userId != null && userId != chatModel.currentUser.value?.userId) { + awaitChatStartedIfNeeded(chatModel) + if (userId != null && userId != chatModel.currentUser.value?.userId && chatModel.currentUser.value != null) { chatModel.controller.changeActiveUser(userId, null) } chatModel.chatId.value = null @@ -505,6 +507,20 @@ fun connectIfOpenedViaUri(uri: Uri, chatModel: ChatModel) { } } } + +suspend fun awaitChatStartedIfNeeded(chatModel: ChatModel, timeout: Long = 30_000) { + // Still decrypting database + if (chatModel.chatRunning.value == null) { + val step = 50L + for (i in 0..(timeout / step)) { + if (chatModel.chatRunning.value == true || chatModel.onboardingStage.value == OnboardingStage.Step1_SimpleXInfo) { + break + } + delay(step) + } + } +} + //fun testJson() { // val str: String = """ // """.trimIndent()