-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Splitting notifications into separate creation and displaying passes #4185
Closed
ouchadam
wants to merge
10
commits into
feature/adm/immutable-notifiable-models
from
feature/adm/notifications-creation-display-split
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3589205
making the event body non null and immutable to allow less cases to b…
ouchadam c1db6c6
creating dedicated class for the processing the serialized events
ouchadam 3b0d4b7
creating the notifications separate to where they're displayed
ouchadam 7ad20df
chaining the event process, notification creation and display logic i…
ouchadam fc203ad
lifting settings change to cancel all notifications out of the renderer
ouchadam b55f158
removing no longer needed hasBeenDisplayed state, the eventList is ou…
ouchadam 9418c85
handling creating the summary when notification events are filtered t…
ouchadam cc90218
removing this usages for project convention
ouchadam ee12c15
formatting
ouchadam 1c35109
ensuring that we removing the summary group before removing individua…
ouchadam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
vector/src/main/java/im/vector/app/features/notifications/NotifiableEventProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2021 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.vector.app.features.notifications | ||
|
||
import im.vector.app.features.invite.AutoAcceptInvites | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class NotifiableEventProcessor @Inject constructor( | ||
private val outdatedDetector: OutdatedEventDetector, | ||
private val autoAcceptInvites: AutoAcceptInvites | ||
) { | ||
|
||
fun modifyAndProcess(eventList: MutableList<NotifiableEvent>, currentRoomId: String?): ProcessedNotificationEvents { | ||
val roomIdToEventMap: MutableMap<String, MutableList<NotifiableMessageEvent>> = LinkedHashMap() | ||
val simpleEvents: MutableMap<String, SimpleNotifiableEvent?> = LinkedHashMap() | ||
val invitationEvents: MutableMap<String, InviteNotifiableEvent?> = LinkedHashMap() | ||
|
||
val eventIterator = eventList.listIterator() | ||
while (eventIterator.hasNext()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can further improve this by only calculating the diff between what we've rendered and the new |
||
when (val event = eventIterator.next()) { | ||
is NotifiableMessageEvent -> { | ||
val roomId = event.roomId | ||
val roomEvents = roomIdToEventMap.getOrPut(roomId) { ArrayList() } | ||
|
||
// should we limit to last 7 messages per room? | ||
if (shouldIgnoreMessageEventInRoom(currentRoomId, roomId) || outdatedDetector.isMessageOutdated(event)) { | ||
// forget this event | ||
eventIterator.remove() | ||
} else { | ||
roomEvents.add(event) | ||
} | ||
} | ||
is InviteNotifiableEvent -> { | ||
if (autoAcceptInvites.hideInvites) { | ||
// Forget this event | ||
eventIterator.remove() | ||
invitationEvents[event.roomId] = null | ||
} else { | ||
invitationEvents[event.roomId] = event | ||
} | ||
} | ||
is SimpleNotifiableEvent -> simpleEvents[event.eventId] = event | ||
else -> Timber.w("Type not handled") | ||
} | ||
} | ||
return ProcessedNotificationEvents(roomIdToEventMap, simpleEvents, invitationEvents) | ||
} | ||
|
||
private fun shouldIgnoreMessageEventInRoom(currentRoomId: String?, roomId: String?): Boolean { | ||
return currentRoomId != null && roomId == currentRoomId | ||
} | ||
} | ||
|
||
data class ProcessedNotificationEvents( | ||
val roomEvents: Map<String, List<NotifiableMessageEvent>>, | ||
val simpleEvents: Map<String, SimpleNotifiableEvent?>, | ||
val invitationEvents: Map<String, InviteNotifiableEvent?> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
vector/src/main/java/im/vector/app/features/notifications/NotificationDisplayer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2021 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.vector.app.features.notifications | ||
|
||
import android.app.Notification | ||
import android.content.Context | ||
import androidx.core.app.NotificationManagerCompat | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class NotificationDisplayer @Inject constructor(context: Context) { | ||
|
||
private val notificationManager = NotificationManagerCompat.from(context) | ||
|
||
fun showNotificationMessage(tag: String?, id: Int, notification: Notification) { | ||
notificationManager.notify(tag, id, notification) | ||
} | ||
|
||
fun cancelNotificationMessage(tag: String?, id: Int) { | ||
notificationManager.cancel(tag, id) | ||
} | ||
|
||
fun cancelAllNotifications() { | ||
// Keep this try catch (reported by GA) | ||
try { | ||
notificationManager.cancelAll() | ||
} catch (e: Exception) { | ||
Timber.e(e, "## cancelAllNotifications() failed") | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this loop extracted as class
https://github.com/vector-im/element-android/blob/b71e2de9acf611d4a50703aed458d1e022d2b105/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt#L248
it's processing the events by filtering out of date/already read or auto accepted invites