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) }