From 677eedfa8dd9b3c38e0b69e1151823a01887da97 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Wed, 10 Jan 2024 22:06:23 +0700 Subject: [PATCH] android: don't show alert when not needed after changing user concurrently --- .../android/src/main/java/chat/simplex/app/SimplexApp.kt | 2 +- .../kotlin/chat/simplex/common/model/SimpleXAPI.kt | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt index e058663539..7e26b27cf2 100644 --- a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt +++ b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt @@ -80,7 +80,7 @@ class SimplexApp: Application(), LifecycleEventObserver { updatingChatsMutex.withLock { kotlin.runCatching { val currentUserId = chatModel.currentUser.value?.userId - val chats = ArrayList(chatController.apiGetChats(chatModel.remoteHostId())) + val chats = ArrayList(chatController.apiGetChatsWithoutAlert(chatModel.remoteHostId()) ?: return@runCatching) /** Active user can be changed in background while [ChatController.apiGetChats] is executing */ if (chatModel.currentUser.value?.userId == currentUserId) { val currentChatId = chatModel.chatId.value diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 110f12273f..d62abf9142 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -654,6 +654,15 @@ object ChatController { return emptyList() } + // It's useful for situations when active user can be changed concurrently and there is no need to show alert in case of failure + suspend fun apiGetChatsWithoutAlert(rh: Long?): List? { + val userId = kotlin.runCatching { currentUserId("apiGetChats") }.getOrElse { return null } + val r = sendCmd(rh, CC.ApiGetChats(userId)) + if (r is CR.ApiChats) return if (rh == null) r.chats else r.chats.map { it.copy(remoteHostId = rh) } + Log.e(TAG, "failed getting the list of chats: ${r.responseType} ${r.details}") + return null + } + suspend fun apiGetChat(rh: Long?, type: ChatType, id: Long, pagination: ChatPagination = ChatPagination.Last(ChatPagination.INITIAL_COUNT), search: String = ""): Chat? { val r = sendCmd(rh, CC.ApiGetChat(type, id, pagination, search)) if (r is CR.ApiChat) return if (rh == null) r.chat else r.chat.copy(remoteHostId = rh)