|
|
@ -29,13 +29,16 @@ import com.google.samples.apps.nowinandroid.core.domain.Synchronizer
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
|
|
|
|
import com.google.samples.apps.nowinandroid.sync.initializers.SyncConstraints
|
|
|
|
import com.google.samples.apps.nowinandroid.sync.initializers.SyncConstraints
|
|
|
|
import com.google.samples.apps.nowinandroid.sync.initializers.syncForegroundInfo
|
|
|
|
import com.google.samples.apps.nowinandroid.sync.initializers.syncForegroundInfo
|
|
|
|
import dagger.assisted.Assisted
|
|
|
|
import dagger.assisted.Assisted
|
|
|
|
import dagger.assisted.AssistedInject
|
|
|
|
import dagger.assisted.AssistedInject
|
|
|
|
|
|
|
|
import kotlinx.coroutines.CoroutineDispatcher
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.awaitAll
|
|
|
|
import kotlinx.coroutines.awaitAll
|
|
|
|
import kotlinx.coroutines.coroutineScope
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Syncs the data layer by delegating to the appropriate repository instances with
|
|
|
|
* Syncs the data layer by delegating to the appropriate repository instances with
|
|
|
@ -49,12 +52,13 @@ class SyncWorker @AssistedInject constructor(
|
|
|
|
private val topicRepository: TopicsRepository,
|
|
|
|
private val topicRepository: TopicsRepository,
|
|
|
|
private val newsRepository: NewsRepository,
|
|
|
|
private val newsRepository: NewsRepository,
|
|
|
|
private val authorsRepository: AuthorsRepository,
|
|
|
|
private val authorsRepository: AuthorsRepository,
|
|
|
|
|
|
|
|
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
|
|
|
|
) : CoroutineWorker(appContext, workerParams), Synchronizer {
|
|
|
|
) : CoroutineWorker(appContext, workerParams), Synchronizer {
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun getForegroundInfo(): ForegroundInfo =
|
|
|
|
override suspend fun getForegroundInfo(): ForegroundInfo =
|
|
|
|
appContext.syncForegroundInfo()
|
|
|
|
appContext.syncForegroundInfo()
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun doWork(): Result = coroutineScope {
|
|
|
|
override suspend fun doWork(): Result = withContext(ioDispatcher) {
|
|
|
|
// First sync the repositories in parallel
|
|
|
|
// First sync the repositories in parallel
|
|
|
|
val syncedSuccessfully = awaitAll(
|
|
|
|
val syncedSuccessfully = awaitAll(
|
|
|
|
async { topicRepository.sync() },
|
|
|
|
async { topicRepository.sync() },
|
|
|
|