Skip to content

Commit

Permalink
Merge pull request #1257 from vector-im/feature/fga/sync_indicator_api
Browse files Browse the repository at this point in the history
Feature/fga/sync indicator api
  • Loading branch information
bmarty authored Sep 8, 2023
2 parents 98226ab + 0e1d61b commit 5a0e0a8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ package io.element.android.appnav.loggedin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import io.element.android.features.networkmonitor.api.NetworkMonitor
import io.element.android.features.networkmonitor.api.NetworkStatus
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.roomlist.RoomListService
import io.element.android.libraries.push.api.PushService
import kotlinx.coroutines.delay
import javax.inject.Inject

private const val DELAY_BEFORE_SHOWING_SYNC_SPINNER_IN_MILLIS = 1500L

class LoggedInPresenter @Inject constructor(
private val matrixClient: MatrixClient,
private val networkMonitor: NetworkMonitor,
Expand All @@ -50,19 +46,11 @@ class LoggedInPresenter @Inject constructor(
pushService.registerWith(matrixClient, pushProvider, distributor)
}

val roomListState by matrixClient.roomListService.state.collectAsState()
val syncIndicator by matrixClient.roomListService.syncIndicator.collectAsState()
val networkStatus by networkMonitor.connectivity.collectAsState()
var showSyncSpinner by remember {
mutableStateOf(false)
}
LaunchedEffect(roomListState, networkStatus) {
showSyncSpinner = when {
networkStatus == NetworkStatus.Offline -> false
roomListState == RoomListService.State.Running -> false
else -> {
delay(DELAY_BEFORE_SHOWING_SYNC_SPINNER_IN_MILLIS)
true
}
val showSyncSpinner by remember {
derivedStateOf {
networkStatus == NetworkStatus.Online && syncIndicator == RoomListService.SyncIndicator.Show
}
}
return LoggedInState(
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1244.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the new SyncIndicator API.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ interface RoomListService {
data object Terminated : State()
}

sealed class SyncIndicator {
data object Show : SyncIndicator()
data object Hide : SyncIndicator()
}

/**
* returns a [RoomList] object of all rooms we want to display.
* This will exclude some rooms like the invites, or spaces.
Expand All @@ -49,6 +54,11 @@ interface RoomListService {
*/
fun updateAllRoomsVisibleRange(range: IntRange)

/**
* The sync indicator as a flow.
*/
val syncIndicator: StateFlow<SyncIndicator>

/**
* The state of the service as a flow.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import org.matrix.rustcomponents.sdk.RoomListLoadingStateListener
import org.matrix.rustcomponents.sdk.RoomListService
import org.matrix.rustcomponents.sdk.RoomListServiceState
import org.matrix.rustcomponents.sdk.RoomListServiceStateListener
import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicator
import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicatorListener
import timber.log.Timber

fun RoomList.loadingStateFlow(): Flow<RoomListLoadingState> =
Expand Down Expand Up @@ -83,6 +85,18 @@ fun RoomListService.stateFlow(): Flow<RoomListServiceState> =
}
}.buffer(Channel.UNLIMITED)

fun RoomListService.syncIndicator(): Flow<RoomListServiceSyncIndicator> =
mxCallbackFlow {
val listener = object : RoomListServiceSyncIndicatorListener {
override fun onUpdate(syncIndicator: RoomListServiceSyncIndicator) {
trySendBlocking(syncIndicator)
}
}
tryOrNull {
syncIndicator(listener)
}
}.buffer(Channel.UNLIMITED)

fun RoomListService.roomOrNull(roomId: String): RoomListItem? {
return try {
room(roomId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.matrix.rustcomponents.sdk.RoomListInput
import org.matrix.rustcomponents.sdk.RoomListLoadingState
import org.matrix.rustcomponents.sdk.RoomListRange
import org.matrix.rustcomponents.sdk.RoomListServiceState
import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicator
import timber.log.Timber
import org.matrix.rustcomponents.sdk.RoomListService as InnerRustRoomListService

Expand Down Expand Up @@ -106,6 +107,15 @@ class RustRoomListService(
}
}

override val syncIndicator: StateFlow<RoomListService.SyncIndicator> =
innerRoomListService.syncIndicator()
.map { it.toSyncIndicator() }
.onEach { syncIndicator ->
Timber.d("SyncIndicator = $syncIndicator")
}
.distinctUntilChanged()
.stateIn(sessionCoroutineScope, SharingStarted.Eagerly, RoomListService.SyncIndicator.Hide)

override val state: StateFlow<RoomListService.State> =
innerRoomListService.stateFlow()
.map { it.toRoomListState() }
Expand Down Expand Up @@ -134,6 +144,13 @@ private fun RoomListServiceState.toRoomListState(): RoomListService.State {
}
}

private fun RoomListServiceSyncIndicator.toSyncIndicator(): RoomListService.SyncIndicator {
return when (this) {
RoomListServiceSyncIndicator.SHOW -> RoomListService.SyncIndicator.Show
RoomListServiceSyncIndicator.HIDE -> RoomListService.SyncIndicator.Hide
}
}

private fun org.matrix.rustcomponents.sdk.RoomList.observeEntriesWithProcessor(processor: RoomSummaryListProcessor): Flow<List<RoomListEntriesUpdate>> {
return entriesFlow { roomListEntries ->
processor.postEntries(roomListEntries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FakeRoomListService : RoomListService {
private val allRoomsLoadingStateFlow = MutableStateFlow<RoomList.LoadingState>(RoomList.LoadingState.NotLoaded)
private val inviteRoomsLoadingStateFlow = MutableStateFlow<RoomList.LoadingState>(RoomList.LoadingState.NotLoaded)
private val roomListStateFlow = MutableStateFlow<RoomListService.State>(RoomListService.State.Idle)
private val syncIndicatorStateFlow = MutableStateFlow<RoomListService.SyncIndicator>(RoomListService.SyncIndicator.Hide)

suspend fun postAllRooms(roomSummaries: List<RoomSummary>) {
allRoomSummariesFlow.emit(roomSummaries)
Expand Down Expand Up @@ -72,4 +73,6 @@ class FakeRoomListService : RoomListService {
}

override val state: StateFlow<RoomListService.State> = roomListStateFlow

override val syncIndicator: StateFlow<RoomListService.SyncIndicator> = syncIndicatorStateFlow
}

0 comments on commit 5a0e0a8

Please sign in to comment.