Merge pull request #6341 from artkoenig/develop

Fixed issues with reporting sync state events from different threads
This commit is contained in:
Benoit Marty 2022-07-25 10:21:12 +02:00 committed by GitHub
commit c28e7c86d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 30 additions and 32 deletions

1
changelog.d/6341.bugfix Normal file
View File

@ -0,0 +1 @@
Fixed issues with reporting sync state events from different threads

View File

@ -60,9 +60,9 @@ interface SyncService {
fun getSyncStateLive(): LiveData<SyncState> fun getSyncStateLive(): LiveData<SyncState>
/** /**
* Get the [SyncRequestState] as a LiveData. * Get the [SyncRequestState] as a SharedFlow.
*/ */
fun getSyncRequestStateLive(): LiveData<SyncRequestState> fun getSyncRequestStateFlow(): SharedFlow<SyncRequestState>
/** /**
* This method returns a flow of SyncResponse. New value will be pushed through the sync thread. * This method returns a flow of SyncResponse. New value will be pushed through the sync thread.

View File

@ -16,8 +16,6 @@
package org.matrix.android.sdk.internal.session.sync package org.matrix.android.sdk.internal.session.sync
import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.session.sync.SyncRequestState
import org.matrix.android.sdk.api.session.sync.SyncService import org.matrix.android.sdk.api.session.sync.SyncService
import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider import org.matrix.android.sdk.internal.di.WorkManagerProvider
@ -75,9 +73,7 @@ internal class DefaultSyncService @Inject constructor(
override fun getSyncState() = getSyncThread().currentState() override fun getSyncState() = getSyncThread().currentState()
override fun getSyncRequestStateLive(): LiveData<SyncRequestState> { override fun getSyncRequestStateFlow() = syncRequestStateTracker.syncRequestState
return syncRequestStateTracker.syncRequestState
}
override fun hasAlreadySynced(): Boolean { override fun hasAlreadySynced(): Boolean {
return syncTokenStore.getLastToken() != null return syncTokenStore.getLastToken() != null

View File

@ -16,23 +16,26 @@
package org.matrix.android.sdk.internal.session.sync package org.matrix.android.sdk.internal.session.sync
import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.sync.InitialSyncStep import org.matrix.android.sdk.api.session.sync.InitialSyncStep
import org.matrix.android.sdk.api.session.sync.SyncRequestState import org.matrix.android.sdk.api.session.sync.SyncRequestState
import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.android.sdk.internal.session.SessionScope
import javax.inject.Inject import javax.inject.Inject
@SessionScope @SessionScope
internal class SyncRequestStateTracker @Inject constructor() : internal class SyncRequestStateTracker @Inject constructor(
ProgressReporter { private val coroutineScope: CoroutineScope
) : ProgressReporter {
val syncRequestState = MutableLiveData<SyncRequestState>() val syncRequestState = MutableSharedFlow<SyncRequestState>()
private var rootTask: TaskInfo? = null private var rootTask: TaskInfo? = null
// Only to be used for incremental sync // Only to be used for incremental sync
fun setSyncRequestState(newSyncRequestState: SyncRequestState.IncrementalSyncRequestState) { fun setSyncRequestState(newSyncRequestState: SyncRequestState.IncrementalSyncRequestState) {
syncRequestState.postValue(newSyncRequestState) emitSyncState(newSyncRequestState)
} }
/** /**
@ -42,7 +45,9 @@ internal class SyncRequestStateTracker @Inject constructor() :
initialSyncStep: InitialSyncStep, initialSyncStep: InitialSyncStep,
totalProgress: Int totalProgress: Int
) { ) {
if (rootTask != null) {
endAll() endAll()
}
rootTask = TaskInfo(initialSyncStep, totalProgress, null, 1F) rootTask = TaskInfo(initialSyncStep, totalProgress, null, 1F)
reportProgress(0F) reportProgress(0F)
} }
@ -71,7 +76,7 @@ internal class SyncRequestStateTracker @Inject constructor() :
// Update the progress of the leaf and all its parents // Update the progress of the leaf and all its parents
leaf.setProgress(progress) leaf.setProgress(progress)
// Then update the live data using leaf wording and root progress // Then update the live data using leaf wording and root progress
syncRequestState.postValue(SyncRequestState.InitialSyncProgressing(leaf.initialSyncStep, root.currentProgress.toInt())) emitSyncState(SyncRequestState.InitialSyncProgressing(leaf.initialSyncStep, root.currentProgress.toInt()))
} }
} }
} }
@ -86,13 +91,19 @@ internal class SyncRequestStateTracker @Inject constructor() :
// And close it // And close it
endedTask.parent.child = null endedTask.parent.child = null
} else { } else {
syncRequestState.postValue(SyncRequestState.Idle) emitSyncState(SyncRequestState.Idle)
} }
} }
} }
fun endAll() { fun endAll() {
rootTask = null rootTask = null
syncRequestState.postValue(SyncRequestState.Idle) emitSyncState(SyncRequestState.Idle)
}
private fun emitSyncState(state: SyncRequestState) {
coroutineScope.launch {
syncRequestState.emit(state)
}
} }
} }

View File

@ -18,7 +18,6 @@ package im.vector.app
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.asFlow
import arrow.core.Option import arrow.core.Option
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.utils.BehaviorDataSource import im.vector.app.core.utils.BehaviorDataSource
@ -114,8 +113,7 @@ class AppStateHandler @Inject constructor(
} }
private fun observeSyncStatus(session: Session) { private fun observeSyncStatus(session: Session) {
session.syncService().getSyncRequestStateLive() session.syncService().getSyncRequestStateFlow()
.asFlow()
.filterIsInstance<SyncRequestState.IncrementalSyncDone>() .filterIsInstance<SyncRequestState.IncrementalSyncDone>()
.map { session.spaceService().getRootSpaceSummaries().size } .map { session.spaceService().getRootSpaceSummaries().size }
.distinctUntilChanged() .distinctUntilChanged()

View File

@ -17,7 +17,6 @@
package im.vector.app package im.vector.app
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.lifecycle.asFlow
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.features.rageshake.BugReporter import im.vector.app.features.rageshake.BugReporter
import im.vector.app.features.rageshake.ReportType import im.vector.app.features.rageshake.ReportType
@ -261,8 +260,7 @@ class AutoRageShaker @Inject constructor(
this.currentActiveSessionId = sessionId this.currentActiveSessionId = sessionId
hasSynced = session.syncService().hasAlreadySynced() hasSynced = session.syncService().hasAlreadySynced()
session.syncService().getSyncRequestStateLive() session.syncService().getSyncRequestStateFlow()
.asFlow()
.onEach { .onEach {
hasSynced = it !is SyncRequestState.InitialSyncProgressing hasSynced = it !is SyncRequestState.InitialSyncProgressing
} }

View File

@ -16,7 +16,6 @@
package im.vector.app.features.analytics.accountdata package im.vector.app.features.analytics.accountdata
import androidx.lifecycle.asFlow
import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
@ -66,7 +65,7 @@ class AnalyticsAccountDataViewModel @AssistedInject constructor(
private fun observeInitSync() { private fun observeInitSync() {
combine( combine(
session.syncService().getSyncRequestStateLive().asFlow(), session.syncService().getSyncRequestStateFlow(),
analytics.getUserConsent(), analytics.getUserConsent(),
analytics.getAnalyticsId() analytics.getAnalyticsId()
) { status, userConsent, analyticsId -> ) { status, userConsent, analyticsId ->

View File

@ -16,7 +16,6 @@
package im.vector.app.features.home package im.vector.app.features.home
import androidx.lifecycle.asFlow
import com.airbnb.mvrx.Mavericks import com.airbnb.mvrx.Mavericks
import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.ViewModelContext import com.airbnb.mvrx.ViewModelContext
@ -218,8 +217,7 @@ class HomeActivityViewModel @AssistedInject constructor(
private fun observeInitialSync() { private fun observeInitialSync() {
val session = activeSessionHolder.getSafeActiveSession() ?: return val session = activeSessionHolder.getSafeActiveSession() ?: return
session.syncService().getSyncRequestStateLive() session.syncService().getSyncRequestStateFlow()
.asFlow()
.onEach { status -> .onEach { status ->
when (status) { when (status) {
is SyncRequestState.Idle -> { is SyncRequestState.Idle -> {

View File

@ -198,8 +198,7 @@ class HomeDetailViewModel @AssistedInject constructor(
copy(syncState = syncState) copy(syncState = syncState)
} }
session.syncService().getSyncRequestStateLive() session.syncService().getSyncRequestStateFlow()
.asFlow()
.filterIsInstance<SyncRequestState.IncrementalSyncRequestState>() .filterIsInstance<SyncRequestState.IncrementalSyncRequestState>()
.setOnEach { .setOnEach {
copy(incrementalSyncRequestState = it) copy(incrementalSyncRequestState = it)

View File

@ -18,7 +18,6 @@ package im.vector.app.features.home.room.detail
import android.net.Uri import android.net.Uri
import androidx.annotation.IdRes import androidx.annotation.IdRes
import androidx.lifecycle.asFlow
import com.airbnb.mvrx.Async import com.airbnb.mvrx.Async
import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
@ -1152,8 +1151,7 @@ class TimelineViewModel @AssistedInject constructor(
copy(syncState = syncState) copy(syncState = syncState)
} }
session.syncService().getSyncRequestStateLive() session.syncService().getSyncRequestStateFlow()
.asFlow()
.filterIsInstance<SyncRequestState.IncrementalSyncRequestState>() .filterIsInstance<SyncRequestState.IncrementalSyncRequestState>()
.setOnEach { .setOnEach {
copy(incrementalSyncRequestState = it) copy(incrementalSyncRequestState = it)