From 2002b6f397b8c33d7b628e15ed6a23598c537514 Mon Sep 17 00:00:00 2001 From: fanchao Date: Mon, 20 May 2024 16:11:43 +1000 Subject: [PATCH] Fixes SES-1931 debouncer crash --- .../ConversationNotificationDebouncer.kt | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ConversationNotificationDebouncer.kt b/app/src/main/java/org/thoughtcrime/securesms/database/ConversationNotificationDebouncer.kt index 84e1b9b20a9..56208141903 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ConversationNotificationDebouncer.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ConversationNotificationDebouncer.kt @@ -5,9 +5,9 @@ import android.content.Context import org.session.libsession.utilities.Debouncer import org.thoughtcrime.securesms.ApplicationContext -class ConversationNotificationDebouncer(private val context: Context) { +class ConversationNotificationDebouncer(private val context: ApplicationContext) { private val threadIDs = mutableSetOf() - private val handler = (context.applicationContext as ApplicationContext).conversationListNotificationHandler + private val handler = context.conversationListNotificationHandler private val debouncer = Debouncer(handler, 100) companion object { @@ -17,20 +17,28 @@ class ConversationNotificationDebouncer(private val context: Context) { @Synchronized fun get(context: Context): ConversationNotificationDebouncer { if (::shared.isInitialized) { return shared } - shared = ConversationNotificationDebouncer(context) + shared = ConversationNotificationDebouncer(context.applicationContext as ApplicationContext) return shared } } fun notify(threadID: Long) { - threadIDs.add(threadID) + synchronized(threadIDs) { + threadIDs.add(threadID) + } + debouncer.publish { publish() } } private fun publish() { - for (threadID in threadIDs.toList()) { + val toNotify = synchronized(threadIDs) { + val copy = threadIDs.toList() + threadIDs.clear() + copy + } + + for (threadID in toNotify) { context.contentResolver.notifyChange(DatabaseContentProviders.Conversation.getUriForThread(threadID), null) } - threadIDs.clear() } } \ No newline at end of file