diff --git a/src/main/kotlin/com/looker/droidify/service/DownloadService.kt b/src/main/kotlin/com/looker/droidify/service/DownloadService.kt index 6f01ca70..77833c45 100644 --- a/src/main/kotlin/com/looker/droidify/service/DownloadService.kt +++ b/src/main/kotlin/com/looker/droidify/service/DownloadService.kt @@ -34,7 +34,6 @@ class DownloadService : ConnectionService() { } private val scope = CoroutineScope(Dispatchers.Default) - private val mainDispatcher = Dispatchers.Main sealed class State(val packageName: String, val name: String) { class Pending(packageName: String, name: String) : State(packageName, name) @@ -135,7 +134,7 @@ class DownloadService : ConnectionService() { private fun cancelTasks(packageName: String?) { tasks.removeAll { (packageName == null || it.packageName == packageName) && run { - scope.launch(mainDispatcher) { + scope.launch { mutableStateSubject.emit( State.Cancel( it.packageName, @@ -152,7 +151,7 @@ class DownloadService : ConnectionService() { currentTask?.let { if (packageName == null || it.task.packageName == packageName) { currentTask = null - scope.launch(mainDispatcher) { + scope.launch { mutableStateSubject.emit( State.Cancel( it.task.packageName, @@ -244,7 +243,7 @@ class DownloadService : ConnectionService() { private fun publishSuccess(task: Task) { var consumed = false - scope.launch(mainDispatcher) { + scope.launch { mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release)) consumed = true } diff --git a/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt b/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt index 020e6516..aa9be013 100644 --- a/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt +++ b/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt @@ -37,6 +37,9 @@ import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.util.* @@ -88,11 +91,12 @@ class AppDetailFragment() : ScreenFragment(), AppDetailAdapter.Callbacks { private var productDisposable: Disposable? = null private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder -> - lifecycleScope.launch { - binder.stateSubject.filter { it.packageName == packageName }.collect { - updateDownloadState(it) - } - } + binder.stateSubject + .filter { it.packageName == packageName } + .flowOn(Dispatchers.Default) + .onEach { updateDownloadState(it) } + .flowOn(Dispatchers.Main) + .launchIn(lifecycleScope) }) override fun onViewCreated(view: View, savedInstanceState: Bundle?) {