Add authors to filter

This commit is contained in:
Koitharu
2025-10-21 14:40:37 +03:00
parent b414758f32
commit c8a8203c39
5 changed files with 62 additions and 0 deletions

View File

@@ -34,6 +34,9 @@ abstract class MangaDao {
@Query("SELECT author FROM manga WHERE author LIKE :query GROUP BY author ORDER BY COUNT(author) DESC LIMIT :limit")
abstract suspend fun findAuthors(query: String, limit: Int): List<String>
@Query("SELECT author FROM manga WHERE manga.source = :source AND author IS NOT NULL AND author != '' GROUP BY author ORDER BY COUNT(author) DESC LIMIT :limit")
abstract suspend fun findAuthorsBySource(source: String, limit: Int): List<String>
@Transaction
@Query("SELECT * FROM manga WHERE (title LIKE :query OR alt_title LIKE :query) AND manga_id IN (SELECT manga_id FROM favourites UNION SELECT manga_id FROM history) LIMIT :limit")
abstract suspend fun searchByTitle(query: String, limit: Int): List<MangaWithTags>

View File

@@ -124,6 +124,20 @@ class FilterCoordinator @Inject constructor(
MutableStateFlow(FilterProperty.EMPTY)
}
val authors: StateFlow<FilterProperty<String>> = if (capabilities.isAuthorSearchSupported) {
combine(
flow { emit(searchRepository.getAuthors(repository.source, TAGS_LIMIT)) },
currentListFilter.distinctUntilChangedBy { it.author },
) { available, selected ->
FilterProperty(
availableItems = available,
selectedItems = setOfNotNull(selected.author),
)
}.stateIn(coroutineScope, SharingStarted.Lazily, FilterProperty.LOADING)
} else {
MutableStateFlow(FilterProperty.EMPTY)
}
val states: StateFlow<FilterProperty<MangaState>> = combine(
filterOptions.asFlow(),
currentListFilter.distinctUntilChangedBy { it.states },

View File

@@ -78,6 +78,7 @@ class FilterSheetFragment : BaseAdaptiveSheet<SheetFilterBinding>(),
filter.originalLocale.observe(viewLifecycleOwner, this::onOriginalLocaleChanged)
filter.tags.observe(viewLifecycleOwner, this::onTagsChanged)
filter.tagsExcluded.observe(viewLifecycleOwner, this::onTagsExcludedChanged)
filter.authors.observe(viewLifecycleOwner, this::onAuthorsChanged)
filter.states.observe(viewLifecycleOwner, this::onStateChanged)
filter.contentTypes.observe(viewLifecycleOwner, this::onContentTypesChanged)
filter.contentRating.observe(viewLifecycleOwner, this::onContentRatingChanged)
@@ -103,6 +104,7 @@ class FilterSheetFragment : BaseAdaptiveSheet<SheetFilterBinding>(),
binding.chipsDemographics.onChipClickListener = this
binding.chipsGenres.onChipClickListener = this
binding.chipsGenresExclude.onChipClickListener = this
binding.chipsAuthor.onChipClickListener = this
binding.chipsSavedFilters.onChipLongClickListener = this
binding.chipsSavedFilters.onChipCloseClickListener = this
binding.sliderYear.addOnChangeListener(this::onSliderValueChange)
@@ -199,6 +201,11 @@ class FilterSheetFragment : BaseAdaptiveSheet<SheetFilterBinding>(),
is ContentRating -> filter.toggleContentRating(data, !chip.isChecked)
is Demographic -> filter.toggleDemographic(data, !chip.isChecked)
is PersistableFilter -> filter.setAdjusted(data.filter)
is String -> if (chip.isChecked) {
filter.setAuthor(null)
} else {
filter.setAuthor(data)
}
null -> router.showTagsCatalogSheet(excludeMode = chip.parentView?.id == R.id.chips_genresExclude)
}
}
@@ -312,6 +319,22 @@ class FilterSheetFragment : BaseAdaptiveSheet<SheetFilterBinding>(),
b.chipsGenresExclude.setChips(chips)
}
private fun onAuthorsChanged(value: FilterProperty<String>) {
val b = viewBinding ?: return
b.layoutAuthor.isGone = value.isEmpty()
if (value.isEmpty()) {
return
}
val chips = value.availableItems.map { author ->
ChipsView.ChipModel(
title = author,
isChecked = author in value.selectedItems,
data = author,
)
}
b.chipsAuthor.setChips(chips)
}
private fun onStateChanged(value: FilterProperty<MangaState>) {
val b = viewBinding ?: return
b.layoutState.isGone = value.isEmpty()

View File

@@ -169,4 +169,8 @@ class MangaSearchRepository @Inject constructor(
null,
)?.use { cursor -> cursor.count } ?: 0
}
suspend fun getAuthors(source: MangaSource, limit: Int): List<String> {
return db.getMangaDao().findAuthorsBySource(source.name, limit)
}
}

View File

@@ -162,6 +162,24 @@
</org.koitharu.kotatsu.filter.ui.FilterFieldLayout>
<org.koitharu.kotatsu.filter.ui.FilterFieldLayout
android:id="@+id/layout_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_small"
app:showMoreButton="false"
app:title="@string/author">
<org.koitharu.kotatsu.core.ui.widgets.ChipsView
android:id="@+id/chips_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_small"
android:layout_marginTop="@dimen/margin_small"
app:chipStyle="@style/Widget.Kotatsu.Chip.Filter" />
</org.koitharu.kotatsu.filter.ui.FilterFieldLayout>
<org.koitharu.kotatsu.filter.ui.FilterFieldLayout
android:id="@+id/layout_types"
android:layout_width="match_parent"