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 GROUP_CHANNEL_ID = "group_channel_id"
const val SUMMARY_CHANNEL_ID = "summary_channel_id"
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()
return NotificationTypes.Room(
AndroidNotification(
channelId = when {
roomOverview.isDm() -> DIRECT_CHANNEL_ID
else -> GROUP_CHANNEL_ID
},
channelId = SUMMARY_CHANNEL_ID,
whenTimestamp = last.utcTimestamp,
groupId = GROUP_ID,
groupAlertBehavior = deviceMeta.whenPOrHigher(
@ -59,7 +56,11 @@ class NotificationFactory(
roomId = roomOverview.roomId,
summary = last.content,
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 openAppIntent = intentFactory.notificationOpenApp(context)
return AndroidNotification(
channelId = notifications.firstOrNull { it.isAlerting }?.notification?.channelId ?: notifications.first().notification.channelId,
channelId = notifications.mostRecent().summaryChannelId,
messageStyle = summaryInboxStyle,
alertMoreThanOnce = notifications.any { it.isAlerting },
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

View File

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

View File

@ -32,7 +32,6 @@ private val EVENTS = listOf(
aNotifiable("message two", utcTimestamp = 2),
)
class NotificationFactoryTest {
private val fakeContext = FakeContext()
@ -50,7 +49,12 @@ class NotificationFactoryTest {
@Test
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)
fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle())
@ -61,7 +65,12 @@ class NotificationFactoryTest {
@Test
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)
fakeNotificationStyleFactory.givenSummary(notifications).returns(anInboxStyle())
@ -129,7 +138,7 @@ class NotificationFactoryTest {
shouldAlertMoreThanOnce: Boolean,
) = NotificationTypes.Room(
AndroidNotification(
channelId = channel,
channelId = SUMMARY_CHANNEL_ID,
whenTimestamp = LATEST_EVENT.utcTimestamp,
groupId = "st",
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,
messageCount = EVENTS.size,
isAlerting = shouldAlertMoreThanOnce,
summaryChannelId = channel,
)
private fun expectedSummary(channelId: String, shouldAlertMoreThanOnce: Boolean) = AndroidNotification(

View File

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