Consume Flows from WorkManager for work status

Change-Id: Ia8002a7b74405de51a37921d3aaec91fc92d3773
pull/826/head
TJ Dahunsi 2 years ago
parent 89c4653115
commit ac56e742a4

@ -27,7 +27,7 @@ androidxTestRunner = "1.5.1"
androidxTracing = "1.1.0" androidxTracing = "1.1.0"
androidxUiAutomator = "2.2.0" androidxUiAutomator = "2.2.0"
androidxWindowManager = "1.0.0" androidxWindowManager = "1.0.0"
androidxWork = "2.7.1" androidxWork = "2.9.0-alpha01"
coil = "2.2.2" coil = "2.2.2"
firebaseBom = "31.2.0" firebaseBom = "31.2.0"
firebaseCrashlyticsPlugin = "2.9.2" firebaseCrashlyticsPlugin = "2.9.2"

@ -17,8 +17,6 @@
package com.google.samples.apps.nowinandroid.sync.status package com.google.samples.apps.nowinandroid.sync.status
import android.content.Context import android.content.Context
import androidx.lifecycle.asFlow
import androidx.lifecycle.map
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkInfo.State import androidx.work.WorkInfo.State
@ -29,6 +27,7 @@ import com.google.samples.apps.nowinandroid.sync.workers.SyncWorker
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -38,9 +37,8 @@ class WorkManagerSyncManager @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
) : SyncManager { ) : SyncManager {
override val isSyncing: Flow<Boolean> = override val isSyncing: Flow<Boolean> =
WorkManager.getInstance(context).getWorkInfosForUniqueWorkLiveData(SyncWorkName) WorkManager.getInstance(context).getWorkInfosForUniqueWorkFlow(SyncWorkName)
.map(MutableList<WorkInfo>::anyRunning) .map(MutableList<WorkInfo>::anyRunning)
.asFlow()
.conflate() .conflate()
override fun requestSync() { override fun requestSync() {
@ -54,4 +52,4 @@ class WorkManagerSyncManager @Inject constructor(
} }
} }
private val List<WorkInfo>.anyRunning get() = any { it.state == State.RUNNING } private fun List<WorkInfo>.anyRunning() = any { it.state == State.RUNNING }

Loading…
Cancel
Save