From f30ab56fd0a2a748f84dd698874cb16d7a61bd62 Mon Sep 17 00:00:00 2001 From: Ota <106656898+Opi-Txm@users.noreply.github.com> Date: Sun, 5 Nov 2023 04:47:32 +0900 Subject: [PATCH] New alphabetical chapter sort (#10073) * added alphabetical chapter sorting * Deleted sort_by_alphabet and re-utilized action_sort_alpha * Accidentally deleted wrong string. Now solved * Accidentally deleted wrong string. Now solved Deleted sort_by_source instead of sort_by_alphabet in strings.xml. Now reverted. * Alphabetical sorting now uses Collator * Clean up repeated Collator instances --------- Co-authored-by: arkon --- .../interactor/GetSourcesWithFavoriteCount.kt | 9 ++------- .../presentation/manga/ChapterSettingsDialog.kt | 1 + .../tachiyomi/ui/library/LibraryScreenModel.kt | 9 ++------- .../java/tachiyomi/core/util/lang/SortUtil.kt | 15 +++++++++++++++ .../domain/chapter/service/ChapterSort.kt | 5 +++++ .../java/tachiyomi/domain/manga/model/Manga.kt | 1 + 6 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 core/src/main/java/tachiyomi/core/util/lang/SortUtil.kt diff --git a/app/src/main/java/eu/kanade/domain/source/interactor/GetSourcesWithFavoriteCount.kt b/app/src/main/java/eu/kanade/domain/source/interactor/GetSourcesWithFavoriteCount.kt index 8ca143cc1..c37be75aa 100644 --- a/app/src/main/java/eu/kanade/domain/source/interactor/GetSourcesWithFavoriteCount.kt +++ b/app/src/main/java/eu/kanade/domain/source/interactor/GetSourcesWithFavoriteCount.kt @@ -3,12 +3,11 @@ package eu.kanade.domain.source.interactor import eu.kanade.domain.source.service.SourcePreferences import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine +import tachiyomi.core.util.lang.compareToWithCollator import tachiyomi.domain.source.model.Source import tachiyomi.domain.source.repository.SourceRepository import tachiyomi.source.local.isLocal -import java.text.Collator import java.util.Collections -import java.util.Locale class GetSourcesWithFavoriteCount( private val repository: SourceRepository, @@ -31,17 +30,13 @@ class GetSourcesWithFavoriteCount( direction: SetMigrateSorting.Direction, sorting: SetMigrateSorting.Mode, ): java.util.Comparator> { - val locale = Locale.getDefault() - val collator = Collator.getInstance(locale).apply { - strength = Collator.PRIMARY - } val sortFn: (Pair, Pair) -> Int = { a, b -> when (sorting) { SetMigrateSorting.Mode.ALPHABETICAL -> { when { a.first.isStub && b.first.isStub.not() -> -1 b.first.isStub && a.first.isStub.not() -> 1 - else -> collator.compare(a.first.name.lowercase(locale), b.first.name.lowercase(locale)) + else -> a.first.name.lowercase().compareToWithCollator(b.first.name.lowercase()) } } SetMigrateSorting.Mode.TOTAL -> { diff --git a/app/src/main/java/eu/kanade/presentation/manga/ChapterSettingsDialog.kt b/app/src/main/java/eu/kanade/presentation/manga/ChapterSettingsDialog.kt index b90a86fa5..dd4b29c72 100644 --- a/app/src/main/java/eu/kanade/presentation/manga/ChapterSettingsDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/manga/ChapterSettingsDialog.kt @@ -144,6 +144,7 @@ private fun ColumnScope.SortPage( R.string.sort_by_source to Manga.CHAPTER_SORTING_SOURCE, R.string.sort_by_number to Manga.CHAPTER_SORTING_NUMBER, R.string.sort_by_upload_date to Manga.CHAPTER_SORTING_UPLOAD_DATE, + R.string.action_sort_alpha to Manga.CHAPTER_SORTING_ALPHABET, ).map { (titleRes, mode) -> SortItem( label = stringResource(titleRes), 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 0120abcfc..fabfaa4b0 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 @@ -41,6 +41,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.update import tachiyomi.core.preference.CheckboxState import tachiyomi.core.preference.TriState +import tachiyomi.core.util.lang.compareToWithCollator import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.core.util.lang.withIOContext @@ -65,9 +66,7 @@ import tachiyomi.domain.track.model.Track import tachiyomi.source.local.isLocal import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.text.Collator import java.util.Collections -import java.util.Locale /** * Typealias for the library manga, using the category as keys, and list of manga as values. @@ -243,12 +242,8 @@ class LibraryScreenModel( // Map> trackMap: Map>, ): LibraryMap { - val locale = Locale.getDefault() - val collator = Collator.getInstance(locale).apply { - strength = Collator.PRIMARY - } val sortAlphabetically: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> - collator.compare(i1.libraryManga.manga.title.lowercase(locale), i2.libraryManga.manga.title.lowercase(locale)) + i1.libraryManga.manga.title.lowercase().compareToWithCollator(i2.libraryManga.manga.title.lowercase()) } val defaultTrackerScoreSortValue = -1.0 diff --git a/core/src/main/java/tachiyomi/core/util/lang/SortUtil.kt b/core/src/main/java/tachiyomi/core/util/lang/SortUtil.kt new file mode 100644 index 000000000..03c15d43b --- /dev/null +++ b/core/src/main/java/tachiyomi/core/util/lang/SortUtil.kt @@ -0,0 +1,15 @@ +package tachiyomi.core.util.lang + +import java.text.Collator +import java.util.Locale + +private val collator by lazy { + val locale = Locale.getDefault() + Collator.getInstance(locale).apply { + strength = Collator.PRIMARY + } +} + +fun String.compareToWithCollator(other: String): Int { + return collator.compare(this, other) +} diff --git a/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterSort.kt b/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterSort.kt index 45f302455..d55c51334 100644 --- a/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterSort.kt +++ b/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterSort.kt @@ -1,5 +1,6 @@ package tachiyomi.domain.chapter.service +import tachiyomi.core.util.lang.compareToWithCollator import tachiyomi.domain.chapter.model.Chapter import tachiyomi.domain.manga.model.Manga @@ -23,6 +24,10 @@ fun getChapterSort( true -> { c1, c2 -> c2.dateUpload.compareTo(c1.dateUpload) } false -> { c1, c2 -> c1.dateUpload.compareTo(c2.dateUpload) } } + Manga.CHAPTER_SORTING_ALPHABET -> when (sortDescending) { + true -> { c1, c2 -> c2.name.compareToWithCollator(c1.name) } + false -> { c1, c2 -> c1.name.compareToWithCollator(c2.name) } + } else -> throw NotImplementedError("Invalid chapter sorting method: ${manga.sorting}") } } diff --git a/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt b/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt index 07451c05b..f694355a4 100644 --- a/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt +++ b/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt @@ -85,6 +85,7 @@ data class Manga( const val CHAPTER_SORTING_SOURCE = 0x00000000L const val CHAPTER_SORTING_NUMBER = 0x00000100L const val CHAPTER_SORTING_UPLOAD_DATE = 0x00000200L + const val CHAPTER_SORTING_ALPHABET = 0x00000300L const val CHAPTER_SORTING_MASK = 0x00000300L const val CHAPTER_DISPLAY_NAME = 0x00000000L