Fix applying filter

This commit is contained in:
Koitharu
2024-09-26 12:33:35 +03:00
parent 9a444cf965
commit 63f4fab40f
3 changed files with 48 additions and 14 deletions

View File

@@ -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<Result<List<MangaTag>>> = combine(
flow { emit(searchRepository.getTopTags(repository.source, limit)) },
filterOptions.asFlow(),

View File

@@ -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<FragmentFilterHeaderBinding>(), ChipsView.OnChipClickListener,
@@ -68,17 +66,12 @@ class FilterHeaderFragment : BaseFragment<FragmentFilterHeaderBinding>(), 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,
)
}

View File

@@ -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,
)
}