chore(library): Using fastFilter, fastAny, fastMap

This commit is contained in:
Cuong-Tran 2025-08-27 14:11:49 +07:00
parent 4e2445d60c
commit 21d221a048

View file

@ -190,7 +190,7 @@ class LibraryScreenModel(
// KMK <-- // KMK <--
getLibraryItemPreferencesFlow(), getLibraryItemPreferencesFlow(),
) { (searchQuery, categories, favorites), (tracksMap, trackingFilters), (includedCategories, excludedCategories), itemPreferences -> ) { (searchQuery, categories, favorites), (tracksMap, trackingFilters), (includedCategories, excludedCategories), itemPreferences ->
val showSystemCategory = favorites.any { it.libraryManga.categories.contains(0) } val showSystemCategory = favorites.fastAny { it.libraryManga.categories.contains(0) }
val filteredFavorites = favorites val filteredFavorites = favorites
.applyFilters( .applyFilters(
tracksMap, tracksMap,
@ -345,7 +345,7 @@ class LibraryScreenModel(
// SY <-- // SY <--
*trackFilters.values.toTypedArray(), *trackFilters.values.toTypedArray(),
) )
.any { it != TriState.DISABLED } || .fastAny { it != TriState.DISABLED } ||
// KMK --> // KMK -->
prefs.filterCategories prefs.filterCategories
// KMK <-- // KMK <--
@ -518,7 +518,7 @@ class LibraryScreenModel(
val filterFnCategories: (LibraryItem) -> Boolean = categories@{ item -> val filterFnCategories: (LibraryItem) -> Boolean = categories@{ item ->
if (!filterCategories) return@categories true if (!filterCategories) return@categories true
val mangaCategories = item.libraryManga.categories.filterNot { it == 0L }.toSet() val mangaCategories = item.libraryManga.categories.fastFilterNot { it == 0L }.toSet()
// Early return // Early return
if (mangaCategories.isEmpty()) { if (mangaCategories.isEmpty()) {
@ -567,9 +567,9 @@ class LibraryScreenModel(
groupCache.getOrPut(categoryId) { mutableListOf() }.add(item.id) groupCache.getOrPut(categoryId) { mutableListOf() }.add(item.id)
} }
} }
return categories.filter { showSystemCategory || !it.isSystemCategory } return categories.fastFilter { showSystemCategory || !it.isSystemCategory }
// KMK --> // KMK -->
.filterNot { !showHiddenCategories && it.hidden } .fastFilterNot { !showHiddenCategories && it.hidden }
// KMK <-- // KMK <--
.associateWith { .associateWith {
groupCache[it.id]?.toList() groupCache[it.id]?.toList()
@ -1343,7 +1343,7 @@ class LibraryScreenModel(
lastSelectionCategory = null lastSelectionCategory = null
mutableState.update { state -> mutableState.update { state ->
val newSelection = state.selection.mutate { list -> val newSelection = state.selection.mutate { list ->
state.getItemsForCategoryId(state.activeCategory?.id).map { it.id }.let(list::addAll) state.getItemsForCategoryId(state.activeCategory?.id).fastMap { it.id }.let(list::addAll)
} }
state.copy(selection = newSelection) state.copy(selection = newSelection)
} }
@ -1382,7 +1382,7 @@ class LibraryScreenModel(
// Hide the default category because it has a different behavior than the ones from db. // Hide the default category because it has a different behavior than the ones from db.
// KMK --> // KMK -->
val categories = state.value.libraryData.categories.filter { it.id != 0L } val categories = state.value.libraryData.categories.fastFilter { it.id != 0L }
// KMK <-- // KMK <--
// Get indexes of the common categories to preselect. // Get indexes of the common categories to preselect.
@ -1390,7 +1390,7 @@ class LibraryScreenModel(
// Get indexes of the mix categories to preselect. // Get indexes of the mix categories to preselect.
val mix = getMixCategories(mangaList) val mix = getMixCategories(mangaList)
val preselected = categories val preselected = categories
.map { .fastMap {
when (it) { when (it) {
in common -> CheckboxState.State.Checked(it) in common -> CheckboxState.State.Checked(it)
in mix -> CheckboxState.TriState.Exclude(it) in mix -> CheckboxState.TriState.Exclude(it)
@ -1436,7 +1436,7 @@ class LibraryScreenModel(
// KMK --> // KMK -->
val groupCache = mutableMapOf</* Track.status */ Int, MutableList</* LibraryItem */ Long>>() val groupCache = mutableMapOf</* Track.status */ Int, MutableList</* LibraryItem */ Long>>()
forEach { item -> forEach { item ->
val statuses = tracks[item.libraryManga.manga.id]?.mapNotNull { track -> val statuses = tracks[item.libraryManga.manga.id]?.fastMapNotNull { track ->
TrackStatus.parseTrackerStatus(trackerManager, track.trackerId, track.status) TrackStatus.parseTrackerStatus(trackerManager, track.trackerId, track.status)
} }
?.takeIf { it.isNotEmpty() } ?.takeIf { it.isNotEmpty() }
@ -1675,7 +1675,7 @@ class LibraryScreenModel(
} }
val showAddToMangadex: Boolean by lazy { val showAddToMangadex: Boolean by lazy {
selectedManga.any { it.source in mangaDexSourceIds } selectedManga.fastAny { it.source in mangaDexSourceIds }
} }
val showResetInfo: Boolean by lazy { val showResetInfo: Boolean by lazy {
@ -1698,7 +1698,7 @@ class LibraryScreenModel(
} }
fun getItemsForCategory(category: Category): List<LibraryItem> { fun getItemsForCategory(category: Category): List<LibraryItem> {
return groupedFavorites[category].orEmpty().mapNotNull { libraryData.favoritesById[it] } return groupedFavorites[category].orEmpty().fastMapNotNull { libraryData.favoritesById[it] }
} }
fun getItemCountForCategory(category: Category): Int? { fun getItemCountForCategory(category: Category): Int? {