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,6 +23,7 @@ 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)
withContext(Dispatchers.Main) {
notifications.summaryNotification.ifNull { notifications.summaryNotification.ifNull {
log(AppLogTag.NOTIFICATION, "cancelling summary") log(AppLogTag.NOTIFICATION, "cancelling summary")
notificationManager.cancel(SUMMARY_NOTIFICATION_ID) notificationManager.cancel(SUMMARY_NOTIFICATION_ID)
@ -45,6 +48,7 @@ class NotificationRenderer(
} }
} }
} }
}
} }

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