Enable Offline mode of the SyncService, so that the sync starts automatically when the network is back.
Also rely on the sync state to render the "Offline" banner.
This commit is contained in:
parent
f4afda119b
commit
f84aa03605
20 changed files with 91 additions and 74 deletions
|
|
@ -14,3 +14,11 @@ enum class SyncState {
|
|||
Terminated,
|
||||
Offline,
|
||||
}
|
||||
|
||||
fun SyncState.isConnected() = when (this) {
|
||||
SyncState.Idle,
|
||||
SyncState.Running,
|
||||
SyncState.Error,
|
||||
SyncState.Terminated -> true
|
||||
SyncState.Offline -> false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ class RustMatrixClientFactory @Inject constructor(
|
|||
|
||||
val syncService = client.syncService()
|
||||
.withUtdHook(UtdTracker(analyticsService))
|
||||
.withOfflineMode()
|
||||
.finish()
|
||||
|
||||
return RustMatrixClient(
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import io.element.android.libraries.matrix.api.notificationsettings.Notification
|
|||
import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
|
||||
import io.element.android.libraries.matrix.api.roomdirectory.RoomDirectoryService
|
||||
import io.element.android.libraries.matrix.api.roomlist.RoomListService
|
||||
import io.element.android.libraries.matrix.api.sync.SyncService
|
||||
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
|
|
@ -45,6 +46,11 @@ object SessionMatrixModule {
|
|||
return matrixClient.roomListService
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesSyncService(matrixClient: MatrixClient): SyncService {
|
||||
return matrixClient.syncService()
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun providesEncryptionService(matrixClient: MatrixClient): EncryptionService {
|
||||
return matrixClient.encryptionService()
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ import org.matrix.rustcomponents.sdk.UnableToDecryptDelegate
|
|||
|
||||
class FakeRustSyncServiceBuilder : SyncServiceBuilder(NoPointer) {
|
||||
override suspend fun withUtdHook(delegate: UnableToDecryptDelegate): SyncServiceBuilder = this
|
||||
override fun withOfflineMode(): SyncServiceBuilder = this
|
||||
override suspend fun finish(): SyncService = FakeRustSyncService()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
class FakeSyncService(
|
||||
syncStateFlow: MutableStateFlow<SyncState> = MutableStateFlow(SyncState.Idle)
|
||||
initialSyncState: SyncState = SyncState.Idle,
|
||||
) : SyncService {
|
||||
private val syncStateFlow: MutableStateFlow<SyncState> = MutableStateFlow(initialSyncState)
|
||||
|
||||
var startSyncLambda: () -> Result<Unit> = { Result.success(Unit) }
|
||||
override suspend fun startSync(): Result<Unit> {
|
||||
return startSyncLambda()
|
||||
|
|
@ -26,4 +28,8 @@ class FakeSyncService(
|
|||
}
|
||||
|
||||
override val syncState: StateFlow<SyncState> = syncStateFlow
|
||||
|
||||
suspend fun emitSyncState(syncState: SyncState) {
|
||||
syncStateFlow.emit(syncState)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ import org.junit.Test
|
|||
|
||||
class SyncOnNotifiableEventTest {
|
||||
private val timelineItems = MutableStateFlow<List<MatrixTimelineItem>>(emptyList())
|
||||
private val syncStateFlow = MutableStateFlow(SyncState.Idle)
|
||||
private val startSyncLambda = lambdaRecorder<Result<Unit>> { Result.success(Unit) }
|
||||
private val stopSyncLambda = lambdaRecorder<Result<Unit>> { Result.success(Unit) }
|
||||
private val subscribeToSyncLambda = lambdaRecorder<Unit> { }
|
||||
|
|
@ -49,7 +48,7 @@ class SyncOnNotifiableEventTest {
|
|||
liveTimeline = liveTimeline,
|
||||
subscribeToSyncLambda = subscribeToSyncLambda
|
||||
)
|
||||
private val syncService = FakeSyncService(syncStateFlow).also {
|
||||
private val syncService = FakeSyncService(SyncState.Idle).also {
|
||||
it.startSyncLambda = startSyncLambda
|
||||
it.stopSyncLambda = stopSyncLambda
|
||||
}
|
||||
|
|
@ -115,7 +114,7 @@ class SyncOnNotifiableEventTest {
|
|||
timelineItems.emit(
|
||||
listOf(MatrixTimelineItem.Event(A_UNIQUE_ID, anEventTimelineItem()))
|
||||
)
|
||||
syncStateFlow.emit(SyncState.Running)
|
||||
syncService.emitSyncState(SyncState.Running)
|
||||
sut(notifiableEvent)
|
||||
|
||||
assert(startSyncLambda).isCalledOnce()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue