From 21804bfc45b05c67fe6198cd0452cd67b05bc3e8 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 3 Sep 2022 10:47:48 -0400 Subject: [PATCH] Reimplement extensions search Not sure if I should abstract this out to per-tab though. Maybe when we need it... --- .../presentation/components/TabbedScreen.kt | 29 +++++++++++++++---- .../tachiyomi/ui/browse/BrowseController.kt | 6 ++++ .../browse/extension/ExtensionsPresenter.kt | 17 +++++++---- .../ui/browse/extension/ExtensionsTab.kt | 5 +--- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt b/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt index 806318d52..9ad3cdfc0 100644 --- a/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt @@ -21,6 +21,8 @@ fun TabbedScreen( @StringRes titleRes: Int, tabs: List, startIndex: Int? = null, + searchQuery: String? = null, + onChangeSearchQuery: (String?) -> Unit = {}, incognitoMode: Boolean, downloadedOnlyMode: Boolean, ) { @@ -35,12 +37,27 @@ fun TabbedScreen( Scaffold( topBar = { - AppBar( - title = stringResource(titleRes), - actions = { - AppBarActions(tabs[state.currentPage].actions) - }, - ) + if (searchQuery == null) { + AppBar( + title = stringResource(titleRes), + actions = { + AppBarActions(tabs[state.currentPage].actions) + }, + ) + } else { + SearchToolbar( + searchQuery = searchQuery, + onChangeSearchQuery = { + onChangeSearchQuery(it) + }, + onClickCloseSearch = { + onChangeSearchQuery(null) + }, + onClickResetSearch = { + onChangeSearchQuery("") + }, + ) + } }, ) { paddingValues -> Column(modifier = Modifier.padding(paddingValues)) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseController.kt index af35024ce..ecf2de02d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseController.kt @@ -5,6 +5,8 @@ import android.os.Bundle import android.view.View import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.core.os.bundleOf import eu.kanade.presentation.components.TabbedScreen import eu.kanade.tachiyomi.R @@ -31,6 +33,8 @@ class BrowseController : FullComposeController, RootController @Composable override fun ComposeContent() { + val query by presenter.extensionsPresenter.query.collectAsState() + TabbedScreen( titleRes = R.string.browse, tabs = listOf( @@ -39,6 +43,8 @@ class BrowseController : FullComposeController, RootController migrateSourcesTab(router, presenter.migrationSourcesPresenter), ), startIndex = 1.takeIf { toExtensions }, + searchQuery = query, + onChangeSearchQuery = { presenter.extensionsPresenter.search(it) }, incognitoMode = presenter.isIncognitoMode, downloadedOnlyMode = presenter.isDownloadOnly, ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt index b75967c62..063c72f3c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt @@ -16,6 +16,8 @@ import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.system.LocaleHelper import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.launchIn @@ -33,7 +35,8 @@ class ExtensionsPresenter( private val getExtensions: GetExtensionsByType = Injekt.get(), ) : ExtensionsState by state { - private val _query: MutableStateFlow = MutableStateFlow("") + private val _query: MutableStateFlow = MutableStateFlow(null) + val query: StateFlow = _query.asStateFlow() private var _currentDownloads = MutableStateFlow>(hashMapOf()) @@ -77,8 +80,10 @@ class ExtensionsPresenter( getExtensions.subscribe(), _currentDownloads, ) { query, (_updates, _installed, _available, _untrusted), downloads -> + val searchQuery = query ?: "" + val languagesWithExtensions = _available - .filter(queryFilter(query)) + .filter(queryFilter(searchQuery)) .groupBy { LocaleHelper.getSourceDisplayName(it.lang, context) } .toSortedMap() .flatMap { (key, value) -> @@ -90,14 +95,14 @@ class ExtensionsPresenter( val items = mutableListOf() - val updates = _updates.filter(queryFilter(query)).map(extensionMapper(downloads)) + val updates = _updates.filter(queryFilter(searchQuery)).map(extensionMapper(downloads)) if (updates.isNotEmpty()) { items.add(ExtensionUiModel.Header.Resource(R.string.ext_updates_pending)) items.addAll(updates) } - val installed = _installed.filter(queryFilter(query)).map(extensionMapper(downloads)) - val untrusted = _untrusted.filter(queryFilter(query)).map(extensionMapper(downloads)) + val installed = _installed.filter(queryFilter(searchQuery)).map(extensionMapper(downloads)) + val untrusted = _untrusted.filter(queryFilter(searchQuery)).map(extensionMapper(downloads)) if (installed.isNotEmpty() || untrusted.isNotEmpty()) { items.add(ExtensionUiModel.Header.Resource(R.string.ext_installed)) items.addAll(installed) @@ -122,7 +127,7 @@ class ExtensionsPresenter( .launchIn(presenterScope) } - fun search(query: String) { + fun search(query: String?) { presenterScope.launchIO { _query.emit(query) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt index 64a15d5ba..6f8f5df11 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsTab.kt @@ -25,10 +25,7 @@ fun extensionsTab( AppBar.Action( title = stringResource(R.string.action_search), icon = Icons.Outlined.Search, - onClick = { - // TODO: extensions search - // presenter.search(query) - }, + onClick = { presenter.search("") }, ), AppBar.Action(