Invalidate search sources list only on filters change

This commit is contained in:
KokaKiwi 2023-07-18 22:37:07 +02:00
parent 64fd13476b
commit 61f449aa42
No known key found for this signature in database
GPG key ID: 09A5A2688F13FAC1
2 changed files with 44 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.presentation.util.ioCoroutineScope
import eu.kanade.tachiyomi.extension.ExtensionManager
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.util.lang.resetableLazy
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
@ -42,6 +43,9 @@ abstract class SearchScreenModel(
private var lastQuery: String? = null
private var lastSourceFilter: SourceFilter? = null
private val sourcesHolder = resetableLazy { getSelectedSources() }
private val sources by sourcesHolder
protected var extensionFilter: String? = null
protected val pinnedSources = sourcePreferences.pinnedSources().get()
@ -100,6 +104,7 @@ abstract class SearchScreenModel(
fun setSourceFilter(filter: SourceFilter) {
mutableState.update { it.copy(sourceFilter = filter) }
sourcesHolder.reset()
search()
}
@ -119,8 +124,6 @@ abstract class SearchScreenModel(
searchJob?.cancel()
val sources = getSelectedSources()
val initialItems = sources.associateWith { SearchItemResult.Loading }
updateItems(initialItems)

View file

@ -0,0 +1,39 @@
package eu.kanade.tachiyomi.util.lang
@Suppress("ClassName")
internal object UNINITIALIZED_VALUE
interface ResetableLazy<out T> : Lazy<T> {
fun reset()
}
fun <T> resetableLazy(initializer: () -> T): ResetableLazy<T> = ResetableLazyImpl(initializer)
class ResetableLazyImpl<out T>(private val initializer: () -> T, lock: Any? = null) : ResetableLazy<T> {
private val lock = lock ?: this
@Volatile
private var _value: Any? = UNINITIALIZED_VALUE
override val value: T
get() {
if (_value !== UNINITIALIZED_VALUE) {
@Suppress("UNCHECKED_CAST")
return _value as T
}
return synchronized(lock) {
val value = initializer()
_value = value
value
}
}
override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE
override fun reset() {
_value = UNINITIALIZED_VALUE
}
override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
}