Fix micro jitters on Download State change

This commit is contained in:
LooKeR 2022-01-13 01:18:48 +05:30
parent 33dd1961f5
commit 4cd50b971d
2 changed files with 12 additions and 9 deletions

View File

@ -34,7 +34,6 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
} }
private val scope = CoroutineScope(Dispatchers.Default) private val scope = CoroutineScope(Dispatchers.Default)
private val mainDispatcher = Dispatchers.Main
sealed class State(val packageName: String, val name: String) { sealed class State(val packageName: String, val name: String) {
class Pending(packageName: String, name: String) : State(packageName, name) class Pending(packageName: String, name: String) : State(packageName, name)
@ -135,7 +134,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private fun cancelTasks(packageName: String?) { private fun cancelTasks(packageName: String?) {
tasks.removeAll { tasks.removeAll {
(packageName == null || it.packageName == packageName) && run { (packageName == null || it.packageName == packageName) && run {
scope.launch(mainDispatcher) { scope.launch {
mutableStateSubject.emit( mutableStateSubject.emit(
State.Cancel( State.Cancel(
it.packageName, it.packageName,
@ -152,7 +151,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
currentTask?.let { currentTask?.let {
if (packageName == null || it.task.packageName == packageName) { if (packageName == null || it.task.packageName == packageName) {
currentTask = null currentTask = null
scope.launch(mainDispatcher) { scope.launch {
mutableStateSubject.emit( mutableStateSubject.emit(
State.Cancel( State.Cancel(
it.task.packageName, it.task.packageName,
@ -244,7 +243,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private fun publishSuccess(task: Task) { private fun publishSuccess(task: Task) {
var consumed = false var consumed = false
scope.launch(mainDispatcher) { scope.launch {
mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release)) mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release))
consumed = true consumed = true
} }

View File

@ -37,6 +37,9 @@ import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.filter 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.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.util.* import java.util.*
@ -88,11 +91,12 @@ class AppDetailFragment() : ScreenFragment(), AppDetailAdapter.Callbacks {
private var productDisposable: Disposable? = null private var productDisposable: Disposable? = null
private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder -> private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder ->
lifecycleScope.launch { binder.stateSubject
binder.stateSubject.filter { it.packageName == packageName }.collect { .filter { it.packageName == packageName }
updateDownloadState(it) .flowOn(Dispatchers.Default)
} .onEach { updateDownloadState(it) }
} .flowOn(Dispatchers.Main)
.launchIn(lifecycleScope)
}) })
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {