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> 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 * 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) eventBus.unregister(this)
} }
override fun getSyncStateLive(): LiveData<SyncState> { override fun getSyncStateLive() = getSyncThread().liveState()
return getSyncThread().liveState()
} override fun getSyncState() = getSyncThread().currentState()
override fun hasAlreadySynced(): Boolean { override fun hasAlreadySynced(): Boolean {
return syncTokenStore.getLastToken() != null return syncTokenStore.getLastToken() != null

View file

@ -104,7 +104,7 @@ abstract class SyncService : Service() {
try { try {
syncTask.execute(params) syncTask.execute(params)
// Start sync if we were doing an initial sync and the syncThread is not launched yet // 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 val isForeground = !backgroundDetectionObserver.isInBackground
session.startSync(isForeground) session.startSync(isForeground)
} }

View file

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