Fix Filter not being applied in Browse Source (#7945)

This commit is contained in:
Andreas 2022-09-04 17:15:13 +02:00 committed by GitHub
parent 99270e370e
commit 6b2b21edfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 27 deletions

View file

@ -100,7 +100,7 @@ fun BrowseSourceScreen(
}, },
floatingActionButton = { floatingActionButton = {
BrowseSourceFloatingActionButton( BrowseSourceFloatingActionButton(
isVisible = presenter.filters.isNotEmpty(), isVisible = presenter.filters.isNotEmpty() && presenter.currentFilter is BrowseSourcePresenter.Filter.UserInput,
onFabClick = onFabClick, onFabClick = onFabClick,
) )
}, },
@ -126,7 +126,7 @@ fun BrowseSourceScreen(
horizontalArrangement = Arrangement.spacedBy(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp),
) { ) {
FilterChip( FilterChip(
selected = presenter.currentQuery == GetRemoteManga.QUERY_POPULAR, selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Popular,
onClick = { onClick = {
presenter.resetFilter() presenter.resetFilter()
presenter.search(GetRemoteManga.QUERY_POPULAR) presenter.search(GetRemoteManga.QUERY_POPULAR)
@ -145,7 +145,7 @@ fun BrowseSourceScreen(
) )
if (presenter.source?.supportsLatest == true) { if (presenter.source?.supportsLatest == true) {
FilterChip( FilterChip(
selected = presenter.currentQuery == GetRemoteManga.QUERY_LATEST, selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Latest,
onClick = { onClick = {
presenter.resetFilter() presenter.resetFilter()
presenter.search(GetRemoteManga.QUERY_LATEST) presenter.search(GetRemoteManga.QUERY_LATEST)
@ -165,7 +165,7 @@ fun BrowseSourceScreen(
} }
if (presenter.filters.isNotEmpty()) { if (presenter.filters.isNotEmpty()) {
FilterChip( FilterChip(
selected = presenter.currentQuery != GetRemoteManga.QUERY_POPULAR && presenter.currentQuery != GetRemoteManga.QUERY_LATEST, selected = presenter.currentFilter is BrowseSourcePresenter.Filter.UserInput,
onClick = onFabClick, onClick = onFabClick,
leadingIcon = { leadingIcon = {
Icon( Icon(

View file

@ -6,42 +6,36 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import eu.davidea.flexibleadapter.items.IFlexible import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.domain.source.interactor.GetRemoteManga
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.model.FilterList 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
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter.Filter
import eu.kanade.tachiyomi.ui.browse.source.browse.toItems import eu.kanade.tachiyomi.ui.browse.source.browse.toItems
@Stable @Stable
interface BrowseSourceState { interface BrowseSourceState {
val source: CatalogueSource? val source: CatalogueSource?
var searchQuery: String? var searchQuery: String?
val currentQuery: String val currentFilter: Filter
val isUserQuery: Boolean val isUserQuery: Boolean
val filters: FilterList val filters: FilterList
val filterItems: List<IFlexible<*>> val filterItems: List<IFlexible<*>>
val currentFilters: FilterList
var dialog: BrowseSourcePresenter.Dialog? var dialog: BrowseSourcePresenter.Dialog?
} }
fun BrowseSourceState(initialQuery: String?): BrowseSourceState { fun BrowseSourceState(initialQuery: String?): BrowseSourceState {
if (initialQuery == GetRemoteManga.QUERY_POPULAR || initialQuery == GetRemoteManga.QUERY_LATEST) { return when (val filter = Filter.valueOf(initialQuery ?: "")) {
return BrowseSourceStateImpl(initialCurrentQuery = 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 source: CatalogueSource? by mutableStateOf(null)
override var searchQuery: String? by mutableStateOf(initialQuery) override var searchQuery: String? by mutableStateOf(initialQuery)
override var currentQuery: String by mutableStateOf(initialCurrentQuery ?: "") override var currentFilter: Filter by mutableStateOf(initialCurrentFilter)
override val isUserQuery: Boolean by derivedStateOf { override val isUserQuery: Boolean by derivedStateOf { currentFilter is Filter.UserInput && currentFilter.query.isNotEmpty() }
currentQuery.isNotEmpty() &&
currentQuery != GetRemoteManga.QUERY_POPULAR &&
currentQuery != GetRemoteManga.QUERY_LATEST
}
override var filters: FilterList by mutableStateOf(FilterList()) override var filters: FilterList by mutableStateOf(FilterList())
override val filterItems: List<IFlexible<*>> by derivedStateOf { filters.toItems() } override val filterItems: List<IFlexible<*>> by derivedStateOf { filters.toItems() }
override var currentFilters by mutableStateOf(FilterList())
override var dialog: BrowseSourcePresenter.Dialog? by mutableStateOf(null) override var dialog: BrowseSourcePresenter.Dialog? by mutableStateOf(null)
} }

View file

@ -43,12 +43,12 @@ fun BrowseSourceToolbar(
) { ) {
if (state.searchQuery == null) { if (state.searchQuery == null) {
BrowseSourceRegularToolbar( BrowseSourceRegularToolbar(
title = if (state.isUserQuery) state.currentQuery else source.name, title = if (state.isUserQuery) state.currentFilter.query else source.name,
isLocalSource = source is LocalSource, isLocalSource = source is LocalSource,
displayMode = displayMode, displayMode = displayMode,
onDisplayModeChange = onDisplayModeChange, onDisplayModeChange = onDisplayModeChange,
navigateUp = navigateUp, navigateUp = navigateUp,
onSearchClick = { state.searchQuery = if (state.isUserQuery) state.currentQuery else "" }, onSearchClick = { state.searchQuery = if (state.isUserQuery) state.currentFilter.query else "" },
onWebViewClick = onWebViewClick, onWebViewClick = onWebViewClick,
onHelpClick = onHelpClick, onHelpClick = onHelpClick,
scrollBehavior = scrollBehavior, scrollBehavior = scrollBehavior,

View file

@ -120,11 +120,11 @@ open class BrowseSourcePresenter(
@Composable @Composable
fun getMangaList(): Flow<PagingData<DomainManga>> { fun getMangaList(): Flow<PagingData<DomainManga>> {
return remember(currentQuery, currentFilters) { return remember(currentFilter) {
Pager( Pager(
PagingConfig(pageSize = 25), PagingConfig(pageSize = 25),
) { ) {
getRemoteManga.subscribe(sourceId, currentQuery, currentFilters) getRemoteManga.subscribe(sourceId, currentFilter.query, currentFilter.filters)
}.flow }.flow
.map { .map {
it.map { it.map {
@ -156,13 +156,16 @@ open class BrowseSourcePresenter(
} }
fun resetFilter() { fun resetFilter() {
val newFilters = source!!.getFilterList() if (currentFilter !is Filter.UserInput) return
state.filters = newFilters state.currentFilter = (currentFilter as Filter.UserInput).copy(filters = source!!.getFilterList())
state.currentFilters = state.filters
} }
fun search(query: String? = null) { 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?) { override fun onCreate(savedState: Bundle?) {
@ -309,7 +312,10 @@ open class BrowseSourcePresenter(
* @param filters a list of active filters. * @param filters a list of active filters.
*/ */
fun setSourceFilter(filters: FilterList) { 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 { sealed class Dialog {
data class RemoveManga(val manga: DomainManga) : Dialog() data class RemoveManga(val manga: DomainManga) : Dialog()
data class AddDuplicateManga(val manga: DomainManga, val duplicate: DomainManga) : Dialog() data class AddDuplicateManga(val manga: DomainManga, val duplicate: DomainManga) : Dialog()