From f2be4b8b95ca5c1aa1c63d493e7827fdd45beb8e Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 8 May 2022 12:23:13 +0100 Subject: [PATCH] forcing the notification rendering to occur on the main thread - hopefully reduce the amount of individual notification popups when a collection of messages are received --- .../st/notifications/NotificationRenderer.kt | 36 ++++++++++--------- .../st/notifications/NotificationsUseCase.kt | 9 ++--- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt index 45a5020..a497ed2 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt @@ -8,6 +8,8 @@ import app.dapk.st.core.log import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.sync.RoomEvent import app.dapk.st.matrix.sync.RoomOverview +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext private const val SUMMARY_NOTIFICATION_ID = 101 private const val MESSAGE_NOTIFICATION_ID = 100 @@ -21,27 +23,29 @@ class NotificationRenderer( removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) } val notifications = notificationFactory.createNotifications(result) - notifications.summaryNotification.ifNull { - log(AppLogTag.NOTIFICATION, "cancelling summary") - notificationManager.cancel(SUMMARY_NOTIFICATION_ID) - } + withContext(Dispatchers.Main) { + notifications.summaryNotification.ifNull { + log(AppLogTag.NOTIFICATION, "cancelling summary") + notificationManager.cancel(SUMMARY_NOTIFICATION_ID) + } - notifications.delegates.forEach { - when (it) { - is NotificationDelegate.DismissRoom -> notificationManager.cancel(it.roomId.value, MESSAGE_NOTIFICATION_ID) - is NotificationDelegate.Room -> { - if (!onlyContainsRemovals) { - log(AppLogTag.NOTIFICATION, "notifying ${it.roomId.value}") - notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification) + notifications.delegates.forEach { + when (it) { + is NotificationDelegate.DismissRoom -> notificationManager.cancel(it.roomId.value, MESSAGE_NOTIFICATION_ID) + is NotificationDelegate.Room -> { + if (!onlyContainsRemovals) { + log(AppLogTag.NOTIFICATION, "notifying ${it.roomId.value}") + notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification) + } } } } - } - notifications.summaryNotification?.let { - if (!onlyContainsRemovals) { - log(AppLogTag.NOTIFICATION, "notifying summary") - notificationManager.notify(SUMMARY_NOTIFICATION_ID, it) + notifications.summaryNotification?.let { + if (!onlyContainsRemovals) { + log(AppLogTag.NOTIFICATION, "notifying summary") + notificationManager.notify(SUMMARY_NOTIFICATION_ID, it) + } } } } diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt index 87cd027..0ed44e2 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt @@ -4,10 +4,9 @@ import app.dapk.st.core.AppLogTag.NOTIFICATION import app.dapk.st.core.log import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.sync.RoomEvent +import app.dapk.st.matrix.sync.RoomOverview import app.dapk.st.matrix.sync.RoomStore -import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.drop -import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.* class NotificationsUseCase( private val roomStore: RoomStore, @@ -23,7 +22,7 @@ class NotificationsUseCase( suspend fun listenForNotificationChanges() { roomStore.observeUnread() - .drop(1) + .skipFirst() .onEach { result -> log(NOTIFICATION, "unread changed - render notifications") @@ -41,4 +40,6 @@ class NotificationsUseCase( } .collect() } + + private fun Flow.skipFirst() = drop(1) }