From ba8abd94a8bcbe409e96cf7d606852a789e439ed Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 1 Oct 2021 23:37:43 +0200 Subject: [PATCH] Ability to order sources by library count when migrating (#6000) * order sources by library count when migrating (closes #4703) * Use plain menu instead of full-on sheet --- .../data/preference/PreferenceKeys.kt | 3 ++ .../data/preference/PreferencesHelper.kt | 4 ++ .../sources/MigrationSourcesController.kt | 35 ++++++++++++++- .../sources/MigrationSourcesPresenter.kt | 44 ++++++++++++++++++- app/src/main/res/menu/browse_migrate.xml | 33 ++++++++++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 118 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 0cb735fcaa..b20d11a1b9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -151,6 +151,9 @@ object PreferenceKeys { const val librarySortingMode = "library_sorting_mode" const val librarySortingDirection = "library_sorting_ascending" + const val migrationSortingMode = "pref_migration_sorting" + const val migrationSortingDirection = "pref_migration_direction" + const val automaticExtUpdates = "automatic_ext_updates" const val showNsfwSource = "show_nsfw_source" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index ba6fe1e7be..7f84ace3e1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferenceValues.ThemeMode.system import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.anilist.Anilist +import eu.kanade.tachiyomi.ui.browse.migration.sources.MigrationSourcesController import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting @@ -267,6 +268,9 @@ class PreferencesHelper(val context: Context) { fun librarySortingMode() = flowPrefs.getEnum(Keys.librarySortingMode, SortModeSetting.ALPHABETICAL) fun librarySortingAscending() = flowPrefs.getEnum(Keys.librarySortingDirection, SortDirectionSetting.ASCENDING) + fun migrationSortingMode() = flowPrefs.getEnum(Keys.migrationSortingMode, MigrationSourcesController.SortSetting.ALPHABETICAL) + fun migrationSortingDirection() = flowPrefs.getEnum(Keys.migrationSortingDirection, MigrationSourcesController.DirectionSetting.ASCENDING) + fun automaticExtUpdates() = flowPrefs.getBoolean(Keys.automaticExtUpdates, true) fun showNsfwSource() = flowPrefs.getBoolean(Keys.showNsfwSource, true) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesController.kt index 803bd89642..166fb22a61 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesController.kt @@ -9,16 +9,20 @@ import androidx.recyclerview.widget.LinearLayoutManager import dev.chrisbanes.insetter.applyInsetter import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.databinding.MigrationSourcesControllerBinding import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.browse.migration.manga.MigrationMangaController import eu.kanade.tachiyomi.util.system.openInBrowser +import uy.kohesive.injekt.injectLazy class MigrationSourcesController : NucleusController(), FlexibleAdapter.OnItemClickListener { + private val preferences: PreferencesHelper by injectLazy() + private var adapter: SourceAdapter? = null init { @@ -56,12 +60,31 @@ class MigrationSourcesController : } override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { + when (val itemId = item.itemId) { R.id.action_source_migration_help -> activity?.openInBrowser(HELP_URL) + R.id.asc_alphabetical, R.id.desc_alphabetical -> { + setSortingDirection(SortSetting.ALPHABETICAL, itemId == R.id.asc_alphabetical) + } + R.id.asc_count, R.id.desc_count -> { + setSortingDirection(SortSetting.TOTAL, itemId == R.id.asc_count) + } } return super.onOptionsItemSelected(item) } + private fun setSortingDirection(sortSetting: SortSetting, isAscending: Boolean) { + val direction = if (isAscending) { + DirectionSetting.ASCENDING + } else { + DirectionSetting.DESCENDING + } + + preferences.migrationSortingDirection().set(direction) + preferences.migrationSortingMode().set(sortSetting) + + presenter.requestSortUpdate() + } + fun setSources(sourcesWithManga: List) { // Show empty view if needed if (sourcesWithManga.isNotEmpty()) { @@ -79,6 +102,16 @@ class MigrationSourcesController : parentController!!.router.pushController(controller.withFadeTransaction()) return false } + + enum class DirectionSetting { + ASCENDING, + DESCENDING; + } + + enum class SortSetting { + ALPHABETICAL, + TOTAL; + } } private const val HELP_URL = "https://tachiyomi.org/help/guides/source-migration/" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt index ed6fb52d28..744046a013 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt @@ -1,25 +1,38 @@ package eu.kanade.tachiyomi.ui.browse.migration.sources import android.os.Bundle +import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter +import eu.kanade.tachiyomi.util.lang.combineLatest import rx.android.schedulers.AndroidSchedulers +import rx.schedulers.Schedulers import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy +import java.text.Collator +import java.util.Collections +import java.util.Locale class MigrationSourcesPresenter( private val sourceManager: SourceManager = Injekt.get(), private val db: DatabaseHelper = Injekt.get() ) : BasePresenter() { + private val preferences: PreferencesHelper by injectLazy() + + private val sortRelay = BehaviorRelay.create(Unit) + override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) db.getFavoriteMangas() .asRxObservable() + .combineLatest(sortRelay.observeOn(Schedulers.io())) { sources, _ -> sources } .observeOn(AndroidSchedulers.mainThread()) .map { findSourcesWithManga(it) } .subscribeLatestCache(MigrationSourcesController::setSources) @@ -34,7 +47,36 @@ class MigrationSourcesPresenter( val source = sourceManager.getOrStub(it.key) SourceItem(source, it.value.size, header) } - .sortedBy { it.source.name.lowercase() } + .sortedWith(sortFn()) .toList() } + + fun sortFn(): java.util.Comparator { + val sort by lazy { + preferences.migrationSortingMode().get() + } + val direction by lazy { + preferences.migrationSortingDirection().get() + } + + val locale = Locale.getDefault() + val collator = Collator.getInstance(locale).apply { + strength = Collator.PRIMARY + } + val sortFn: (SourceItem, SourceItem) -> Int = { a, b -> + when (sort) { + MigrationSourcesController.SortSetting.ALPHABETICAL -> collator.compare(a.source.name.lowercase(locale), b.source.name.lowercase(locale)) + MigrationSourcesController.SortSetting.TOTAL -> a.mangaCount.compareTo(b.mangaCount) + } + } + + return when (direction) { + MigrationSourcesController.DirectionSetting.ASCENDING -> Comparator(sortFn) + MigrationSourcesController.DirectionSetting.DESCENDING -> Collections.reverseOrder(sortFn) + } + } + + fun requestSortUpdate() { + sortRelay.call(Unit) + } } diff --git a/app/src/main/res/menu/browse_migrate.xml b/app/src/main/res/menu/browse_migrate.xml index bc4155f0fb..1d1d557b3e 100644 --- a/app/src/main/res/menu/browse_migrate.xml +++ b/app/src/main/res/menu/browse_migrate.xml @@ -7,5 +7,38 @@ android:title="@string/migration_help_guide" app:iconTint="?attr/colorOnToolbar" app:showAsAction="ifRoom" /> + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3f940deb86..09f7861d0d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -39,6 +39,7 @@ Unread Remove filter Alphabetically + Total manga Total chapters Last read Last checked