From c1e23ec18e54a53d1f027f66e71822a99602f603 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 8 Apr 2023 09:18:48 +0700 Subject: [PATCH] GlobalSearchScreen: Skip result screen when using search intent (#9299) --- .../source/globalsearch/GlobalSearchScreen.kt | 58 ++++++++++++++----- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt index 151e750444..82ed21be7b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt @@ -1,8 +1,12 @@ package eu.kanade.tachiyomi.ui.browse.source.globalsearch import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow @@ -10,10 +14,11 @@ import eu.kanade.presentation.browse.GlobalSearchScreen import eu.kanade.presentation.util.Screen import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceScreen import eu.kanade.tachiyomi.ui.manga.MangaScreen +import tachiyomi.presentation.core.screens.LoadingScreen class GlobalSearchScreen( val searchQuery: String = "", - val extensionFilter: String = "", + private val extensionFilter: String = "", ) : Screen() { @Composable @@ -27,21 +32,44 @@ class GlobalSearchScreen( ) } val state by screenModel.state.collectAsState() + var showSingleLoadingScreen by remember { + mutableStateOf(searchQuery.isNotEmpty() && extensionFilter.isNotEmpty() && state.total == 1) + } - GlobalSearchScreen( - state = state, - navigateUp = navigator::pop, - onChangeSearchQuery = screenModel::updateSearchQuery, - onSearch = screenModel::search, - getManga = { screenModel.getManga(it) }, - onClickSource = { - if (!screenModel.incognitoMode.get()) { - screenModel.lastUsedSourceId.set(it.id) + if (showSingleLoadingScreen) { + LoadingScreen() + + LaunchedEffect(state.items) { + when (val result = state.items.values.singleOrNull()) { + SearchItemResult.Loading -> return@LaunchedEffect + is SearchItemResult.Success -> { + val manga = result.result.singleOrNull() + if (manga != null) { + navigator.replace(MangaScreen(manga.id, true)) + } else { + // Backoff to result screen + showSingleLoadingScreen = false + } + } + else -> showSingleLoadingScreen = false } - navigator.push(BrowseSourceScreen(it.id, state.searchQuery)) - }, - onClickItem = { navigator.push(MangaScreen(it.id, true)) }, - onLongClickItem = { navigator.push(MangaScreen(it.id, true)) }, - ) + } + } else { + GlobalSearchScreen( + state = state, + navigateUp = navigator::pop, + onChangeSearchQuery = screenModel::updateSearchQuery, + onSearch = screenModel::search, + getManga = { screenModel.getManga(it) }, + onClickSource = { + if (!screenModel.incognitoMode.get()) { + screenModel.lastUsedSourceId.set(it.id) + } + navigator.push(BrowseSourceScreen(it.id, state.searchQuery)) + }, + onClickItem = { navigator.push(MangaScreen(it.id, true)) }, + onLongClickItem = { navigator.push(MangaScreen(it.id, true)) }, + ) + } } }