Skip to content

Commit c28e7c8

Browse files
authored
Merge pull request #6341 from artkoenig/develop
Fixed issues with reporting sync state events from different threads
2 parents 75de805 + 03da067 commit c28e7c8

File tree

10 files changed

+30
-32
lines changed

10 files changed

+30
-32
lines changed

changelog.d/6341.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issues with reporting sync state events from different threads

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/sync/SyncService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ interface SyncService {
6060
fun getSyncStateLive(): LiveData<SyncState>
6161

6262
/**
63-
* Get the [SyncRequestState] as a LiveData.
63+
* Get the [SyncRequestState] as a SharedFlow.
6464
*/
65-
fun getSyncRequestStateLive(): LiveData<SyncRequestState>
65+
fun getSyncRequestStateFlow(): SharedFlow<SyncRequestState>
6666

6767
/**
6868
* This method returns a flow of SyncResponse. New value will be pushed through the sync thread.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/DefaultSyncService.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.matrix.android.sdk.internal.session.sync
1818

19-
import androidx.lifecycle.LiveData
20-
import org.matrix.android.sdk.api.session.sync.SyncRequestState
2119
import org.matrix.android.sdk.api.session.sync.SyncService
2220
import org.matrix.android.sdk.internal.di.SessionId
2321
import org.matrix.android.sdk.internal.di.WorkManagerProvider
@@ -75,9 +73,7 @@ internal class DefaultSyncService @Inject constructor(
7573

7674
override fun getSyncState() = getSyncThread().currentState()
7775

78-
override fun getSyncRequestStateLive(): LiveData<SyncRequestState> {
79-
return syncRequestStateTracker.syncRequestState
80-
}
76+
override fun getSyncRequestStateFlow() = syncRequestStateTracker.syncRequestState
8177

8278
override fun hasAlreadySynced(): Boolean {
8379
return syncTokenStore.getLastToken() != null

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/SyncRequestStateTracker.kt

+20-9
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,26 @@
1616

1717
package org.matrix.android.sdk.internal.session.sync
1818

19-
import androidx.lifecycle.MutableLiveData
19+
import kotlinx.coroutines.CoroutineScope
20+
import kotlinx.coroutines.flow.MutableSharedFlow
21+
import kotlinx.coroutines.launch
2022
import org.matrix.android.sdk.api.session.sync.InitialSyncStep
2123
import org.matrix.android.sdk.api.session.sync.SyncRequestState
2224
import org.matrix.android.sdk.internal.session.SessionScope
2325
import javax.inject.Inject
2426

2527
@SessionScope
26-
internal class SyncRequestStateTracker @Inject constructor() :
27-
ProgressReporter {
28+
internal class SyncRequestStateTracker @Inject constructor(
29+
private val coroutineScope: CoroutineScope
30+
) : ProgressReporter {
2831

29-
val syncRequestState = MutableLiveData<SyncRequestState>()
32+
val syncRequestState = MutableSharedFlow<SyncRequestState>()
3033

3134
private var rootTask: TaskInfo? = null
3235

3336
// Only to be used for incremental sync
3437
fun setSyncRequestState(newSyncRequestState: SyncRequestState.IncrementalSyncRequestState) {
35-
syncRequestState.postValue(newSyncRequestState)
38+
emitSyncState(newSyncRequestState)
3639
}
3740

3841
/**
@@ -42,7 +45,9 @@ internal class SyncRequestStateTracker @Inject constructor() :
4245
initialSyncStep: InitialSyncStep,
4346
totalProgress: Int
4447
) {
45-
endAll()
48+
if (rootTask != null) {
49+
endAll()
50+
}
4651
rootTask = TaskInfo(initialSyncStep, totalProgress, null, 1F)
4752
reportProgress(0F)
4853
}
@@ -71,7 +76,7 @@ internal class SyncRequestStateTracker @Inject constructor() :
7176
// Update the progress of the leaf and all its parents
7277
leaf.setProgress(progress)
7378
// Then update the live data using leaf wording and root progress
74-
syncRequestState.postValue(SyncRequestState.InitialSyncProgressing(leaf.initialSyncStep, root.currentProgress.toInt()))
79+
emitSyncState(SyncRequestState.InitialSyncProgressing(leaf.initialSyncStep, root.currentProgress.toInt()))
7580
}
7681
}
7782
}
@@ -86,13 +91,19 @@ internal class SyncRequestStateTracker @Inject constructor() :
8691
// And close it
8792
endedTask.parent.child = null
8893
} else {
89-
syncRequestState.postValue(SyncRequestState.Idle)
94+
emitSyncState(SyncRequestState.Idle)
9095
}
9196
}
9297
}
9398

9499
fun endAll() {
95100
rootTask = null
96-
syncRequestState.postValue(SyncRequestState.Idle)
101+
emitSyncState(SyncRequestState.Idle)
102+
}
103+
104+
private fun emitSyncState(state: SyncRequestState) {
105+
coroutineScope.launch {
106+
syncRequestState.emit(state)
107+
}
97108
}
98109
}

vector/src/main/java/im/vector/app/AppStateHandler.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package im.vector.app
1818

1919
import androidx.lifecycle.DefaultLifecycleObserver
2020
import androidx.lifecycle.LifecycleOwner
21-
import androidx.lifecycle.asFlow
2221
import arrow.core.Option
2322
import im.vector.app.core.di.ActiveSessionHolder
2423
import im.vector.app.core.utils.BehaviorDataSource
@@ -114,8 +113,7 @@ class AppStateHandler @Inject constructor(
114113
}
115114

116115
private fun observeSyncStatus(session: Session) {
117-
session.syncService().getSyncRequestStateLive()
118-
.asFlow()
116+
session.syncService().getSyncRequestStateFlow()
119117
.filterIsInstance<SyncRequestState.IncrementalSyncDone>()
120118
.map { session.spaceService().getRootSpaceSummaries().size }
121119
.distinctUntilChanged()

vector/src/main/java/im/vector/app/AutoRageShaker.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package im.vector.app
1818

1919
import android.content.SharedPreferences
20-
import androidx.lifecycle.asFlow
2120
import im.vector.app.core.di.ActiveSessionHolder
2221
import im.vector.app.features.rageshake.BugReporter
2322
import im.vector.app.features.rageshake.ReportType
@@ -261,8 +260,7 @@ class AutoRageShaker @Inject constructor(
261260
this.currentActiveSessionId = sessionId
262261

263262
hasSynced = session.syncService().hasAlreadySynced()
264-
session.syncService().getSyncRequestStateLive()
265-
.asFlow()
263+
session.syncService().getSyncRequestStateFlow()
266264
.onEach {
267265
hasSynced = it !is SyncRequestState.InitialSyncProgressing
268266
}

vector/src/main/java/im/vector/app/features/analytics/accountdata/AnalyticsAccountDataViewModel.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package im.vector.app.features.analytics.accountdata
1818

19-
import androidx.lifecycle.asFlow
2019
import com.airbnb.mvrx.MavericksViewModelFactory
2120
import dagger.assisted.Assisted
2221
import dagger.assisted.AssistedFactory
@@ -66,7 +65,7 @@ class AnalyticsAccountDataViewModel @AssistedInject constructor(
6665

6766
private fun observeInitSync() {
6867
combine(
69-
session.syncService().getSyncRequestStateLive().asFlow(),
68+
session.syncService().getSyncRequestStateFlow(),
7069
analytics.getUserConsent(),
7170
analytics.getAnalyticsId()
7271
) { status, userConsent, analyticsId ->

vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package im.vector.app.features.home
1818

19-
import androidx.lifecycle.asFlow
2019
import com.airbnb.mvrx.Mavericks
2120
import com.airbnb.mvrx.MavericksViewModelFactory
2221
import com.airbnb.mvrx.ViewModelContext
@@ -218,8 +217,7 @@ class HomeActivityViewModel @AssistedInject constructor(
218217
private fun observeInitialSync() {
219218
val session = activeSessionHolder.getSafeActiveSession() ?: return
220219

221-
session.syncService().getSyncRequestStateLive()
222-
.asFlow()
220+
session.syncService().getSyncRequestStateFlow()
223221
.onEach { status ->
224222
when (status) {
225223
is SyncRequestState.Idle -> {

vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ class HomeDetailViewModel @AssistedInject constructor(
198198
copy(syncState = syncState)
199199
}
200200

201-
session.syncService().getSyncRequestStateLive()
202-
.asFlow()
201+
session.syncService().getSyncRequestStateFlow()
203202
.filterIsInstance<SyncRequestState.IncrementalSyncRequestState>()
204203
.setOnEach {
205204
copy(incrementalSyncRequestState = it)

vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package im.vector.app.features.home.room.detail
1818

1919
import android.net.Uri
2020
import androidx.annotation.IdRes
21-
import androidx.lifecycle.asFlow
2221
import com.airbnb.mvrx.Async
2322
import com.airbnb.mvrx.Fail
2423
import com.airbnb.mvrx.Loading
@@ -1152,8 +1151,7 @@ class TimelineViewModel @AssistedInject constructor(
11521151
copy(syncState = syncState)
11531152
}
11541153

1155-
session.syncService().getSyncRequestStateLive()
1156-
.asFlow()
1154+
session.syncService().getSyncRequestStateFlow()
11571155
.filterIsInstance<SyncRequestState.IncrementalSyncRequestState>()
11581156
.setOnEach {
11591157
copy(incrementalSyncRequestState = it)

0 commit comments

Comments
 (0)