From 2ebaacfc898a42e87225323281e70518bb36b564 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 27 Dec 2019 07:18:30 -0500 Subject: [PATCH] Replace dependency for case insensitive natural sorting (#2389) Replace dependency for case insensitive natural sorting --- app/build.gradle | 3 --- .../eu/kanade/tachiyomi/source/LocalSource.kt | 26 ++++++++++--------- .../ui/reader/loader/DirectoryPageLoader.kt | 6 ++--- .../ui/reader/loader/RarPageLoader.kt | 6 ++--- .../ui/reader/loader/ZipPageLoader.kt | 6 ++--- .../kanade/tachiyomi/util/ComparatorUtil.kt | 5 ++++ 6 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/ComparatorUtil.kt diff --git a/app/build.gradle b/app/build.gradle index c3becab094..05b5b64916 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -191,9 +191,6 @@ dependencies { // Crash reports implementation 'ch.acra:acra:4.9.2' - // Sort - implementation 'com.github.gpanther:java-nat-sort:natural-comparator-1.1' - // UI implementation 'com.dmitrymalkovich.android:material-design-dimens:1.4' implementation 'com.github.dmytrodanylyk.android-process-button:library:1.0.4' diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt index 4dd8d4936b..1630e723d7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt @@ -2,20 +2,24 @@ package eu.kanade.tachiyomi.source import android.content.Context import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.source.model.* +import eu.kanade.tachiyomi.source.model.Filter +import eu.kanade.tachiyomi.source.model.FilterList +import eu.kanade.tachiyomi.source.model.MangasPage +import eu.kanade.tachiyomi.source.model.Page +import eu.kanade.tachiyomi.source.model.SChapter +import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.util.ChapterRecognition +import eu.kanade.tachiyomi.util.ComparatorUtil.CaseInsensitiveNaturalComparator import eu.kanade.tachiyomi.util.DiskUtil import eu.kanade.tachiyomi.util.EpubFile import eu.kanade.tachiyomi.util.ImageUtil import junrar.Archive import junrar.rarfile.FileHeader -import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import rx.Observable import timber.log.Timber import java.io.File import java.io.FileInputStream import java.io.InputStream -import java.util.Comparator import java.util.Locale import java.util.concurrent.TimeUnit import java.util.zip.ZipEntry @@ -125,7 +129,6 @@ class LocalSource(private val context: Context) : CatalogueSource { override fun fetchMangaDetails(manga: SManga) = Observable.just(manga) override fun fetchChapterList(manga: SManga): Observable> { - val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance() val chapters = getBaseDirectories(context) .mapNotNull { File(it, manga.url).listFiles()?.toList() } .flatten() @@ -146,7 +149,7 @@ class LocalSource(private val context: Context) : CatalogueSource { } .sortedWith(Comparator { c1, c2 -> val c = c2.chapter_number.compareTo(c1.chapter_number) - if (c == 0) comparator.compare(c2.name, c1.name) else c + if (c == 0) CaseInsensitiveNaturalComparator.compare(c2.name, c1.name) else c }) return Observable.just(chapters) @@ -189,20 +192,19 @@ class LocalSource(private val context: Context) : CatalogueSource { private fun updateCover(chapter: SChapter, manga: SManga): File? { val format = getFormat(chapter) - val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance() return when (format) { is Format.Directory -> { val entry = format.file.listFiles() - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.name, f2.name) }) - .find { !it.isDirectory && ImageUtil.isImage(it.name, { FileInputStream(it) }) } + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.name, f2.name) }) + .find { !it.isDirectory && ImageUtil.isImage(it.name) { FileInputStream(it) } } entry?.let { updateCover(context, manga, it.inputStream())} } is Format.Zip -> { ZipFile(format.file).use { zip -> val entry = zip.entries().toList() - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.name, f2.name) }) - .find { !it.isDirectory && ImageUtil.isImage(it.name, { zip.getInputStream(it) }) } + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.name, f2.name) }) + .find { !it.isDirectory && ImageUtil.isImage(it.name) { zip.getInputStream(it) } } entry?.let { updateCover(context, manga, zip.getInputStream(it) )} } @@ -210,8 +212,8 @@ class LocalSource(private val context: Context) : CatalogueSource { is Format.Rar -> { Archive(format.file).use { archive -> val entry = archive.fileHeaders - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.fileNameString, f2.fileNameString) }) - .find { !it.isDirectory && ImageUtil.isImage(it.fileNameString, { archive.getInputStream(it) }) } + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.fileNameString, f2.fileNameString) }) + .find { !it.isDirectory && ImageUtil.isImage(it.fileNameString) { archive.getInputStream(it) } } entry?.let { updateCover(context, manga, archive.getInputStream(it) )} } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DirectoryPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DirectoryPageLoader.kt index d42efcef06..8d70d4a760 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DirectoryPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DirectoryPageLoader.kt @@ -2,8 +2,8 @@ package eu.kanade.tachiyomi.ui.reader.loader import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage +import eu.kanade.tachiyomi.util.ComparatorUtil.CaseInsensitiveNaturalComparator import eu.kanade.tachiyomi.util.ImageUtil -import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import rx.Observable import java.io.File import java.io.FileInputStream @@ -18,11 +18,9 @@ class DirectoryPageLoader(val file: File) : PageLoader() { * comparator. */ override fun getPages(): Observable> { - val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance() - return file.listFiles() .filter { !it.isDirectory && ImageUtil.isImage(it.name) { FileInputStream(it) } } - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.name, f2.name) }) + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.name, f2.name) }) .mapIndexed { i, file -> val streamFn = { FileInputStream(file) } ReaderPage(i).apply { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt index dcba332aaa..4a30378a6d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt @@ -2,10 +2,10 @@ package eu.kanade.tachiyomi.ui.reader.loader import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage +import eu.kanade.tachiyomi.util.ComparatorUtil.CaseInsensitiveNaturalComparator import eu.kanade.tachiyomi.util.ImageUtil import junrar.Archive import junrar.rarfile.FileHeader -import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import rx.Observable import java.io.File import java.io.InputStream @@ -42,11 +42,9 @@ class RarPageLoader(file: File) : PageLoader() { * comparator. */ override fun getPages(): Observable> { - val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance() - return archive.fileHeaders .filter { !it.isDirectory && ImageUtil.isImage(it.fileNameString) { archive.getInputStream(it) } } - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.fileNameString, f2.fileNameString) }) + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.fileNameString, f2.fileNameString) }) .mapIndexed { i, header -> val streamFn = { getStream(header) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ZipPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ZipPageLoader.kt index 3cd1b541e8..78d7488847 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ZipPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ZipPageLoader.kt @@ -2,8 +2,8 @@ package eu.kanade.tachiyomi.ui.reader.loader import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage +import eu.kanade.tachiyomi.util.ComparatorUtil.CaseInsensitiveNaturalComparator import eu.kanade.tachiyomi.util.ImageUtil -import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import rx.Observable import java.io.File import java.util.zip.ZipEntry @@ -32,11 +32,9 @@ class ZipPageLoader(file: File) : PageLoader() { * comparator. */ override fun getPages(): Observable> { - val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance() - return zip.entries().toList() .filter { !it.isDirectory && ImageUtil.isImage(it.name) { zip.getInputStream(it) } } - .sortedWith(Comparator { f1, f2 -> comparator.compare(f1.name, f2.name) }) + .sortedWith(Comparator { f1, f2 -> CaseInsensitiveNaturalComparator.compare(f1.name, f2.name) }) .mapIndexed { i, entry -> val streamFn = { zip.getInputStream(entry) } ReaderPage(i).apply { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ComparatorUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ComparatorUtil.kt new file mode 100644 index 0000000000..aff3603b65 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ComparatorUtil.kt @@ -0,0 +1,5 @@ +package eu.kanade.tachiyomi.util + +object ComparatorUtil { + val CaseInsensitiveNaturalComparator = compareBy(String.CASE_INSENSITIVE_ORDER) { it }.then(naturalOrder()) +}