Skip to content

Commit

Permalink
Fix manual sync is not triggered, issue #2492 (#2507)
Browse files Browse the repository at this point in the history
* refactor demo project

* refactor demo project

* splitting the flow into 2 state

* Spotless

* fixed manual is not triggered

* fixed manual is not triggered

* spotless apply

---------

Co-authored-by: Jing Tang <jingtang@google.com>
  • Loading branch information
hoangchungk53qx1 and jingtang10 committed May 1, 2024
1 parent 56339b4 commit 12d0762
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch

/** View model for [MainActivity]. */
@OptIn(ExperimentalCoroutinesApi::class)
Expand All @@ -46,7 +49,11 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
val lastSyncTimestampLiveData: LiveData<String>
get() = _lastSyncTimestampLiveData

private val _oneTimeSyncTrigger = MutableStateFlow(false)
private val _oneTimeSyncTrigger =
MutableSharedFlow<Boolean>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)

val pollPeriodicSyncJobStatus: SharedFlow<PeriodicSyncJobStatus> =
Sync.periodicSync<DemoFhirSyncWorker>(
Expand All @@ -61,15 +68,14 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica

val pollState: SharedFlow<CurrentSyncJobStatus> =
_oneTimeSyncTrigger
.combine(
flow = Sync.oneTimeSync<DemoFhirSyncWorker>(context = application.applicationContext),
) { _, syncJobStatus ->
syncJobStatus
.flatMapLatest {
Sync.oneTimeSync<DemoFhirSyncWorker>(context = application.applicationContext)
}
.map { it }
.shareIn(viewModelScope, SharingStarted.Eagerly, 0)

fun triggerOneTimeSync() {
_oneTimeSyncTrigger.value = !_oneTimeSyncTrigger.value
viewModelScope.launch { _oneTimeSyncTrigger.emit(true) }
}

/** Emits last sync time. */
Expand Down

0 comments on commit 12d0762

Please sign in to comment.