Improve performance across screens (#4071)

* Keep screens loaded while they're hidden and rebuild the library faster

* Count chapters in SQL for the migration list

* Show the extension list sooner and check trust against loaded stores

* Cancel the previous tracker search when searching again

* Write reader progress less often and keep its work off the main thread

* Stop rebuilding the settings search index on every key

* Watch updates for widgets only while one is placed

The query re-ran on every chapter or manga write even with no widget on
the home screen.

* Observe download pages and limit how often their progress updates

* Throttle the library update notification and write fetch data together
This commit is contained in:
AntsyLich
2026-10-11 10:28:52 +06:00
committed by GitHub
parent b15af71b94
commit 260e31cd2c
33 changed files with 458 additions and 136 deletions
@@ -30,6 +30,7 @@ import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
@@ -48,6 +49,7 @@ import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.presentation.components.UpIcon
import eu.kanade.presentation.more.settings.Preference
import eu.kanade.presentation.util.Screen
import kotlinx.coroutines.flow.collectLatest
import mihon.icons.materialsymbols.MaterialSymbols
import mihon.icons.materialsymbols.rounded.Close
import tachiyomi.i18n.MR
@@ -141,7 +143,7 @@ class SettingsSearchScreen : Screen() {
},
) { contentPadding ->
SearchResult(
searchKey = textFieldState.text.toString(),
searchKey = { textFieldState.text.toString() },
listState = listState,
contentPadding = contentPadding,
) { result ->
@@ -154,64 +156,20 @@ class SettingsSearchScreen : Screen() {
@Composable
private fun SearchResult(
searchKey: String,
searchKey: () -> String,
modifier: Modifier = Modifier,
listState: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(),
onItemClick: (SearchResultItem) -> Unit,
) {
if (searchKey.isEmpty()) return
val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr
val index = getIndex()
val result by produceState<List<SearchResultItem>?>(initialValue = null, searchKey) {
value = index.asSequence()
.flatMap { settingsData ->
fun result(title: String, categoryTitle: String?) = SearchResultItem(
route = settingsData.route,
title = title,
breadcrumbs = getLocalizedBreadcrumb(
path = settingsData.title,
node = categoryTitle,
isLtr = isLtr,
),
highlightKey = title,
)
fun Preference.PreferenceItem<*, *>.matches(): Boolean {
// Don't show info preference
if (this is Preference.PreferenceItem.InfoPreference) return false
val inTitle = title.contains(searchKey, true)
val inSummary = subtitle?.contains(searchKey, true) ?: false
return inTitle || inSummary
}
settingsData.contents.asSequence()
// Only search from enabled prefs and one with valid title
.filter { it.visible && it.title.isNotBlank() }
.flatMap { p ->
when (p) {
is Preference.PreferenceGroup -> {
// A matching heading leads to the group itself, not to one of its items
val heading = if (p.title.contains(searchKey, true)) {
sequenceOf(result(p.title, null))
} else {
emptySequence()
}
val items = p.preferenceItems.asSequence()
.filter { it.visible && it.title.isNotBlank() && it.matches() }
.map { result(it.title, p.title) }
heading + items
}
is Preference.PreferenceItem<*, *> -> {
if (p.matches()) sequenceOf(result(p.title, null)) else emptySequence()
}
}
}
}
.take(10) // Just take top 10 result for quicker result
.toList()
// The query is read here rather than in composition, so typing doesn't rebuild the index on every key
val result by produceState<List<SearchResultItem>?>(initialValue = null, index) {
snapshotFlow(searchKey).collectLatest { searchKey ->
value = if (searchKey.isEmpty()) null else search(index, searchKey, isLtr)
}
}
Crossfade(
@@ -262,6 +220,55 @@ private fun SearchResult(
}
}
private fun search(index: List<SettingsData>, searchKey: String, isLtr: Boolean): List<SearchResultItem> {
return index.asSequence()
.flatMap { settingsData ->
fun result(title: String, categoryTitle: String?) = SearchResultItem(
route = settingsData.route,
title = title,
breadcrumbs = getLocalizedBreadcrumb(
path = settingsData.title,
node = categoryTitle,
isLtr = isLtr,
),
highlightKey = title,
)
fun Preference.PreferenceItem<*, *>.matches(): Boolean {
// Don't show info preference
if (this is Preference.PreferenceItem.InfoPreference) return false
val inTitle = title.contains(searchKey, true)
val inSummary = subtitle?.contains(searchKey, true) ?: false
return inTitle || inSummary
}
settingsData.contents.asSequence()
// Only search from enabled prefs and one with valid title
.filter { it.visible && it.title.isNotBlank() }
.flatMap { p ->
when (p) {
is Preference.PreferenceGroup -> {
// A matching heading leads to the group itself, not to one of its items
val heading = if (p.title.contains(searchKey, true)) {
sequenceOf(result(p.title, null))
} else {
emptySequence()
}
val items = p.preferenceItems.asSequence()
.filter { it.visible && it.title.isNotBlank() && it.matches() }
.map { result(it.title, p.title) }
heading + items
}
is Preference.PreferenceItem<*, *> -> {
if (p.matches()) sequenceOf(result(p.title, null)) else emptySequence()
}
}
}
}
.take(10) // Just take top 10 result for quicker result
.toList()
}
@Composable
@NonRestartableComposable
private fun getIndex() = settingScreens