From 2f4bb7cadb0297492cfb21393e75ca276e0539d7 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Sat, 2 Nov 2024 21:20:07 +0600 Subject: [PATCH] Cleanup some code --- .../eu/kanade/core/util/CollectionUtils.kt | 61 +------------------ .../migration/search/SourceSearchScreen.kt | 3 +- .../source/browse/BrowseSourceScreen.kt | 4 +- .../source/browse/BrowseSourceScreenModel.kt | 10 ++- .../ui/library/LibraryScreenModel.kt | 6 +- .../tachiyomi/ui/stats/StatsScreenModel.kt | 6 +- 6 files changed, 14 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt index baf369e39..e8eed9d3d 100644 --- a/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt +++ b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt @@ -1,5 +1,6 @@ package eu.kanade.core.util +import androidx.compose.ui.util.fastFilter import androidx.compose.ui.util.fastForEach import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -45,21 +46,6 @@ fun HashSet.addOrRemove(value: E, shouldAdd: Boolean) { } } -/** - * Returns a list containing only elements matching the given [predicate]. - * - * **Do not use for collections that come from public APIs**, since they may not support random - * access in an efficient way, and this method may actually be a lot slower. Only use for - * collections that are created by code we control and are known to support random access. - */ -@OptIn(ExperimentalContracts::class) -inline fun List.fastFilter(predicate: (T) -> Boolean): List { - contract { callsInPlace(predicate) } - val destination = ArrayList() - fastForEach { if (predicate(it)) destination.add(it) } - return destination -} - /** * Returns a list containing all elements not matching the given [predicate]. * @@ -70,27 +56,7 @@ inline fun List.fastFilter(predicate: (T) -> Boolean): List { @OptIn(ExperimentalContracts::class) inline fun List.fastFilterNot(predicate: (T) -> Boolean): List { contract { callsInPlace(predicate) } - val destination = ArrayList() - fastForEach { if (!predicate(it)) destination.add(it) } - return destination -} - -/** - * Returns a list containing only the non-null results of applying the - * given [transform] function to each element in the original collection. - * - * **Do not use for collections that come from public APIs**, since they may not support random - * access in an efficient way, and this method may actually be a lot slower. Only use for - * collections that are created by code we control and are known to support random access. - */ -@OptIn(ExperimentalContracts::class) -inline fun List.fastMapNotNull(transform: (T) -> R?): List { - contract { callsInPlace(transform) } - val destination = ArrayList() - fastForEach { element -> - transform(element)?.let(destination::add) - } - return destination + return fastFilter { !predicate(it) } } /** @@ -131,26 +97,3 @@ inline fun List.fastCountNot(predicate: (T) -> Boolean): Int { fastForEach { if (predicate(it)) --count } return count } - -/** - * Returns a list containing only elements from the given collection - * having distinct keys returned by the given [selector] function. - * - * Among elements of the given collection with equal keys, only the first one will be present in the resulting list. - * The elements in the resulting list are in the same order as they were in the source collection. - * - * **Do not use for collections that come from public APIs**, since they may not support random - * access in an efficient way, and this method may actually be a lot slower. Only use for - * collections that are created by code we control and are known to support random access. - */ -@OptIn(ExperimentalContracts::class) -inline fun List.fastDistinctBy(selector: (T) -> K): List { - contract { callsInPlace(selector) } - val set = HashSet() - val list = ArrayList() - fastForEach { - val key = selector(it) - if (set.add(key)) list.add(it) - } - return list -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt index 533dbd3af..9cf50b2af 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt @@ -81,13 +81,12 @@ data class SourceSearchScreen( }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, ) { paddingValues -> - val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState() val openMigrateDialog: (Manga) -> Unit = { screenModel.setDialog(BrowseSourceScreenModel.Dialog.Migrate(newManga = it, oldManga = oldManga)) } BrowseSourceContent( source = screenModel.source, - mangaList = pagingFlow.collectAsLazyPagingItems(), + mangaList = screenModel.mangaPagerFlow.collectAsLazyPagingItems(), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), displayMode = screenModel.displayMode, snackbarHostState = snackbarHostState, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt index 152b2adfe..283351093 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt @@ -206,11 +206,9 @@ data class BrowseSourceScreen( }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, ) { paddingValues -> - val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState() - BrowseSourceContent( source = screenModel.source, - mangaList = pagingFlow.collectAsLazyPagingItems(), + mangaList = screenModel.mangaPagerFlow.collectAsLazyPagingItems(), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), displayMode = screenModel.displayMode, snackbarHostState = snackbarHostState, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt index dff062503..5761c5320 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt @@ -26,11 +26,10 @@ import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.util.removeCovers import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList -import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update @@ -106,9 +105,9 @@ class BrowseSourceScreenModel( * Flow of Pager flow tied to [State.listing] */ private val hideInLibraryItems = sourcePreferences.hideInLibraryItems().get() - val mangaPagerFlowFlow = state.map { it.listing } + val mangaPagerFlow = state.map { it.listing } .distinctUntilChanged() - .map { listing -> + .flatMapLatest { listing -> Pager(PagingConfig(pageSize = 25)) { getRemoteManga.subscribe(sourceId, listing.query ?: "", listing.filters) }.flow.map { pagingData -> @@ -120,9 +119,8 @@ class BrowseSourceScreenModel( } .filter { !hideInLibraryItems || !it.value.favorite } } - .cachedIn(ioCoroutineScope) } - .stateIn(ioCoroutineScope, SharingStarted.Lazily, emptyFlow()) + .cachedIn(ioCoroutineScope) fun getColumnsPreference(orientation: Int): GridCells { val isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt index 794dac4d0..7d6350a63 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt @@ -4,15 +4,15 @@ import androidx.compose.runtime.Immutable import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.util.fastAny +import androidx.compose.ui.util.fastDistinctBy +import androidx.compose.ui.util.fastFilter import androidx.compose.ui.util.fastMap +import androidx.compose.ui.util.fastMapNotNull import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.screenModelScope import eu.kanade.core.preference.PreferenceMutableState import eu.kanade.core.preference.asState -import eu.kanade.core.util.fastDistinctBy -import eu.kanade.core.util.fastFilter import eu.kanade.core.util.fastFilterNot -import eu.kanade.core.util.fastMapNotNull import eu.kanade.core.util.fastPartition import eu.kanade.domain.base.BasePreferences import eu.kanade.domain.chapter.interactor.SetReadStatus diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/stats/StatsScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/stats/StatsScreenModel.kt index 174ac26dc..41f02060d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/stats/StatsScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/stats/StatsScreenModel.kt @@ -1,12 +1,12 @@ package eu.kanade.tachiyomi.ui.stats +import androidx.compose.ui.util.fastDistinctBy +import androidx.compose.ui.util.fastFilter +import androidx.compose.ui.util.fastMapNotNull import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.screenModelScope import eu.kanade.core.util.fastCountNot -import eu.kanade.core.util.fastDistinctBy -import eu.kanade.core.util.fastFilter import eu.kanade.core.util.fastFilterNot -import eu.kanade.core.util.fastMapNotNull import eu.kanade.presentation.more.stats.StatsScreenState import eu.kanade.presentation.more.stats.data.StatsData import eu.kanade.tachiyomi.data.download.DownloadManager