From 41cfd99d32cd6592dbd32e75a248c214e0676349 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 26 Sep 2024 12:33:35 +0300 Subject: [PATCH] Fix applying filter --- .../kotatsu/filter/ui/FilterCoordinator.kt | 40 ++++++++++++++++--- .../kotatsu/filter/ui/FilterHeaderFragment.kt | 11 +---- .../kotatsu/filter/ui/FilterHeaderProducer.kt | 11 +++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterCoordinator.kt b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterCoordinator.kt index 248760075..110bf6373 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterCoordinator.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterCoordinator.kt @@ -267,32 +267,50 @@ class FilterCoordinator @Inject constructor( } fun setQuery(value: String?) { + val newQuery = value?.trim()?.takeUnless { it.isEmpty() } currentListFilter.update { oldValue -> - oldValue.copy(query = value?.trim()?.takeUnless { it.isEmpty() }) + if (capabilities.isSearchWithFiltersSupported || newQuery == null) { + oldValue.copy(query = newQuery) + } else { + MangaListFilter(query = newQuery) + } } } fun setLocale(value: Locale?) { currentListFilter.update { oldValue -> - oldValue.copy(locale = value) + oldValue.copy( + locale = value, + query = oldValue.takeQueryIfSupported(), + ) } } fun setOriginalLocale(value: Locale?) { currentListFilter.update { oldValue -> - oldValue.copy(originalLocale = value) + oldValue.copy( + originalLocale = value, + query = oldValue.takeQueryIfSupported(), + ) } } fun setYear(value: Int) { currentListFilter.update { oldValue -> - oldValue.copy(year = value) + oldValue.copy( + year = value, + query = oldValue.takeQueryIfSupported(), + ) } } fun setYearRange(valueFrom: Int, valueTo: Int) { currentListFilter.update { oldValue -> - oldValue.copy(yearFrom = valueFrom, yearTo = valueTo) + oldValue.copy( + yearFrom = valueFrom, + yearTo = valueTo, + query = oldValue.takeQueryIfSupported(), + ) } } @@ -300,6 +318,7 @@ class FilterCoordinator @Inject constructor( currentListFilter.update { oldValue -> oldValue.copy( states = if (isSelected) oldValue.states + value else oldValue.states - value, + query = oldValue.takeQueryIfSupported(), ) } } @@ -308,6 +327,7 @@ class FilterCoordinator @Inject constructor( currentListFilter.update { oldValue -> oldValue.copy( contentRating = if (isSelected) oldValue.contentRating + value else oldValue.contentRating - value, + query = oldValue.takeQueryIfSupported(), ) } } @@ -316,6 +336,7 @@ class FilterCoordinator @Inject constructor( currentListFilter.update { oldValue -> oldValue.copy( types = if (isSelected) oldValue.types + value else oldValue.types - value, + query = oldValue.takeQueryIfSupported(), ) } } @@ -330,6 +351,7 @@ class FilterCoordinator @Inject constructor( oldValue.copy( tags = newTags, tagsExclude = oldValue.tagsExclude - newTags, + query = oldValue.takeQueryIfSupported(), ) } } @@ -344,6 +366,7 @@ class FilterCoordinator @Inject constructor( oldValue.copy( tags = oldValue.tags - newTagsExclude, tagsExclude = newTagsExclude, + query = oldValue.takeQueryIfSupported(), ) } } @@ -352,6 +375,13 @@ class FilterCoordinator @Inject constructor( it.map { x -> x.availableTags.sortedWithSafe(TagTitleComparator(sourceLocale)) } } + private fun MangaListFilter.takeQueryIfSupported() = when { + capabilities.isSearchWithFiltersSupported -> query + query.isNullOrEmpty() -> query + hasNonSearchOptions() -> null + else -> query + } + private fun getTopTags(limit: Int): Flow>> = combine( flow { emit(searchRepository.getTopTags(repository.source, limit)) }, filterOptions.asFlow(), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderFragment.kt index 57662e12c..0dc0271e7 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderFragment.kt @@ -9,7 +9,6 @@ import com.google.android.material.chip.Chip import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.flowOn -import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ui.BaseFragment import org.koitharu.kotatsu.core.ui.widgets.ChipsView import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled @@ -19,7 +18,6 @@ import org.koitharu.kotatsu.filter.ui.model.FilterHeaderModel import org.koitharu.kotatsu.filter.ui.tags.TagsCatalogSheet import org.koitharu.kotatsu.parsers.model.MangaTag import javax.inject.Inject -import com.google.android.material.R as materialR @AndroidEntryPoint class FilterHeaderFragment : BaseFragment(), ChipsView.OnChipClickListener, @@ -68,17 +66,12 @@ class FilterHeaderFragment : BaseFragment(), ChipsV binding.root.isVisible = false return } + binding.chipsTags.setChips(header.chips) + binding.root.isVisible = true if (binding.root.context.isAnimationsEnabled) { binding.scrollView.smoothScrollTo(0, 0) } else { binding.scrollView.scrollTo(0, 0) } - binding.chipsTags.setChips(header.chips + moreTagsChip()) - binding.root.isVisible = true } - - private fun moreTagsChip() = ChipsView.ChipModel( - title = getString(R.string.more), - icon = materialR.drawable.abc_ic_menu_overflow_material, - ) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderProducer.kt b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderProducer.kt index 9b6ef87c1..c29c0eeec 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderProducer.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/filter/ui/FilterHeaderProducer.kt @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.filter.ui import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine +import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ui.widgets.ChipsView import org.koitharu.kotatsu.filter.ui.model.FilterHeaderModel import org.koitharu.kotatsu.filter.ui.model.FilterProperty @@ -86,6 +87,16 @@ class FilterHeaderProducer @Inject constructor( ), ) } + val hasTags = result.any { it.data is MangaTag } + if (hasTags) { + result.addLast(moreTagsChip()) + } return result } + + private fun moreTagsChip() = ChipsView.ChipModel( + titleResId = R.string.more, + isDropdown = true, + // icon = materialR.drawable.abc_ic_menu_overflow_material, + ) }