From c99dd4a615d672cef35c0029e156c114c21ea163 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 7 Oct 2021 11:46:13 +0100 Subject: [PATCH] making the isRedacted event property immutable - also makes the notifiable events sealed interfaces so that we can copy the data classes with new redacted values when it changes --- .../notifications/InviteNotifiableEvent.kt | 5 +++-- .../features/notifications/NotifiableEvent.kt | 6 ++---- .../notifications/NotifiableMessageEvent.kt | 7 +++---- .../notifications/NotificationDrawerManager.kt | 17 ++++++++++++++--- .../notifications/SimpleNotifiableEvent.kt | 6 ++++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/notifications/InviteNotifiableEvent.kt b/vector/src/main/java/im/vector/app/features/notifications/InviteNotifiableEvent.kt index 91cc216759..9f958965ea 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/InviteNotifiableEvent.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/InviteNotifiableEvent.kt @@ -26,8 +26,9 @@ data class InviteNotifiableEvent( val type: String?, val timestamp: Long, val soundName: String?, - override var isPushGatewayEvent: Boolean = false) : NotifiableEvent { + override var isPushGatewayEvent: Boolean = false, + override val isRedacted: Boolean = false +) : NotifiableEvent { - override var isRedacted: Boolean = false override var hasBeenDisplayed = false } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEvent.kt b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEvent.kt index 279bc192fd..6365b1c7bf 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEvent.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEvent.kt @@ -20,14 +20,12 @@ import java.io.Serializable /** * Parent interface for all events which can be displayed as a Notification */ -interface NotifiableEvent : Serializable { +sealed interface NotifiableEvent : Serializable { val eventId: String val editedEventId: String? var noisy: Boolean - var hasBeenDisplayed: Boolean - var isRedacted: Boolean - // Used to know if event should be replaced with the one coming from eventstream var isPushGatewayEvent: Boolean + val isRedacted: Boolean } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotifiableMessageEvent.kt b/vector/src/main/java/im/vector/app/features/notifications/NotifiableMessageEvent.kt index f23c84afad..7d9ef42f8e 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotifiableMessageEvent.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotifiableMessageEvent.kt @@ -36,12 +36,11 @@ data class NotifiableMessageEvent( // This is used for >N notification, as the result of a smart reply val outGoingMessage: Boolean = false, - val outGoingMessageFailed: Boolean = false - + val outGoingMessageFailed: Boolean = false, + override var hasBeenDisplayed: Boolean = false, + override val isRedacted: Boolean = false ) : NotifiableEvent { - override var hasBeenDisplayed: Boolean = false - override var isRedacted: Boolean = false override var isPushGatewayEvent: Boolean = false val type: String = EventType.MESSAGE 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 8648490c6a..5496dd6649 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 @@ -162,9 +162,12 @@ class NotificationDrawerManager @Inject constructor(private val context: Context fun onEventRedacted(eventId: String) { synchronized(eventList) { - eventList.find { it.eventId == eventId }?.apply { - isRedacted = true - hasBeenDisplayed = false + eventList.replace(eventId) { + when (it) { + is InviteNotifiableEvent -> it.copy(isRedacted = true).apply { hasBeenDisplayed = false } + is NotifiableMessageEvent -> it.copy(isRedacted = true).apply { hasBeenDisplayed = false } + is SimpleNotifiableEvent -> it.copy(isRedacted = true).apply { hasBeenDisplayed = false } + } } } } @@ -665,3 +668,11 @@ class NotificationDrawerManager @Inject constructor(private val context: Context private const val KEY_ALIAS_SECRET_STORAGE = "notificationMgr" } } + +private fun MutableList.replace(eventId: String, block: (NotifiableEvent) -> NotifiableEvent) { + val indexToReplace = indexOfFirst { it.eventId == eventId } + if (indexToReplace == -1) { + return + } + set(indexToReplace, block(get(indexToReplace))) +} diff --git a/vector/src/main/java/im/vector/app/features/notifications/SimpleNotifiableEvent.kt b/vector/src/main/java/im/vector/app/features/notifications/SimpleNotifiableEvent.kt index 7e776222af..49a69fc51a 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/SimpleNotifiableEvent.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/SimpleNotifiableEvent.kt @@ -25,8 +25,10 @@ data class SimpleNotifiableEvent( val type: String?, val timestamp: Long, val soundName: String?, - override var isPushGatewayEvent: Boolean = false) : NotifiableEvent { + override var isPushGatewayEvent: Boolean = false, + override val isRedacted: Boolean = false +) : NotifiableEvent { override var hasBeenDisplayed: Boolean = false - override var isRedacted: Boolean = false + }