renaming event lists to give more context and remove the list suffix/inconsistencies

This commit is contained in:
Adam Brown 2021-10-20 09:42:19 +01:00
parent d1f6db4236
commit 743a71c78d
3 changed files with 38 additions and 38 deletions

View file

@ -28,8 +28,8 @@ class NotifiableEventProcessor @Inject constructor(
private val autoAcceptInvites: AutoAcceptInvites private val autoAcceptInvites: AutoAcceptInvites
) { ) {
fun process(eventList: List<NotifiableEvent>, currentRoomId: String?, renderedEventsList: ProcessedEvents): ProcessedEvents { fun process(queuedEvents: List<NotifiableEvent>, currentRoomId: String?, renderedEvents: ProcessedEvents): ProcessedEvents {
val processedEventList = eventList.map { val processedEvents = queuedEvents.map {
val type = when (it) { val type = when (it) {
is InviteNotifiableEvent -> if (autoAcceptInvites.hideInvites) REMOVE else KEEP is InviteNotifiableEvent -> if (autoAcceptInvites.hideInvites) REMOVE else KEEP
is NotifiableMessageEvent -> if (shouldIgnoreMessageEventInRoom(currentRoomId, it.roomId) || outdatedDetector.isMessageOutdated(it)) { is NotifiableMessageEvent -> if (shouldIgnoreMessageEventInRoom(currentRoomId, it.roomId) || outdatedDetector.isMessageOutdated(it)) {
@ -40,11 +40,11 @@ class NotifiableEventProcessor @Inject constructor(
ProcessedEvent(type, it) ProcessedEvent(type, it)
} }
val removedEventsDiff = renderedEventsList.filter { renderedEvent -> val removedEventsDiff = renderedEvents.filter { renderedEvent ->
eventList.none { it.eventId == renderedEvent.event.eventId } queuedEvents.none { it.eventId == renderedEvent.event.eventId }
}.map { ProcessedEvent(REMOVE, it.event) } }.map { ProcessedEvent(REMOVE, it.event) }
return removedEventsDiff + processedEventList return removedEventsDiff + processedEvents
} }
private fun shouldIgnoreMessageEventInRoom(currentRoomId: String?, roomId: String?): Boolean { private fun shouldIgnoreMessageEventInRoom(currentRoomId: String?, roomId: String?): Boolean {

View file

@ -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 * 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 * The last known rendered notifiable events
* we keep track of them in order to know which events have been removed from the eventList * 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 * 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 val avatarSize = context.resources.getDimensionPixelSize(R.dimen.profile_avatar_size)
private var currentRoomId: String? = null private var currentRoomId: String? = null
@ -105,8 +105,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
} else { } else {
Timber.d("onNotifiableEventReceived(): is push: ${notifiableEvent.canBeReplaced}") Timber.d("onNotifiableEventReceived(): is push: ${notifiableEvent.canBeReplaced}")
} }
synchronized(eventList) { synchronized(queuedEvents) {
val existing = eventList.firstOrNull { it.eventId == notifiableEvent.eventId } val existing = queuedEvents.firstOrNull { it.eventId == notifiableEvent.eventId }
if (existing != null) { if (existing != null) {
if (existing.canBeReplaced) { if (existing.canBeReplaced) {
// Use the event coming from the event stream as it may contains more info than // 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 // Use setOnlyAlertOnce to ensure update notification does not interfere with sound
// from first notify invocation as outlined in: // from first notify invocation as outlined in:
// https://developer.android.com/training/notify-user/build-notification#Updating // https://developer.android.com/training/notify-user/build-notification#Updating
eventList.remove(existing) queuedEvents.remove(existing)
eventList.add(notifiableEvent) queuedEvents.add(notifiableEvent)
} else { } else {
// keep the existing one, do not replace // 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 // Check if this is an edit
if (notifiableEvent.editedEventId != null) { if (notifiableEvent.editedEventId != null) {
// This is an edition // This is an edition
val eventBeforeEdition = eventList.firstOrNull { val eventBeforeEdition = queuedEvents.firstOrNull {
// Edition of an event // Edition of an event
it.eventId == notifiableEvent.editedEventId || it.eventId == notifiableEvent.editedEventId ||
// or edition of an edition // or edition of an edition
@ -135,9 +135,9 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
if (eventBeforeEdition != null) { if (eventBeforeEdition != null) {
// Replace the existing notification with the new content // Replace the existing notification with the new content
eventList.remove(eventBeforeEdition) queuedEvents.remove(eventBeforeEdition)
eventList.add(notifiableEvent) queuedEvents.add(notifiableEvent)
} else { } else {
// Ignore an edit of a not displayed event in the notification drawer // 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") Timber.d("onNotifiableEventReceived(): skipping event, already seen")
} else { } else {
seenEventIds.put(notifiableEvent.eventId) 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) { fun onEventRedacted(eventId: String) {
synchronized(eventList) { synchronized(queuedEvents) {
eventList.replace(eventId) { queuedEvents.replace(eventId) {
when (it) { when (it) {
is InviteNotifiableEvent -> it.copy(isRedacted = true) is InviteNotifiableEvent -> it.copy(isRedacted = true)
is NotifiableMessageEvent -> 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 * Clear all known events and refresh the notification drawer
*/ */
fun clearAllEvents() { fun clearAllEvents() {
synchronized(eventList) { synchronized(queuedEvents) {
eventList.clear() queuedEvents.clear()
} }
refreshNotificationDrawer() refreshNotificationDrawer()
} }
@ -194,7 +194,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
*/ */
fun setCurrentRoom(roomId: String?) { fun setCurrentRoom(roomId: String?) {
var hasChanged: Boolean var hasChanged: Boolean
synchronized(eventList) { synchronized(queuedEvents) {
hasChanged = roomId != currentRoomId hasChanged = roomId != currentRoomId
currentRoomId = roomId currentRoomId = roomId
} }
@ -211,8 +211,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
} }
private fun removeAll(predicate: (NotifiableEvent) -> Boolean): Boolean { private fun removeAll(predicate: (NotifiableEvent) -> Boolean): Boolean {
return synchronized(eventList) { return synchronized(queuedEvents) {
eventList.removeAll(predicate) queuedEvents.removeAll(predicate)
} }
} }
@ -247,17 +247,17 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
useCompleteNotificationFormat = newSettings useCompleteNotificationFormat = newSettings
} }
val eventsToRender = synchronized(eventList) { val eventsToRender = synchronized(queuedEvents) {
notifiableEventProcessor.process(eventList, currentRoomId, renderedEventsList).also { notifiableEventProcessor.process(queuedEvents, currentRoomId, renderedEvents).also {
eventList.clear() queuedEvents.clear()
eventList.addAll(it.onlyKeptEvents()) queuedEvents.addAll(it.onlyKeptEvents())
} }
} }
if (renderedEventsList == eventsToRender) { if (renderedEvents == eventsToRender) {
Timber.d("Skipping notification update due to event list not changing") Timber.d("Skipping notification update due to event list not changing")
} else { } else {
renderedEventsList = eventsToRender renderedEvents = eventsToRender
val session = currentSession ?: return val session = currentSession ?: return
val user = session.getUser(session.myUserId) val user = session.getUser(session.myUserId)
// myUserDisplayName cannot be empty else NotificationCompat.MessagingStyle() will crash // myUserDisplayName cannot be empty else NotificationCompat.MessagingStyle() will crash
@ -277,8 +277,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
} }
fun persistInfo() { fun persistInfo() {
synchronized(eventList) { synchronized(queuedEvents) {
if (eventList.isEmpty()) { if (queuedEvents.isEmpty()) {
deleteCachedRoomNotifications() deleteCachedRoomNotifications()
return return
} }
@ -286,7 +286,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME) val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME)
if (!file.exists()) file.createNewFile() if (!file.exists()) file.createNewFile()
FileOutputStream(file).use { FileOutputStream(file).use {
currentSession?.securelyStoreObject(eventList, KEY_ALIAS_SECRET_STORAGE, it) currentSession?.securelyStoreObject(queuedEvents, KEY_ALIAS_SECRET_STORAGE, it)
} }
} catch (e: Throwable) { } catch (e: Throwable) {
Timber.e(e, "## Failed to save cached notification info") Timber.e(e, "## Failed to save cached notification info")

View file

@ -38,7 +38,7 @@ class NotifiableEventProcessorTest {
aSimpleNotifiableEvent(eventId = "event-2") 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( result shouldBeEqualTo listOfProcessedEvents(
Type.KEEP to events[0], Type.KEEP to events[0],
@ -54,7 +54,7 @@ class NotifiableEventProcessorTest {
anInviteNotifiableEvent(roomId = "room-2") 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( result shouldBeEqualTo listOfProcessedEvents(
Type.REMOVE to events[0], Type.REMOVE to events[0],
@ -70,7 +70,7 @@ class NotifiableEventProcessorTest {
anInviteNotifiableEvent(roomId = "room-2") 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( result shouldBeEqualTo listOfProcessedEvents(
Type.KEEP to events[0], Type.KEEP to events[0],
@ -83,7 +83,7 @@ class NotifiableEventProcessorTest {
val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1")) val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1"))
outdatedDetector.givenEventIsOutOfDate(events[0]) 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( result shouldBeEqualTo listOfProcessedEvents(
Type.REMOVE to events[0], Type.REMOVE to events[0],
@ -95,7 +95,7 @@ class NotifiableEventProcessorTest {
val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1")) val events = listOf(aNotifiableMessageEvent(eventId = "event-1", roomId = "room-1"))
outdatedDetector.givenEventIsInDate(events[0]) 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( result shouldBeEqualTo listOfProcessedEvents(
Type.KEEP to events[0], 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`() { 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 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( result shouldBeEqualTo listOfProcessedEvents(
Type.REMOVE to events[0], Type.REMOVE to events[0],
@ -121,7 +121,7 @@ class NotifiableEventProcessorTest {
ProcessedEvent(Type.KEEP, anInviteNotifiableEvent(roomId = "event-2")) 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( result shouldBeEqualTo listOfProcessedEvents(
Type.REMOVE to renderedEvents[1].event, Type.REMOVE to renderedEvents[1].event,