From 995377343b8c67ffddd90f83ba570f80c03abac8 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 8 May 2022 12:36:53 +0100 Subject: [PATCH] adding summary notification title and ensuring that it updates without pinging when the counts change --- .../st/notifications/NotificationFactory.kt | 23 ++++++++++++++----- .../st/notifications/NotificationRenderer.kt | 10 ++++---- 2 files changed, 21 insertions(+), 12 deletions(-) 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 9c440c7..61594f4 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 @@ -7,8 +7,6 @@ import android.content.Context import android.content.Intent import android.os.Build import androidx.annotation.RequiresApi -import app.dapk.st.core.AppLogTag -import app.dapk.st.core.log import app.dapk.st.imageloader.IconLoader import app.dapk.st.matrix.sync.RoomEvent import app.dapk.st.matrix.sync.RoomOverview @@ -22,7 +20,7 @@ class NotificationFactory( private val context: Context, ) { - suspend fun createNotifications(events: Map>): Notifications { + suspend fun createNotifications(events: Map>, onlyContainsRemovals: Boolean): Notifications { val notifications = events.map { (roomOverview, events) -> val messageEvents = events.filterIsInstance() when (messageEvents.isEmpty()) { @@ -32,14 +30,14 @@ class NotificationFactory( } val summaryNotification = if (notifications.filterIsInstance().isNotEmpty()) { - createSummary(notifications) + createSummary(notifications, onlyContainsRemovals) } else { null } return Notifications(summaryNotification, notifications) } - private fun createSummary(notifications: List): Notification { + private fun createSummary(notifications: List, onlyContainsRemovals: Boolean): Notification { val summaryInboxStyle = Notification.InboxStyle().also { style -> notifications.forEach { when (it) { @@ -51,8 +49,13 @@ class NotificationFactory( } } + if (notifications.size > 1) { + summaryInboxStyle.setSummaryText("${notifications.countMessages()} messages from ${notifications.size} chats") + } + return builder() .setStyle(summaryInboxStyle) + .setOnlyAlertOnce(onlyContainsRemovals) .setSmallIcon(R.drawable.ic_notification_small_icon) .setCategory(Notification.CATEGORY_MESSAGE) .setGroupSummary(true) @@ -60,6 +63,13 @@ class NotificationFactory( .build() } + private fun List.countMessages() = this.sumOf { + when (it) { + is NotificationDelegate.DismissRoom -> 0 + is NotificationDelegate.Room -> it.messageCount + } + } + @RequiresApi(Build.VERSION_CODES.P) private suspend fun createMessageStyle(events: List, roomOverview: RoomOverview): Notification.MessagingStyle { val messageStyle = Notification.MessagingStyle( @@ -131,7 +141,8 @@ class NotificationFactory( .setAutoCancel(true) .build(), roomId = roomOverview.roomId, - summary = events.last().content + summary = events.last().content, + messageCount = events.size, ) } 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 a497ed2..fd71c44 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 @@ -21,7 +21,7 @@ class NotificationRenderer( suspend fun render(result: Map>, removedRooms: Set, onlyContainsRemovals: Boolean) { removedRooms.forEach { notificationManager.cancel(it.value, MESSAGE_NOTIFICATION_ID) } - val notifications = notificationFactory.createNotifications(result) + val notifications = notificationFactory.createNotifications(result, onlyContainsRemovals) withContext(Dispatchers.Main) { notifications.summaryNotification.ifNull { @@ -42,10 +42,8 @@ class NotificationRenderer( } notifications.summaryNotification?.let { - if (!onlyContainsRemovals) { - log(AppLogTag.NOTIFICATION, "notifying summary") - notificationManager.notify(SUMMARY_NOTIFICATION_ID, it) - } + log(AppLogTag.NOTIFICATION, "notifying summary") + notificationManager.notify(SUMMARY_NOTIFICATION_ID, it) } } } @@ -53,6 +51,6 @@ class NotificationRenderer( } sealed interface NotificationDelegate { - data class Room(val notification: Notification, val roomId: RoomId, val summary: String) : NotificationDelegate + data class Room(val notification: Notification, val roomId: RoomId, val summary: String, val messageCount: Int) : NotificationDelegate data class DismissRoom(val roomId: RoomId) : NotificationDelegate } \ No newline at end of file