@@ -22,12 +22,20 @@ import io.element.android.libraries.matrix.ui.media.ImageLoaderHolder
2222import io.element.android.libraries.push.api.notifications.NotificationCleaner
2323import io.element.android.libraries.push.api.notifications.NotificationIdProvider
2424import io.element.android.libraries.push.impl.notifications.factories.NotificationCreator
25+ import io.element.android.libraries.push.impl.notifications.model.FallbackNotifiableEvent
26+ import io.element.android.libraries.push.impl.notifications.model.InviteNotifiableEvent
2527import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
26- import io.element.android.libraries.push.impl.notifications.model.shouldIgnoreEventInRoom
28+ import io.element.android.libraries.push.impl.notifications.model.NotifiableMessageEvent
29+ import io.element.android.libraries.push.impl.notifications.model.NotifiableRingingCallEvent
30+ import io.element.android.libraries.push.impl.notifications.model.SimpleNotifiableEvent
2731import io.element.android.libraries.sessionstorage.api.observer.SessionListener
2832import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
33+ import io.element.android.services.appnavstate.api.AppNavigationState
2934import io.element.android.services.appnavstate.api.AppNavigationStateService
3035import io.element.android.services.appnavstate.api.NavigationState
36+ import io.element.android.services.appnavstate.api.currentRoomId
37+ import io.element.android.services.appnavstate.api.currentSessionId
38+ import io.element.android.services.appnavstate.api.currentThreadId
3139import kotlinx.coroutines.CoroutineScope
3240import kotlinx.coroutines.launch
3341
@@ -71,7 +79,10 @@ class DefaultNotificationDrawerManager(
7179 private fun onAppNavigationStateChange (navigationState : NavigationState ) {
7280 when (navigationState) {
7381 NavigationState .Root -> {}
74- is NavigationState .Session -> {}
82+ is NavigationState .Session -> {
83+ // Cleanup the fallback notification
84+ clearFallbackForSession(navigationState.sessionId)
85+ }
7586 is NavigationState .Room -> {
7687 // Cleanup notification for current room
7788 clearMessagesForRoom(
@@ -94,14 +105,11 @@ class DefaultNotificationDrawerManager(
94105 * Events might be grouped and there might not be one notification per event!
95106 */
96107 suspend fun onNotifiableEventReceived (notifiableEvent : NotifiableEvent ) {
97- if (notifiableEvent.shouldIgnoreEventInRoom(appNavigationStateService.appNavigationState.value)) {
98- return
99- }
100- renderEvents(listOf (notifiableEvent))
108+ onNotifiableEventsReceived(listOf (notifiableEvent))
101109 }
102110
103111 suspend fun onNotifiableEventsReceived (notifiableEvents : List <NotifiableEvent >) {
104- val eventsToNotify = notifiableEvents.filter { ! it.shouldIgnoreEventInRoom( appNavigationStateService.appNavigationState.value) }
112+ val eventsToNotify = notifiableEvents.filter { ! appNavigationStateService.appNavigationState.value.shouldIgnoreEvent(it ) }
105113 renderEvents(eventsToNotify)
106114 }
107115
@@ -121,6 +129,17 @@ class DefaultNotificationDrawerManager(
121129 .forEach { notificationDisplayer.cancelNotification(it.tag, it.id) }
122130 }
123131
132+ /* *
133+ * Remove the fallback notification for the session.
134+ */
135+ fun clearFallbackForSession (sessionId : SessionId ) {
136+ notificationDisplayer.cancelNotification(
137+ DefaultNotificationDataFactory .FALLBACK_NOTIFICATION_TAG ,
138+ NotificationIdProvider .getFallbackNotificationId(sessionId),
139+ )
140+ clearSummaryNotificationIfNeeded(sessionId)
141+ }
142+
124143 /* *
125144 * Should be called when the application is currently opened and showing timeline for the given [roomId].
126145 * Used to ignore events related to that room (no need to display notification) and clean any existing notification on this room.
@@ -192,3 +211,30 @@ class DefaultNotificationDrawerManager(
192211 }
193212 }
194213}
214+
215+ /* *
216+ * Used to check if a notifiableEvent should be ignored based on the current application navigation state.
217+ */
218+ private fun AppNavigationState.shouldIgnoreEvent (event : NotifiableEvent ): Boolean {
219+ if (! isInForeground) return false
220+ return navigationState.currentSessionId() == event.sessionId &&
221+ when (event) {
222+ is NotifiableRingingCallEvent -> {
223+ // Never ignore ringing call notifications
224+ // Note that NotifiableRingingCallEvent are not handled by DefaultNotificationDrawerManager
225+ false
226+ }
227+ is FallbackNotifiableEvent -> {
228+ // Ignore if the room list is currently displayed
229+ navigationState is NavigationState .Session
230+ }
231+ is InviteNotifiableEvent ,
232+ is SimpleNotifiableEvent -> {
233+ event.roomId == navigationState.currentRoomId()
234+ }
235+ is NotifiableMessageEvent -> {
236+ event.roomId == navigationState.currentRoomId() &&
237+ event.threadId == navigationState.currentThreadId()
238+ }
239+ }
240+ }
0 commit comments