Sync: fix liveState not initialized and add method to get current state without livedata

This commit is contained in:
ganfra 2020-07-01 12:10:12 +02:00
parent 73ce38c6a9
commit faeeec0e37
4 changed files with 13 additions and 5 deletions

View File

@ -127,6 +127,12 @@ interface Session :
*/
fun getSyncStateLive(): LiveData<SyncState>
/**
* This method returns the current sync state.
* @return the current [SyncState].
*/
fun getSyncState(): SyncState
/**
* This methods return true if an initial sync has been processed
*/

View File

@ -197,9 +197,9 @@ internal class DefaultSession @Inject constructor(
eventBus.unregister(this)
}
override fun getSyncStateLive(): LiveData<SyncState> {
return getSyncThread().liveState()
}
override fun getSyncStateLive() = getSyncThread().liveState()
override fun getSyncState() = getSyncThread().currentState()
override fun hasAlreadySynced(): Boolean {
return syncTokenStore.getLastToken() != null

View File

@ -104,7 +104,7 @@ abstract class SyncService : Service() {
try {
syncTask.execute(params)
// Start sync if we were doing an initial sync and the syncThread is not launched yet
if (isInitialSync && session.getSyncStateLive().value == SyncState.Idle) {
if (isInitialSync && session.getSyncState() == SyncState.Idle) {
val isForeground = !backgroundDetectionObserver.isInBackground
session.startSync(isForeground)
}

View File

@ -51,7 +51,7 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
: Thread(), NetworkConnectivityChecker.Listener, BackgroundDetectionObserver.Listener {
private var state: SyncState = SyncState.Idle
private var liveState = MutableLiveData<SyncState>()
private var liveState = MutableLiveData<SyncState>(state)
private val lock = Object()
private val syncScope = CoroutineScope(SupervisorJob())
private val debouncer = Debouncer(createUIHandler())
@ -98,6 +98,8 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
lock.notify()
}
fun currentState() = state
fun liveState(): LiveData<SyncState> {
return liveState
}