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
This commit is contained in:
Adam Brown 2022-05-08 12:23:13 +01:00
parent cf35d382c6
commit f2be4b8b95
2 changed files with 25 additions and 20 deletions

View File

@ -8,6 +8,8 @@ import app.dapk.st.core.log
import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.sync.RoomEvent import app.dapk.st.matrix.sync.RoomEvent
import app.dapk.st.matrix.sync.RoomOverview 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 SUMMARY_NOTIFICATION_ID = 101
private const val MESSAGE_NOTIFICATION_ID = 100 private const val MESSAGE_NOTIFICATION_ID = 100
@ -21,27 +23,29 @@ class NotificationRenderer(
removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) } removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) }
val notifications = notificationFactory.createNotifications(result) val notifications = notificationFactory.createNotifications(result)
notifications.summaryNotification.ifNull { withContext(Dispatchers.Main) {
log(AppLogTag.NOTIFICATION, "cancelling summary") notifications.summaryNotification.ifNull {
notificationManager.cancel(SUMMARY_NOTIFICATION_ID) log(AppLogTag.NOTIFICATION, "cancelling summary")
} notificationManager.cancel(SUMMARY_NOTIFICATION_ID)
}
notifications.delegates.forEach { notifications.delegates.forEach {
when (it) { when (it) {
is NotificationDelegate.DismissRoom -> notificationManager.cancel(it.roomId.value, MESSAGE_NOTIFICATION_ID) is NotificationDelegate.DismissRoom -> notificationManager.cancel(it.roomId.value, MESSAGE_NOTIFICATION_ID)
is NotificationDelegate.Room -> { is NotificationDelegate.Room -> {
if (!onlyContainsRemovals) { if (!onlyContainsRemovals) {
log(AppLogTag.NOTIFICATION, "notifying ${it.roomId.value}") log(AppLogTag.NOTIFICATION, "notifying ${it.roomId.value}")
notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification) notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification)
}
} }
} }
} }
}
notifications.summaryNotification?.let { notifications.summaryNotification?.let {
if (!onlyContainsRemovals) { if (!onlyContainsRemovals) {
log(AppLogTag.NOTIFICATION, "notifying summary") log(AppLogTag.NOTIFICATION, "notifying summary")
notificationManager.notify(SUMMARY_NOTIFICATION_ID, it) notificationManager.notify(SUMMARY_NOTIFICATION_ID, it)
}
} }
} }
} }

View File

@ -4,10 +4,9 @@ import app.dapk.st.core.AppLogTag.NOTIFICATION
import app.dapk.st.core.log import app.dapk.st.core.log
import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.sync.RoomEvent import app.dapk.st.matrix.sync.RoomEvent
import app.dapk.st.matrix.sync.RoomOverview
import app.dapk.st.matrix.sync.RoomStore import app.dapk.st.matrix.sync.RoomStore
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.onEach
class NotificationsUseCase( class NotificationsUseCase(
private val roomStore: RoomStore, private val roomStore: RoomStore,
@ -23,7 +22,7 @@ class NotificationsUseCase(
suspend fun listenForNotificationChanges() { suspend fun listenForNotificationChanges() {
roomStore.observeUnread() roomStore.observeUnread()
.drop(1) .skipFirst()
.onEach { result -> .onEach { result ->
log(NOTIFICATION, "unread changed - render notifications") log(NOTIFICATION, "unread changed - render notifications")
@ -41,4 +40,6 @@ class NotificationsUseCase(
} }
.collect() .collect()
} }
private fun <T> Flow<T>.skipFirst() = drop(1)
} }