From da1cad96fced205180b487e37f5c89643d0df22f Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 28 May 2024 14:04:23 +0700 Subject: [PATCH] android: fix showing empty notification (#4237) * android: fix showing empty notification * fix desktop concurrent notifications --- .../simplex/app/model/NtfManager.android.kt | 2 +- .../common/model/NtfManager.desktop.kt | 37 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/apps/multiplatform/android/src/main/java/chat/simplex/app/model/NtfManager.android.kt b/apps/multiplatform/android/src/main/java/chat/simplex/app/model/NtfManager.android.kt index f83933b2e0..69ad8defbf 100644 --- a/apps/multiplatform/android/src/main/java/chat/simplex/app/model/NtfManager.android.kt +++ b/apps/multiplatform/android/src/main/java/chat/simplex/app/model/NtfManager.android.kt @@ -77,7 +77,7 @@ object NtfManager { val msgNtfs = manager.activeNotifications.filter { ntf -> ntf.notification.channelId == MessageChannel } - if (msgNtfs.count() == 1) { + if (msgNtfs.size <= 1) { // Have a group notification with no children so cancel it manager.cancel(0) } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/model/NtfManager.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/model/NtfManager.desktop.kt index 3fab849361..3913c0dc9b 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/model/NtfManager.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/model/NtfManager.desktop.kt @@ -8,6 +8,8 @@ import chat.simplex.common.views.call.RcvCallInvitation import chat.simplex.common.views.helpers.* import chat.simplex.res.MR import com.sshtools.twoslices.* +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import java.awt.* import java.awt.TrayIcon.MessageType import java.io.File @@ -15,6 +17,7 @@ import javax.imageio.ImageIO object NtfManager { private val prevNtfs = arrayListOf>() + private val prevNtfsMutex: Mutex = Mutex() fun notifyCallInvitation(invitation: RcvCallInvitation): Boolean { if (simplexWindowState.windowFocused.value) return false @@ -55,21 +58,29 @@ object NtfManager { fun hasNotificationsForChat(chatId: ChatId) = false//prevNtfs.any { it.first == chatId } fun cancelNotificationsForChat(chatId: ChatId) { - val ntf = prevNtfs.firstOrNull { it.first == chatId } - if (ntf != null) { - prevNtfs.remove(ntf) - /*try { - ntf.second.close() - } catch (e: Exception) { - // Can be java.lang.UnsupportedOperationException, for example. May do nothing - println("Failed to close notification: ${e.stackTraceToString()}") - }*/ + withBGApi { + prevNtfsMutex.withLock { + val ntf = prevNtfs.firstOrNull { it.first == chatId } + if (ntf != null) { + prevNtfs.remove(ntf) + /*try { + ntf.second.close() + } catch (e: Exception) { + // Can be java.lang.UnsupportedOperationException, for example. May do nothing + println("Failed to close notification: ${e.stackTraceToString()}") + }*/ + } + } } } fun cancelAllNotifications() { // prevNtfs.forEach { try { it.second.close() } catch (e: Exception) { println("Failed to close notification: ${e.stackTraceToString()}") } } - prevNtfs.clear() + withBGApi { + prevNtfsMutex.withLock { + prevNtfs.clear() + } + } } fun displayNotification(user: UserLike, chatId: String, displayName: String, msgText: String, image: String?, actions: List Unit>>) { @@ -110,7 +121,11 @@ object NtfManager { builder.action(it.first, it.second) } try { - prevNtfs.add(chatId to builder.toast()) + withBGApi { + prevNtfsMutex.withLock { + prevNtfs.add(chatId to builder.toast()) + } + } } catch (e: Throwable) { Log.e(TAG, e.stackTraceToString()) if (e !is Exception) {