only display message notifications if we've received a new one, not when marking old ones as read

This commit is contained in:
Adam Brown 2022-03-17 21:29:45 +00:00
parent 509667f595
commit 271727dda7
3 changed files with 18 additions and 8 deletions

View File

@ -30,4 +30,6 @@ WHERE event_id = ?;
selectAllUnread:
SELECT dbRoomEvent.blob, dbRoomEvent.room_id
FROM dbUnreadEvent
INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id;
INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id
ORDER BY dbRoomEvent.timestamp_utc DESC
LIMIT 100;

View File

@ -15,7 +15,7 @@ class NotificationRenderer(
private val notificationFactory: NotificationFactory,
) {
suspend fun render(result: Map<RoomOverview, List<RoomEvent>>, removedRooms: Set<RoomId>) {
suspend fun render(result: Map<RoomOverview, List<RoomEvent>>, removedRooms: Set<RoomId>, onlyContainsRemovals: Boolean) {
removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) }
val notifications = notificationFactory.createNotifications(result)
@ -26,7 +26,11 @@ class NotificationRenderer(
notifications.delegates.forEach {
when (it) {
is NotificationDelegate.DismissRoom -> notificationManager.cancel(it.roomId.value, MESSAGE_NOTIFICATION_ID)
is NotificationDelegate.Room -> notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification)
is NotificationDelegate.Room -> {
if (!onlyContainsRemovals) {
notificationManager.notify(it.roomId.value, MESSAGE_NOTIFICATION_ID, it.notification)
}
}
}
}

View File

@ -3,6 +3,7 @@ package app.dapk.st.notifications
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.RoomStore
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.drop
@ -14,7 +15,7 @@ class NotificationsUseCase(
notificationChannels: NotificationChannels,
) {
private val inferredCurrentNotifications = mutableSetOf<RoomId>()
private val inferredCurrentNotifications = mutableMapOf<RoomId, List<RoomEvent>>()
init {
notificationChannels.initChannels()
@ -26,13 +27,16 @@ class NotificationsUseCase(
.onEach { result ->
log(NOTIFICATION, "unread changed - render notifications")
val asRooms = result.keys.map { it.roomId }.toSet()
val removedRooms = inferredCurrentNotifications - asRooms
val changes = result.mapKeys { it.key.roomId }
val asRooms = changes.keys
val removedRooms = inferredCurrentNotifications.keys - asRooms
val onlyContainsRemovals = inferredCurrentNotifications.filterKeys { !removedRooms.contains(it) } == changes.filterKeys { !removedRooms.contains(it) }
inferredCurrentNotifications.clear()
inferredCurrentNotifications.addAll(asRooms)
inferredCurrentNotifications.putAll(changes)
notificationRenderer.render(result, removedRooms)
notificationRenderer.render(result, removedRooms, onlyContainsRemovals)
}
.collect()
}