Merge branch 'main' into dependabot/gradle/io.mockk-mockk-1.12.5

This commit is contained in:
Adam Brown 2022-07-28 19:51:47 +01:00 committed by GitHub
commit 95b27fc456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import android.os.Build
const val DIRECT_CHANNEL_ID = "direct_channel_id" const val DIRECT_CHANNEL_ID = "direct_channel_id"
const val GROUP_CHANNEL_ID = "group_channel_id" const val GROUP_CHANNEL_ID = "group_channel_id"
const val SUMMARY_CHANNEL_ID = "summary_channel_id"
private const val CHATS_NOTIFICATION_GROUP_ID = "chats_notification_group" private const val CHATS_NOTIFICATION_GROUP_ID = "chats_notification_group"
@ -43,6 +44,18 @@ class NotificationChannels(
} }
) )
} }
if (notificationManager.getNotificationChannel(SUMMARY_CHANNEL_ID) == null) {
notificationManager.createNotificationChannel(
NotificationChannel(
SUMMARY_CHANNEL_ID,
"Other notifications",
NotificationManager.IMPORTANCE_DEFAULT,
).also {
it.group = CHATS_NOTIFICATION_GROUP_ID
}
)
}
} }
} }

View File

@ -37,10 +37,7 @@ class NotificationFactory(
val last = sortedEvents.last() val last = sortedEvents.last()
return NotificationTypes.Room( return NotificationTypes.Room(
AndroidNotification( AndroidNotification(
channelId = when { channelId = SUMMARY_CHANNEL_ID,
roomOverview.isDm() -> DIRECT_CHANNEL_ID
else -> GROUP_CHANNEL_ID
},
whenTimestamp = last.utcTimestamp, whenTimestamp = last.utcTimestamp,
groupId = GROUP_ID, groupId = GROUP_ID,
groupAlertBehavior = deviceMeta.whenPOrHigher( groupAlertBehavior = deviceMeta.whenPOrHigher(
@ -59,7 +56,11 @@ class NotificationFactory(
roomId = roomOverview.roomId, roomId = roomOverview.roomId,
summary = last.content, summary = last.content,
messageCount = sortedEvents.size, messageCount = sortedEvents.size,
isAlerting = shouldAlertMoreThanOnce isAlerting = shouldAlertMoreThanOnce,
summaryChannelId = when {
roomOverview.isDm() -> DIRECT_CHANNEL_ID
else -> GROUP_CHANNEL_ID
}
) )
} }
@ -67,7 +68,7 @@ class NotificationFactory(
val summaryInboxStyle = notificationStyleFactory.summary(notifications) val summaryInboxStyle = notificationStyleFactory.summary(notifications)
val openAppIntent = intentFactory.notificationOpenApp(context) val openAppIntent = intentFactory.notificationOpenApp(context)
return AndroidNotification( return AndroidNotification(
channelId = notifications.firstOrNull { it.isAlerting }?.notification?.channelId ?: notifications.first().notification.channelId, channelId = notifications.mostRecent().summaryChannelId,
messageStyle = summaryInboxStyle, messageStyle = summaryInboxStyle,
alertMoreThanOnce = notifications.any { it.isAlerting }, alertMoreThanOnce = notifications.any { it.isAlerting },
smallIcon = R.drawable.ic_notification_small_icon, smallIcon = R.drawable.ic_notification_small_icon,
@ -83,4 +84,6 @@ class NotificationFactory(
} }
} }
private fun List<NotificationTypes.Room>.mostRecent() = this.sortedBy { it.notification.whenTimestamp }.first()
private fun RoomOverview.isDm() = !this.isGroup private fun RoomOverview.isDm() = !this.isGroup

View File

@ -68,7 +68,8 @@ sealed interface NotificationTypes {
val roomId: RoomId, val roomId: RoomId,
val summary: String, val summary: String,
val messageCount: Int, val messageCount: Int,
val isAlerting: Boolean val isAlerting: Boolean,
val summaryChannelId: String,
) : NotificationTypes ) : NotificationTypes
data class DismissRoom(val roomId: RoomId) : NotificationTypes data class DismissRoom(val roomId: RoomId) : NotificationTypes

View File

@ -32,7 +32,6 @@ private val EVENTS = listOf(
aNotifiable("message two", utcTimestamp = 2), aNotifiable("message two", utcTimestamp = 2),
) )
class NotificationFactoryTest { class NotificationFactoryTest {
private val fakeContext = FakeContext() private val fakeContext = FakeContext()
@ -50,7 +49,12 @@ class NotificationFactoryTest {
@Test @Test
fun `given alerting room notification, when creating summary, then is alerting`() { fun `given alerting room notification, when creating summary, then is alerting`() {
val notifications = listOf(aRoomNotification(notification = anAndroidNotification(channelId = A_CHANNEL_ID), isAlerting = true)) val notifications = listOf(
aRoomNotification(
summaryChannelId = A_CHANNEL_ID,
notification = anAndroidNotification(channelId = A_CHANNEL_ID), isAlerting = true
)
)
fakeIntentFactory.givenNotificationOpenApp(fakeContext.instance).returns(AN_OPEN_APP_INTENT) fakeIntentFactory.givenNotificationOpenApp(fakeContext.instance).returns(AN_OPEN_APP_INTENT)
fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle()) fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle())
@ -61,7 +65,12 @@ class NotificationFactoryTest {
@Test @Test
fun `given non alerting room notification, when creating summary, then is alerting`() { fun `given non alerting room notification, when creating summary, then is alerting`() {
val notifications = listOf(aRoomNotification(notification = anAndroidNotification(channelId = A_CHANNEL_ID), isAlerting = false)) val notifications = listOf(
aRoomNotification(
summaryChannelId = A_CHANNEL_ID,
notification = anAndroidNotification(channelId = A_CHANNEL_ID), isAlerting = false
)
)
fakeIntentFactory.givenNotificationOpenApp(fakeContext.instance).returns(AN_OPEN_APP_INTENT) fakeIntentFactory.givenNotificationOpenApp(fakeContext.instance).returns(AN_OPEN_APP_INTENT)
fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle()) fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle())
@ -129,7 +138,7 @@ class NotificationFactoryTest {
shouldAlertMoreThanOnce: Boolean, shouldAlertMoreThanOnce: Boolean,
) = NotificationTypes.Room( ) = NotificationTypes.Room(
AndroidNotification( AndroidNotification(
channelId = channel, channelId = SUMMARY_CHANNEL_ID,
whenTimestamp = LATEST_EVENT.utcTimestamp, whenTimestamp = LATEST_EVENT.utcTimestamp,
groupId = "st", groupId = "st",
groupAlertBehavior = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.GROUP_ALERT_SUMMARY else null, groupAlertBehavior = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.GROUP_ALERT_SUMMARY else null,
@ -146,6 +155,7 @@ class NotificationFactoryTest {
summary = LATEST_EVENT.content, summary = LATEST_EVENT.content,
messageCount = EVENTS.size, messageCount = EVENTS.size,
isAlerting = shouldAlertMoreThanOnce, isAlerting = shouldAlertMoreThanOnce,
summaryChannelId = channel,
) )
private fun expectedSummary(channelId: String, shouldAlertMoreThanOnce: Boolean) = AndroidNotification( private fun expectedSummary(channelId: String, shouldAlertMoreThanOnce: Boolean) = AndroidNotification(

View File

@ -18,12 +18,14 @@ object NotificationFixtures {
summary: String = "a summary line", summary: String = "a summary line",
messageCount: Int = 1, messageCount: Int = 1,
isAlerting: Boolean = false, isAlerting: Boolean = false,
summaryChannelId: String = "a-summary-channel-id",
) = NotificationTypes.Room( ) = NotificationTypes.Room(
notification, notification,
aRoomId(), aRoomId(),
summary = summary, summary = summary,
messageCount = messageCount, messageCount = messageCount,
isAlerting = isAlerting isAlerting = isAlerting,
summaryChannelId = summaryChannelId
) )
fun aDismissRoomNotification( fun aDismissRoomNotification(