diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationFactory.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationFactory.kt index 515a285..743d863 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationFactory.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationFactory.kt @@ -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>, onlyContainsRemovals: Boolean): Notifications { + suspend fun createNotifications(events: Map>, onlyContainsRemovals: Boolean, roomsWithNewEvents: Set): Notifications { val notifications = events.map { (roomOverview, events) -> val messageEvents = events.filterIsInstance() 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, roomOverview: RoomOverview): NotificationDelegate { + private suspend fun createNotification(events: List, roomOverview: RoomOverview, roomsWithNewEvents: Set): 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) diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt index fd71c44..d22bc71 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationRenderer.kt @@ -19,9 +19,9 @@ class NotificationRenderer( private val notificationFactory: NotificationFactory, ) { - suspend fun render(result: Map>, removedRooms: Set, onlyContainsRemovals: Boolean) { + suspend fun render(result: Map>, removedRooms: Set, onlyContainsRemovals: Boolean, roomsWithNewEvents: Set) { 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 { diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt index 0ed44e2..60f1586 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsUseCase.kt @@ -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() }