From ce4ab83ae9c6032a1f9d5cfa8073eae09d35e64a 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 (cherry picked from commit ba8abd94a8bcbe409e96cf7d606852a789e439ed) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesController.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/MigrationSourcesPresenter.kt --- .../data/preference/PreferenceKeys.kt | 3 ++ .../data/preference/PreferencesHelper.kt | 4 ++ .../sources/MigrationSourcesController.kt | 36 +++++++++++++-- .../sources/MigrationSourcesPresenter.kt | 44 ++++++++++++++++++- app/src/main/res/menu/browse_migrate.xml | 33 ++++++++++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 117 insertions(+), 4 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 e3129551b..9fe35842b 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -157,6 +157,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 dc838abf7..e842286d3 100755 --- 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 @@ -275,6 +276,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 256783c56..ce6003276 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 @@ -23,6 +23,7 @@ import eu.kanade.tachiyomi.util.system.openInBrowser import exh.util.executeOnIO import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy class MigrationSourcesController : NucleusController(), @@ -31,6 +32,8 @@ class MigrationSourcesController : SourceAdapter.OnAllClickListener { // SY <-- + private val preferences: PreferencesHelper by injectLazy() + private var adapter: SourceAdapter? = null init { @@ -68,12 +71,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()) { @@ -127,7 +149,15 @@ class MigrationSourcesController : } // SY <-- - companion object { - private const val HELP_URL = "https://tachiyomi.org/help/guides/source-migration/" + 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 e70378077..42a17eceb 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,26 +1,39 @@ 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 exh.source.MERGED_SOURCE_ID 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) @@ -35,7 +48,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 bc4155f0f..1d1d557b3 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 4f7ac65b7..42b22ba5f 100755 --- 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