From 9009606e86fd1584513324f203c8051af19d68ea Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 3 Nov 2021 12:24:16 +0000 Subject: [PATCH] flattening some of the onNotifiableEventReceived branches to simplify the chain --- .../NotificationDrawerManager.kt | 60 +++++++++---------- .../notifications/NotificationEventQueue.kt | 7 ++- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt index 649d733914..0e43e41819 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt @@ -107,40 +107,34 @@ class NotificationDrawerManager @Inject constructor(private val context: Context } synchronized(queuedEvents) { val existing = queuedEvents.findExistingById(notifiableEvent) - if (existing != null) { - if (existing.canBeReplaced) { - // Use the event coming from the event stream as it may contains more info than - // the fcm one (like type/content/clear text) (e.g when an encrypted message from - // FCM should be update with clear text after a sync) - // In this case the message has already been notified, and might have done some noise - // So we want the notification to be updated even if it has already been displayed - // Use setOnlyAlertOnce to ensure update notification does not interfere with sound - // from first notify invocation as outlined in: - // https://developer.android.com/training/notify-user/build-notification#Updating - queuedEvents.replace(replace = existing, with = notifiableEvent) - } else { - // keep the existing one, do not replace + val edited = queuedEvents.findEdited(notifiableEvent) + when { + existing != null -> { + if (existing.canBeReplaced) { + // Use the event coming from the event stream as it may contains more info than + // the fcm one (like type/content/clear text) (e.g when an encrypted message from + // FCM should be update with clear text after a sync) + // In this case the message has already been notified, and might have done some noise + // So we want the notification to be updated even if it has already been displayed + // Use setOnlyAlertOnce to ensure update notification does not interfere with sound + // from first notify invocation as outlined in: + // https://developer.android.com/training/notify-user/build-notification#Updating + queuedEvents.replace(replace = existing, with = notifiableEvent) + } else { + // keep the existing one, do not replace + } } - } else { - // Check if this is an edit - if (notifiableEvent.editedEventId != null) { - // This is an edition - val eventBeforeEdition = queuedEvents.findEdited(notifiableEvent) - if (eventBeforeEdition != null) { - // Replace the existing notification with the new content - queuedEvents.replace(replace = eventBeforeEdition, with = notifiableEvent) - } else { - // Ignore an edit of a not displayed event in the notification drawer - } - } else { - // Not an edit - if (seenEventIds.contains(notifiableEvent.eventId)) { - // we've already seen the event, lets skip - Timber.d("onNotifiableEventReceived(): skipping event, already seen") - } else { - seenEventIds.put(notifiableEvent.eventId) - queuedEvents.add(notifiableEvent) - } + edited != null -> { + // Replace the existing notification with the new content + queuedEvents.replace(replace = edited, with = notifiableEvent) + } + seenEventIds.contains(notifiableEvent.eventId) -> { + // we've already seen the event, lets skip + Timber.d("onNotifiableEventReceived(): skipping event, already seen") + } + else -> { + seenEventIds.put(notifiableEvent.eventId) + queuedEvents.add(notifiableEvent) } } } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationEventQueue.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationEventQueue.kt index 29ec91c976..7488b98664 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationEventQueue.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationEventQueue.kt @@ -66,9 +66,10 @@ class NotificationEventQueue( } fun findEdited(notifiableEvent: NotifiableEvent): NotifiableEvent? { - return queue.firstOrNull { - it.eventId == notifiableEvent.editedEventId || - it.editedEventId == notifiableEvent.editedEventId // or edition of an edition + return notifiableEvent.editedEventId?.let { editedId -> + queue.firstOrNull { + it.eventId == editedId || it.editedEventId == editedId + } } }