From 743a71c78da6be0f23f18f5ee94984b725d72439 Mon Sep 17 00:00:00 2001
From: Adam Brown <adampsbrown@gmail.com>
Date: Wed, 20 Oct 2021 09:42:19 +0100
Subject: [PATCH] renaming event lists to give more context and remove the list
 suffix/inconsistencies

---
 .../notifications/NotifiableEventProcessor.kt | 10 ++--
 .../NotificationDrawerManager.kt              | 52 +++++++++----------
 .../NotifiableEventProcessorTest.kt           | 14 ++---
 3 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt
index 338c3d58eb..858df81bf6 100644
--- a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt
+++ b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt
@@ -28,8 +28,8 @@ class NotifiableEventProcessor @Inject constructor(
         private val autoAcceptInvites: AutoAcceptInvites
 ) {
 
-    fun process(eventList: List<NotifiableEvent>, currentRoomId: String?, renderedEventsList: ProcessedEvents): ProcessedEvents {
-        val processedEventList = eventList.map {
+    fun process(queuedEvents: List<NotifiableEvent>, currentRoomId: String?, renderedEvents: ProcessedEvents): ProcessedEvents {
+        val processedEvents = queuedEvents.map {
             val type = when (it) {
                 is InviteNotifiableEvent  -> if (autoAcceptInvites.hideInvites) REMOVE else KEEP
                 is NotifiableMessageEvent -> if (shouldIgnoreMessageEventInRoom(currentRoomId, it.roomId) || outdatedDetector.isMessageOutdated(it)) {
@@ -40,11 +40,11 @@ class NotifiableEventProcessor @Inject constructor(
             ProcessedEvent(type, it)
         }
 
-        val removedEventsDiff = renderedEventsList.filter { renderedEvent ->
-            eventList.none { it.eventId == renderedEvent.event.eventId }
+        val removedEventsDiff = renderedEvents.filter { renderedEvent ->
+            queuedEvents.none { it.eventId == renderedEvent.event.eventId }
         }.map { ProcessedEvent(REMOVE, it.event) }
 
-        return removedEventsDiff + processedEventList
+        return removedEventsDiff + processedEvents
     }
 
     private fun shouldIgnoreMessageEventInRoom(currentRoomId: String?, roomId: String?): Boolean {
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 5d2212ba1e..c052de650e 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
@@ -63,14 +63,14 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
      *
      * Events are unique by their properties, we should be careful not to insert multiple events with the same event-id
      */
-    private val eventList = loadEventInfo()
+    private val queuedEvents = loadEventInfo()
 
     /**
      * The last known rendered notifiable events
      * we keep track of them in order to know which events have been removed from the eventList
      * allowing us to cancel any notifications previous displayed by now removed events
      */
-    private var renderedEventsList = emptyList<ProcessedEvent<NotifiableEvent>>()
+    private var renderedEvents = emptyList<ProcessedEvent<NotifiableEvent>>()
     private val avatarSize = context.resources.getDimensionPixelSize(R.dimen.profile_avatar_size)
     private var currentRoomId: String? = null
 
@@ -105,8 +105,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
         } else {
             Timber.d("onNotifiableEventReceived(): is push: ${notifiableEvent.canBeReplaced}")
         }
-        synchronized(eventList) {
-            val existing = eventList.firstOrNull { it.eventId == notifiableEvent.eventId }
+        synchronized(queuedEvents) {
+            val existing = queuedEvents.firstOrNull { it.eventId == notifiableEvent.eventId }
             if (existing != null) {
                 if (existing.canBeReplaced) {
                     // Use the event coming from the event stream as it may contains more info than
@@ -117,8 +117,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
                     // 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
-                    eventList.remove(existing)
-                    eventList.add(notifiableEvent)
+                    queuedEvents.remove(existing)
+                    queuedEvents.add(notifiableEvent)
                 } else {
                     // keep the existing one, do not replace
                 }
@@ -126,7 +126,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
                 // Check if this is an edit
                 if (notifiableEvent.editedEventId != null) {
                     // This is an edition
-                    val eventBeforeEdition = eventList.firstOrNull {
+                    val eventBeforeEdition = queuedEvents.firstOrNull {
                         // Edition of an event
                         it.eventId == notifiableEvent.editedEventId ||
                                 // or edition of an edition
@@ -135,9 +135,9 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
 
                     if (eventBeforeEdition != null) {
                         // Replace the existing notification with the new content
-                        eventList.remove(eventBeforeEdition)
+                        queuedEvents.remove(eventBeforeEdition)
 
-                        eventList.add(notifiableEvent)
+                        queuedEvents.add(notifiableEvent)
                     } else {
                         // Ignore an edit of a not displayed event in the notification drawer
                     }
@@ -148,7 +148,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
                         Timber.d("onNotifiableEventReceived(): skipping event, already seen")
                     } else {
                         seenEventIds.put(notifiableEvent.eventId)
-                        eventList.add(notifiableEvent)
+                        queuedEvents.add(notifiableEvent)
                     }
                 }
             }
@@ -156,8 +156,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
     }
 
     fun onEventRedacted(eventId: String) {
-        synchronized(eventList) {
-            eventList.replace(eventId) {
+        synchronized(queuedEvents) {
+            queuedEvents.replace(eventId) {
                 when (it) {
                     is InviteNotifiableEvent  -> it.copy(isRedacted = true)
                     is NotifiableMessageEvent -> it.copy(isRedacted = true)
@@ -171,8 +171,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
      * Clear all known events and refresh the notification drawer
      */
     fun clearAllEvents() {
-        synchronized(eventList) {
-            eventList.clear()
+        synchronized(queuedEvents) {
+            queuedEvents.clear()
         }
         refreshNotificationDrawer()
     }
@@ -194,7 +194,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
      */
     fun setCurrentRoom(roomId: String?) {
         var hasChanged: Boolean
-        synchronized(eventList) {
+        synchronized(queuedEvents) {
             hasChanged = roomId != currentRoomId
             currentRoomId = roomId
         }
@@ -211,8 +211,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
     }
 
     private fun removeAll(predicate: (NotifiableEvent) -> Boolean): Boolean {
-        return synchronized(eventList) {
-            eventList.removeAll(predicate)
+        return synchronized(queuedEvents) {
+            queuedEvents.removeAll(predicate)
         }
     }
 
@@ -247,17 +247,17 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
             useCompleteNotificationFormat = newSettings
         }
 
-        val eventsToRender = synchronized(eventList) {
-            notifiableEventProcessor.process(eventList, currentRoomId, renderedEventsList).also {
-                eventList.clear()
-                eventList.addAll(it.onlyKeptEvents())
+        val eventsToRender = synchronized(queuedEvents) {
+            notifiableEventProcessor.process(queuedEvents, currentRoomId, renderedEvents).also {
+                queuedEvents.clear()
+                queuedEvents.addAll(it.onlyKeptEvents())
             }
         }
 
-        if (renderedEventsList == eventsToRender) {
+        if (renderedEvents == eventsToRender) {
             Timber.d("Skipping notification update due to event list not changing")
         } else {
-            renderedEventsList = eventsToRender
+            renderedEvents = eventsToRender
             val session = currentSession ?: return
             val user = session.getUser(session.myUserId)
             // myUserDisplayName cannot be empty else NotificationCompat.MessagingStyle() will crash
@@ -277,8 +277,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
     }
 
     fun persistInfo() {
-        synchronized(eventList) {
-            if (eventList.isEmpty()) {
+        synchronized(queuedEvents) {
+            if (queuedEvents.isEmpty()) {
                 deleteCachedRoomNotifications()
                 return
             }
@@ -286,7 +286,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
                 val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME)
                 if (!file.exists()) file.createNewFile()
                 FileOutputStream(file).use {
-                    currentSession?.securelyStoreObject(eventList, KEY_ALIAS_SECRET_STORAGE, it)
+                    currentSession?.securelyStoreObject(queuedEvents, KEY_ALIAS_SECRET_STORAGE, it)
                 }
             } catch (e: Throwable) {
                 Timber.e(e, "## Failed to save cached notification info")
diff --git a/vector/src/test/java/im/vector/app/features/notifications/NotifiableEventProcessorTest.kt b/vector/src/test/java/im/vector/app/features/notifications/NotifiableEventProcessorTest.kt
index 342567753c..f6938cb4ae 100644
--- a/vector/src/test/java/im/vector/app/features/notifications/NotifiableEventProcessorTest.kt
+++ b/vector/src/test/java/im/vector/app/features/notifications/NotifiableEventProcessorTest.kt
@@ -38,7 +38,7 @@ class NotifiableEventProcessorTest {
                 aSimpleNotifiableEvent(eventId = "event-2")
         )
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.KEEP to events[0],
@@ -54,7 +54,7 @@ class NotifiableEventProcessorTest {
                 anInviteNotifiableEvent(roomId = "room-2")
         )
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.REMOVE to events[0],
@@ -70,7 +70,7 @@ class NotifiableEventProcessorTest {
                 anInviteNotifiableEvent(roomId = "room-2")
         )
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.KEEP to events[0],
@@ -83,7 +83,7 @@ class NotifiableEventProcessorTest {
         val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1"))
         outdatedDetector.givenEventIsOutOfDate(events[0])
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.REMOVE to events[0],
@@ -95,7 +95,7 @@ class NotifiableEventProcessorTest {
         val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1"))
         outdatedDetector.givenEventIsInDate(events[0])
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.KEEP to events[0],
@@ -106,7 +106,7 @@ class NotifiableEventProcessorTest {
     fun `given viewing the same room as message event when processing then removes message`() {
         val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1"))
 
-        val result = eventProcessor.process(events, currentRoomId = "room-1", renderedEventsList = emptyList())
+        val result = eventProcessor.process(events, currentRoomId = "room-1", renderedEvents = emptyList())
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.REMOVE to events[0],
@@ -121,7 +121,7 @@ class NotifiableEventProcessorTest {
                 ProcessedEvent(Type.KEEP, anInviteNotifiableEvent(roomId = "event-2"))
         )
 
-        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEventsList = renderedEvents)
+        val result = eventProcessor.process(events, currentRoomId = NOT_VIEWING_A_ROOM, renderedEvents = renderedEvents)
 
         result shouldBeEqualTo listOfProcessedEvents(
                 Type.REMOVE to renderedEvents[1].event,