Fix new sources dialog list

pull/416/head
Koitharu 3 years ago
parent 619be69580
commit 7b60ed6bad

@ -4,6 +4,9 @@ import androidx.annotation.WorkerThread
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import org.koitharu.kotatsu.core.model.getLocaleTitle import org.koitharu.kotatsu.core.model.getLocaleTitle
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
@ -19,19 +22,27 @@ class NewSourcesViewModel @Inject constructor(
private val initialList = settings.newSources private val initialList = settings.newSources
val sources = MutableStateFlow<List<SourceConfigItem>?>(null) val sources = MutableStateFlow<List<SourceConfigItem>?>(null)
private var listUpdateJob: Job? = null
init { init {
launchJob(Dispatchers.Default) { listUpdateJob = launchJob(Dispatchers.Default) {
sources.value = buildList() sources.value = buildList()
} }
} }
fun onItemEnabledChanged(item: SourceConfigItem.SourceItem, isEnabled: Boolean) { fun onItemEnabledChanged(item: SourceConfigItem.SourceItem, isEnabled: Boolean) {
val prevJob = listUpdateJob
listUpdateJob = launchJob(Dispatchers.Default) {
if (isEnabled) { if (isEnabled) {
settings.hiddenSources -= item.source.name settings.hiddenSources -= item.source.name
} else { } else {
settings.hiddenSources += item.source.name settings.hiddenSources += item.source.name
} }
prevJob?.cancelAndJoin()
val list = buildList()
ensureActive()
sources.value = list
}
} }
fun apply() { fun apply() {
@ -61,3 +72,4 @@ class NewSourcesViewModel @Inject constructor(
} }
} }
} }

Loading…
Cancel
Save