Merge pull request #1492 from simophin/ses-1931-fix-debouncer

[SES-1931] - Fix debouncer crash
This commit is contained in:
Andrew 2024-05-29 11:38:29 +09:30 committed by GitHub
commit 410e298bca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,9 +5,9 @@ import android.content.Context
import org.session.libsession.utilities.Debouncer import org.session.libsession.utilities.Debouncer
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
class ConversationNotificationDebouncer(private val context: Context) { class ConversationNotificationDebouncer(private val context: ApplicationContext) {
private val threadIDs = mutableSetOf<Long>() private val threadIDs = mutableSetOf<Long>()
private val handler = (context.applicationContext as ApplicationContext).conversationListNotificationHandler private val handler = context.conversationListNotificationHandler
private val debouncer = Debouncer(handler, 100) private val debouncer = Debouncer(handler, 100)
companion object { companion object {
@ -17,20 +17,28 @@ class ConversationNotificationDebouncer(private val context: Context) {
@Synchronized @Synchronized
fun get(context: Context): ConversationNotificationDebouncer { fun get(context: Context): ConversationNotificationDebouncer {
if (::shared.isInitialized) { return shared } if (::shared.isInitialized) { return shared }
shared = ConversationNotificationDebouncer(context) shared = ConversationNotificationDebouncer(context.applicationContext as ApplicationContext)
return shared return shared
} }
} }
fun notify(threadID: Long) { fun notify(threadID: Long) {
threadIDs.add(threadID) synchronized(threadIDs) {
threadIDs.add(threadID)
}
debouncer.publish { publish() } debouncer.publish { publish() }
} }
private fun 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) context.contentResolver.notifyChange(DatabaseContentProviders.Conversation.getUriForThread(threadID), null)
} }
threadIDs.clear()
} }
} }