Skip to content

Commit d7e38cf

Browse files
committed
Fix some docs and do some cleanup
1 parent ca7d98b commit d7e38cf

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotificationDrawerManager.kt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package io.element.android.libraries.push.impl.notifications
1818

19+
import androidx.annotation.VisibleForTesting
1920
import androidx.core.app.NotificationManagerCompat
2021
import io.element.android.libraries.core.data.tryOrNull
2122
import io.element.android.libraries.core.log.logger.LoggerTag
2223
import io.element.android.libraries.di.AppScope
2324
import io.element.android.libraries.di.SingleIn
25+
import io.element.android.libraries.matrix.api.MatrixClient
2426
import io.element.android.libraries.matrix.api.MatrixClientProvider
2527
import io.element.android.libraries.matrix.api.core.EventId
2628
import io.element.android.libraries.matrix.api.core.RoomId
@@ -43,7 +45,7 @@ import javax.inject.Inject
4345
private val loggerTag = LoggerTag("DefaultNotificationDrawerManager", LoggerTag.NotificationLoggerTag)
4446

4547
/**
46-
* The NotificationDrawerManager receives notification events as they arrived (from event stream or fcm) and
48+
* The NotificationDrawerManager receives notification events as they arrive (from event stream or fcm) and
4749
* organise them in order to display them in the notification drawer.
4850
* Events can be grouped into the same notification, old (already read) events can be removed to do some cleaning.
4951
*/
@@ -72,7 +74,8 @@ class DefaultNotificationDrawerManager @Inject constructor(
7274
}
7375

7476
// For test only
75-
fun destroy() {
77+
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
78+
internal fun destroy() {
7679
appNavigationStateObserver?.cancel()
7780
}
7881

@@ -107,9 +110,7 @@ class DefaultNotificationDrawerManager @Inject constructor(
107110
}
108111

109112
/**
110-
* Should be called as soon as a new event is ready to be displayed.
111-
* The notification corresponding to this event will not be displayed until
112-
* #refreshNotificationDrawer() is called.
113+
* Should be called as soon as a new event is ready to be displayed, filtering out notifications that shouldn't be displayed.
113114
* Events might be grouped and there might not be one notification per event!
114115
*/
115116
suspend fun onNotifiableEventReceived(notifiableEvent: NotifiableEvent) {
@@ -120,23 +121,23 @@ class DefaultNotificationDrawerManager @Inject constructor(
120121
}
121122

122123
/**
123-
* Clear all known events and refresh the notification drawer.
124+
* Clear all known message events for a [sessionId].
124125
*/
125126
fun clearAllMessagesEvents(sessionId: SessionId) {
126127
notificationManager.cancel(null, notificationIdProvider.getRoomMessagesNotificationId(sessionId))
127128
clearSummaryNotificationIfNeeded(sessionId)
128129
}
129130

130131
/**
131-
* Clear all notifications related to the session and refresh the notification drawer.
132+
* Clear all notifications related to the session.
132133
*/
133134
fun clearAllEvents(sessionId: SessionId) {
134135
activeNotificationsProvider.getNotificationsForSession(sessionId)
135136
.forEach { notificationManager.cancel(it.tag, it.id) }
136137
}
137138

138139
/**
139-
* Should be called when the application is currently opened and showing timeline for the given roomId.
140+
* Should be called when the application is currently opened and showing timeline for the given [roomId].
140141
* Used to ignore events related to that room (no need to display notification) and clean any existing notification on this room.
141142
* Can also be called when a notification for this room is dismissed by the user.
142143
*/
@@ -192,35 +193,37 @@ class DefaultNotificationDrawerManager @Inject constructor(
192193
it.sessionId
193194
}
194195

195-
eventsForSessions.forEach { (sessionId, notifiableEvents) ->
196+
for ((sessionId, notifiableEvents) in eventsForSessions) {
196197
val client = matrixClientProvider.getOrRestore(sessionId).getOrThrow()
197198
val imageLoader = imageLoaderHolder.get(client)
198199
val userFromCache = client.userProfile.value
199200
val currentUser = if (userFromCache.avatarUrl != null && userFromCache.displayName.isNullOrEmpty().not()) {
200201
// We have an avatar and a display name, use it
201202
userFromCache
202203
} else {
203-
tryOrNull(
204-
onError = { Timber.tag(loggerTag.value).e(it, "Unable to retrieve info for user ${sessionId.value}") },
205-
operation = {
206-
client.getUserProfile().getOrNull()
207-
?.let {
208-
// displayName cannot be empty else NotificationCompat.MessagingStyle() will crash
209-
if (it.displayName.isNullOrEmpty()) {
210-
it.copy(displayName = sessionId.value)
211-
} else {
212-
it
213-
}
214-
}
215-
}
216-
) ?: MatrixUser(
217-
userId = sessionId,
218-
displayName = sessionId.value,
219-
avatarUrl = null
220-
)
204+
client.getSafeUserProfile()
221205
}
222206

223207
notificationRenderer.render(currentUser, useCompleteNotificationFormat, notifiableEvents, imageLoader)
224208
}
225209
}
210+
211+
private suspend fun MatrixClient.getSafeUserProfile(): MatrixUser {
212+
return tryOrNull(
213+
onError = { Timber.tag(loggerTag.value).e(it, "Unable to retrieve info for user ${sessionId.value}") },
214+
operation = {
215+
val profile = getUserProfile().getOrNull()
216+
// displayName cannot be empty else NotificationCompat.MessagingStyle() will crash
217+
if (profile?.displayName.isNullOrEmpty()) {
218+
profile?.copy(displayName = sessionId.value)
219+
} else {
220+
profile
221+
}
222+
}
223+
) ?: MatrixUser(
224+
userId = sessionId,
225+
displayName = sessionId.value,
226+
avatarUrl = null
227+
)
228+
}
226229
}

0 commit comments

Comments
 (0)