only notifying message notifications when they're new

This commit is contained in:
Adam Brown 2022-05-16 22:43:55 +01:00
parent 8716500d99
commit ffa8fad2b0
3 changed files with 12 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import app.dapk.st.imageloader.IconLoader
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.messenger.MessengerActivity
@ -22,12 +23,12 @@ class NotificationFactory(
private val intentFactory: IntentFactory,
) {
suspend fun createNotifications(events: Map<RoomOverview, List<RoomEvent>>, onlyContainsRemovals: Boolean): Notifications {
suspend fun createNotifications(events: Map<RoomOverview, List<RoomEvent>>, onlyContainsRemovals: Boolean, roomsWithNewEvents: Set<RoomId>): Notifications {
val notifications = events.map { (roomOverview, events) ->
val messageEvents = events.filterIsInstance<RoomEvent.Message>()
when (messageEvents.isEmpty()) {
true -> NotificationDelegate.DismissRoom(roomOverview.roomId)
false -> createNotification(messageEvents, roomOverview)
false -> createNotification(messageEvents, roomOverview, roomsWithNewEvents)
}
}
@ -110,7 +111,7 @@ class NotificationFactory(
return messageStyle
}
private suspend fun createNotification(events: List<RoomEvent.Message>, roomOverview: RoomOverview): NotificationDelegate {
private suspend fun createNotification(events: List<RoomEvent.Message>, roomOverview: RoomOverview, roomsWithNewEvents: Set<RoomId>): NotificationDelegate {
val sortedEvents = events.sortedBy { it.utcTimestamp }
val messageStyle = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@ -136,7 +137,7 @@ class NotificationFactory(
.setWhen(sortedEvents.last().utcTimestamp)
.setShowWhen(true)
.setGroup(GROUP_ID)
.setOnlyAlertOnce(roomOverview.isGroup)
.setOnlyAlertOnce(roomOverview.isGroup || !roomsWithNewEvents.contains(roomOverview.roomId))
.setContentIntent(openRoomIntent)
.setStyle(messageStyle)
.setCategory(Notification.CATEGORY_MESSAGE)

View File

@ -19,9 +19,9 @@ class NotificationRenderer(
private val notificationFactory: NotificationFactory,
) {
suspend fun render(result: Map<RoomOverview, List<RoomEvent>>, removedRooms: Set<RoomId>, onlyContainsRemovals: Boolean) {
suspend fun render(result: Map<RoomOverview, List<RoomEvent>>, removedRooms: Set<RoomId>, onlyContainsRemovals: Boolean, roomsWithNewEvents: Set<RoomId>) {
removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) }
val notifications = notificationFactory.createNotifications(result, onlyContainsRemovals)
val notifications = notificationFactory.createNotifications(result, onlyContainsRemovals, roomsWithNewEvents)
withContext(Dispatchers.Main) {
notifications.summaryNotification.ifNull {

View File

@ -4,7 +4,6 @@ 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.*
@ -31,12 +30,16 @@ class NotificationsUseCase(
val asRooms = changes.keys
val removedRooms = inferredCurrentNotifications.keys - asRooms
val roomsWithNewEvents = changes.filter {
inferredCurrentNotifications[it.key]?.map { it.eventId } != it.value.map { it.eventId }
}.keys
val onlyContainsRemovals =
inferredCurrentNotifications.filterKeys { !removedRooms.contains(it) } == changes.filterKeys { !removedRooms.contains(it) }
inferredCurrentNotifications.clear()
inferredCurrentNotifications.putAll(changes)
notificationRenderer.render(result, removedRooms, onlyContainsRemovals)
notificationRenderer.render(result, removedRooms, onlyContainsRemovals, roomsWithNewEvents)
}
.collect()
}