From 7b60ed6bad89db34a45f7e85d0058aed9d307bb2 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 13 Jul 2023 13:12:21 +0300 Subject: [PATCH] Fix new sources dialog list --- .../newsources/NewSourcesViewModel.kt | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/newsources/NewSourcesViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/newsources/NewSourcesViewModel.kt index 1c00899b3..bec1e8f3d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/newsources/NewSourcesViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/newsources/NewSourcesViewModel.kt @@ -4,6 +4,9 @@ import androidx.annotation.WorkerThread import androidx.core.os.LocaleListCompat import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.MutableStateFlow import org.koitharu.kotatsu.core.model.getLocaleTitle import org.koitharu.kotatsu.core.prefs.AppSettings @@ -19,18 +22,26 @@ class NewSourcesViewModel @Inject constructor( private val initialList = settings.newSources val sources = MutableStateFlow?>(null) + private var listUpdateJob: Job? = null init { - launchJob(Dispatchers.Default) { + listUpdateJob = launchJob(Dispatchers.Default) { sources.value = buildList() } } fun onItemEnabledChanged(item: SourceConfigItem.SourceItem, isEnabled: Boolean) { - if (isEnabled) { - settings.hiddenSources -= item.source.name - } else { - settings.hiddenSources += item.source.name + val prevJob = listUpdateJob + listUpdateJob = launchJob(Dispatchers.Default) { + if (isEnabled) { + settings.hiddenSources -= item.source.name + } else { + settings.hiddenSources += item.source.name + } + prevJob?.cancelAndJoin() + val list = buildList() + ensureActive() + sources.value = list } } @@ -61,3 +72,4 @@ class NewSourcesViewModel @Inject constructor( } } } +