From 6b2b21edfa63f01b956ec27386b0c729be2e6d18 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 4 Sep 2022 17:15:13 +0200 Subject: [PATCH] Fix Filter not being applied in Browse Source (#7945) --- .../presentation/browse/BrowseSourceScreen.kt | 8 ++--- .../presentation/browse/BrowseSourceState.kt | 22 +++++------- .../browse/components/BrowseSourceToolbar.kt | 4 +-- .../source/browse/BrowseSourcePresenter.kt | 36 +++++++++++++++---- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt index c0541daa77..faef3409a1 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt @@ -100,7 +100,7 @@ fun BrowseSourceScreen( }, floatingActionButton = { BrowseSourceFloatingActionButton( - isVisible = presenter.filters.isNotEmpty(), + isVisible = presenter.filters.isNotEmpty() && presenter.currentFilter is BrowseSourcePresenter.Filter.UserInput, onFabClick = onFabClick, ) }, @@ -126,7 +126,7 @@ fun BrowseSourceScreen( horizontalArrangement = Arrangement.spacedBy(8.dp), ) { FilterChip( - selected = presenter.currentQuery == GetRemoteManga.QUERY_POPULAR, + selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Popular, onClick = { presenter.resetFilter() presenter.search(GetRemoteManga.QUERY_POPULAR) @@ -145,7 +145,7 @@ fun BrowseSourceScreen( ) if (presenter.source?.supportsLatest == true) { FilterChip( - selected = presenter.currentQuery == GetRemoteManga.QUERY_LATEST, + selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Latest, onClick = { presenter.resetFilter() presenter.search(GetRemoteManga.QUERY_LATEST) @@ -165,7 +165,7 @@ fun BrowseSourceScreen( } if (presenter.filters.isNotEmpty()) { FilterChip( - selected = presenter.currentQuery != GetRemoteManga.QUERY_POPULAR && presenter.currentQuery != GetRemoteManga.QUERY_LATEST, + selected = presenter.currentFilter is BrowseSourcePresenter.Filter.UserInput, onClick = onFabClick, leadingIcon = { Icon( diff --git a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceState.kt b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceState.kt index 2e71236bf5..afff9f0dda 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceState.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceState.kt @@ -6,42 +6,36 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import eu.davidea.flexibleadapter.items.IFlexible -import eu.kanade.domain.source.interactor.GetRemoteManga import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter +import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter.Filter import eu.kanade.tachiyomi.ui.browse.source.browse.toItems @Stable interface BrowseSourceState { val source: CatalogueSource? var searchQuery: String? - val currentQuery: String + val currentFilter: Filter val isUserQuery: Boolean val filters: FilterList val filterItems: List> - val currentFilters: FilterList var dialog: BrowseSourcePresenter.Dialog? } fun BrowseSourceState(initialQuery: String?): BrowseSourceState { - if (initialQuery == GetRemoteManga.QUERY_POPULAR || initialQuery == GetRemoteManga.QUERY_LATEST) { - return BrowseSourceStateImpl(initialCurrentQuery = initialQuery) + return when (val filter = Filter.valueOf(initialQuery ?: "")) { + Filter.Latest, Filter.Popular -> BrowseSourceStateImpl(initialCurrentFilter = filter) + is Filter.UserInput -> BrowseSourceStateImpl(initialQuery = initialQuery, initialCurrentFilter = filter) } - return BrowseSourceStateImpl(initialQuery = initialQuery) } -class BrowseSourceStateImpl(initialQuery: String? = null, initialCurrentQuery: String? = initialQuery) : BrowseSourceState { +class BrowseSourceStateImpl(initialQuery: String? = null, initialCurrentFilter: Filter) : BrowseSourceState { override var source: CatalogueSource? by mutableStateOf(null) override var searchQuery: String? by mutableStateOf(initialQuery) - override var currentQuery: String by mutableStateOf(initialCurrentQuery ?: "") - override val isUserQuery: Boolean by derivedStateOf { - currentQuery.isNotEmpty() && - currentQuery != GetRemoteManga.QUERY_POPULAR && - currentQuery != GetRemoteManga.QUERY_LATEST - } + override var currentFilter: Filter by mutableStateOf(initialCurrentFilter) + override val isUserQuery: Boolean by derivedStateOf { currentFilter is Filter.UserInput && currentFilter.query.isNotEmpty() } override var filters: FilterList by mutableStateOf(FilterList()) override val filterItems: List> by derivedStateOf { filters.toItems() } - override var currentFilters by mutableStateOf(FilterList()) override var dialog: BrowseSourcePresenter.Dialog? by mutableStateOf(null) } diff --git a/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt b/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt index 3bfff65396..83246f2e44 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt @@ -43,12 +43,12 @@ fun BrowseSourceToolbar( ) { if (state.searchQuery == null) { BrowseSourceRegularToolbar( - title = if (state.isUserQuery) state.currentQuery else source.name, + title = if (state.isUserQuery) state.currentFilter.query else source.name, isLocalSource = source is LocalSource, displayMode = displayMode, onDisplayModeChange = onDisplayModeChange, navigateUp = navigateUp, - onSearchClick = { state.searchQuery = if (state.isUserQuery) state.currentQuery else "" }, + onSearchClick = { state.searchQuery = if (state.isUserQuery) state.currentFilter.query else "" }, onWebViewClick = onWebViewClick, onHelpClick = onHelpClick, scrollBehavior = scrollBehavior, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index 0500ad3450..04e7eddebc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -120,11 +120,11 @@ open class BrowseSourcePresenter( @Composable fun getMangaList(): Flow> { - return remember(currentQuery, currentFilters) { + return remember(currentFilter) { Pager( PagingConfig(pageSize = 25), ) { - getRemoteManga.subscribe(sourceId, currentQuery, currentFilters) + getRemoteManga.subscribe(sourceId, currentFilter.query, currentFilter.filters) }.flow .map { it.map { @@ -156,13 +156,16 @@ open class BrowseSourcePresenter( } fun resetFilter() { - val newFilters = source!!.getFilterList() - state.filters = newFilters - state.currentFilters = state.filters + if (currentFilter !is Filter.UserInput) return + state.currentFilter = (currentFilter as Filter.UserInput).copy(filters = source!!.getFilterList()) } fun search(query: String? = null) { - state.currentQuery = query ?: searchQuery ?: "" + var new = Filter.valueOf(query ?: searchQuery ?: "") + if (new is Filter.UserInput && currentFilter is Filter.UserInput) { + new = new.copy(filters = currentFilter.filters) + } + state.currentFilter = new } override fun onCreate(savedState: Bundle?) { @@ -309,7 +312,10 @@ open class BrowseSourcePresenter( * @param filters a list of active filters. */ fun setSourceFilter(filters: FilterList) { - state.currentFilters = filters + state.currentFilter = when (val filter = currentFilter) { + Filter.Latest, Filter.Popular -> Filter.UserInput(filters = filters) + is Filter.UserInput -> filter.copy(filters = filters) + } } /** @@ -341,6 +347,22 @@ open class BrowseSourcePresenter( } } + sealed class Filter(open val query: String, open val filters: FilterList) { + object Popular : Filter(query = GetRemoteManga.QUERY_POPULAR, filters = FilterList()) + object Latest : Filter(query = GetRemoteManga.QUERY_LATEST, filters = FilterList()) + data class UserInput(override val query: String = "", override val filters: FilterList = FilterList()) : Filter(query = query, filters = filters) + + companion object { + fun valueOf(query: String): Filter { + return when (query) { + GetRemoteManga.QUERY_POPULAR -> Popular + GetRemoteManga.QUERY_LATEST -> Latest + else -> UserInput(query = query) + } + } + } + } + sealed class Dialog { data class RemoveManga(val manga: DomainManga) : Dialog() data class AddDuplicateManga(val manga: DomainManga, val duplicate: DomainManga) : Dialog()