mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-10-31 16:22:49 +00:00
Add MatrixClient.totalUnreadCountFlow() extension
Extract the logic to observe the total unread count of a MatrixClient, refreshed on each sync heartbeat, to a shared extension and use it in HomePresenter and PreferencesRootPresenter. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+21
@@ -51,9 +51,15 @@ import io.element.android.libraries.matrix.api.user.UserStatus
|
||||
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import java.util.Optional
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
/**
|
||||
* Entry point for everything scoped to a single logged in session: the sub-services, room access, and account level operations.
|
||||
@@ -523,3 +529,18 @@ fun MatrixClient.roomAliasFromName(name: String): RoomAlias? {
|
||||
?.takeIf { MatrixPatterns.isRoomAlias(it) }
|
||||
?.let { tryOrNull { RoomAlias(it) } }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a flow of the total number of unread notifications across all rooms.
|
||||
* The first value is emitted immediately, then the value is refreshed on each sync heartbeat.
|
||||
* In case of error, 0 is emitted.
|
||||
*/
|
||||
@OptIn(FlowPreview::class)
|
||||
fun MatrixClient.totalUnreadCountFlow(): Flow<Int> {
|
||||
return syncService.heartBeat
|
||||
// Delay the heartbeat so the SDK has time to update the unread count before we read it.
|
||||
.debounce(100.milliseconds)
|
||||
.onStart { emit(Unit) }
|
||||
.map { totalUnreadCount().getOrDefault(0).toInt() }
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user