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

View File

@ -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 <T> Flow<T>.skipFirst() = drop(1)
}