Notification: better code

This commit is contained in:
Benoit Marty 2019-06-25 17:09:01 +02:00
parent 5a1242109d
commit b388be93c8
2 changed files with 24 additions and 23 deletions

View File

@ -216,22 +216,23 @@ class NotificationDrawerManager(val context: Context,
continue
}

val roomGroup = RoomEventGroupInfo(roomId)
roomGroup.hasNewEvent = false
roomGroup.shouldBing = false
roomGroup.isDirect = events[0].roomIsDirect
val roomName = events[0].roomName ?: events[0].senderName ?: ""

val roomEventGroupInfo = RoomEventGroupInfo(
roomId = roomId,
isDirect = events[0].roomIsDirect,
roomDisplayName = roomName)

val style = NotificationCompat.MessagingStyle(Person.Builder()
.setName(myUserDisplayName)
.setIcon(iconLoader.getUserIcon(myUserAvatarUrl))
.setKey(events[0].matrixID)
.build())
roomGroup.roomDisplayName = roomName

style.isGroupConversation = !roomGroup.isDirect
style.isGroupConversation = !roomEventGroupInfo.isDirect

if (!roomGroup.isDirect) {
style.conversationTitle = roomName
if (!roomEventGroupInfo.isDirect) {
style.conversationTitle = roomEventGroupInfo.roomDisplayName
}

val largeBitmap = getRoomBitmap(events)
@ -240,10 +241,10 @@ class NotificationDrawerManager(val context: Context,
for (event in events) {
//if all events in this room have already been displayed there is no need to update it
if (!event.hasBeenDisplayed) {
roomGroup.shouldBing = roomGroup.shouldBing || event.noisy
roomGroup.customSound = event.soundName
roomEventGroupInfo.shouldBing = roomEventGroupInfo.shouldBing || event.noisy
roomEventGroupInfo.customSound = event.soundName
}
roomGroup.hasNewEvent = roomGroup.hasNewEvent || !event.hasBeenDisplayed
roomEventGroupInfo.hasNewEvent = roomEventGroupInfo.hasNewEvent || !event.hasBeenDisplayed

val senderPerson = Person.Builder()
.setName(event.senderName)
@ -253,7 +254,7 @@ class NotificationDrawerManager(val context: Context,

if (event.outGoingMessage && event.outGoingMessageFailed) {
style.addMessage(context.getString(R.string.notification_inline_reply_failed), event.timestamp, senderPerson)
roomGroup.hasSmartReplyError = true
roomEventGroupInfo.hasSmartReplyError = true
} else {
style.addMessage(event.body, event.timestamp, senderPerson)
}
@ -274,7 +275,7 @@ class NotificationDrawerManager(val context: Context,
summaryInboxStyle.addLine(roomName)
}

if (firstTime || roomGroup.hasNewEvent) {
if (firstTime || roomEventGroupInfo.hasNewEvent) {
//Should update displayed notification
Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER $roomId need refresh")
val lastMessageTimestamp = events.last().timestamp
@ -283,14 +284,14 @@ class NotificationDrawerManager(val context: Context,
globalLastMessageTimestamp = lastMessageTimestamp
}

NotificationUtils.buildMessagesListNotification(context, style, roomGroup, largeBitmap, lastMessageTimestamp, myUserDisplayName)
NotificationUtils.buildMessagesListNotification(context, style, roomEventGroupInfo, largeBitmap, lastMessageTimestamp, myUserDisplayName)
?.let {
//is there an id for this room?
notifications.add(it)
NotificationUtils.showNotificationMessage(context, roomId, ROOM_MESSAGES_NOTIFICATION_ID, it)
}
hasNewEvent = true
summaryIsNoisy = summaryIsNoisy || roomGroup.shouldBing
summaryIsNoisy = summaryIsNoisy || roomEventGroupInfo.shouldBing
} else {
Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER $roomId is up to date")
}

View File

@ -20,15 +20,15 @@ package im.vector.riotredesign.features.notifications
* Data class to hold information about a group of notifications for a room
*/
data class RoomEventGroupInfo(
val roomId: String
val roomId: String,
val roomDisplayName: String = "",
val roomAvatarPath: String? = null,
val isDirect: Boolean = false
) {
var roomDisplayName: String = ""
var roomAvatarPath: String? = null
//An event in the list has not yet been display
// An event in the list has not yet been display
var hasNewEvent: Boolean = false
//true if at least one on the not yet displayed event is noisy
// true if at least one on the not yet displayed event is noisy
var shouldBing: Boolean = false
var customSound: String? = null
var hasSmartReplyError = false
var isDirect = false
}
var hasSmartReplyError: Boolean = false
}