From fff0b17adddc51d9123a4ccca82041cd115863af Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 22 Jul 2023 10:40:58 +0530 Subject: [PATCH] Convert some interface implementations to objects --- .../kotatsu/bookmarks/domain/Bookmark.kt | 2 +- .../kotatsu/core/util/AlphanumComparator.kt | 3 +-- .../koitharu/kotatsu/local/data/CbzFilter.kt | 20 ++++++++----------- .../kotatsu/local/data/ImageFileFilter.kt | 3 +-- .../local/data/LocalMangaRepository.kt | 9 +++------ .../koitharu/kotatsu/local/data/MangaIndex.kt | 4 ++-- .../local/data/input/LocalMangaDirInput.kt | 18 ++++++++--------- .../local/data/input/LocalMangaZipInput.kt | 7 ++++--- 8 files changed, 29 insertions(+), 37 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt b/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt index 1dc908cc4..3b2a7f463 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt @@ -28,7 +28,7 @@ class Bookmark( private fun isImageUrlDirect(): Boolean { val extension = imageUrl.substringAfterLast('.') - return extension.isNotEmpty() && ImageFileFilter().isExtensionValid(extension) + return extension.isNotEmpty() && ImageFileFilter.isExtensionValid(extension) } override fun equals(other: Any?): Boolean { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/AlphanumComparator.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/AlphanumComparator.kt index 46867633e..1c615a4bf 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/AlphanumComparator.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/AlphanumComparator.kt @@ -1,7 +1,6 @@ package org.koitharu.kotatsu.core.util -class AlphanumComparator : Comparator { - +object AlphanumComparator : Comparator { override fun compare(s1: String?, s2: String?): Int { if (s1 == null || s2 == null) { return 0 diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/CbzFilter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/CbzFilter.kt index 24e9f8a2d..55cd7e287 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/CbzFilter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/CbzFilter.kt @@ -6,8 +6,7 @@ import java.io.FileFilter import java.io.FilenameFilter import java.util.Locale -class CbzFilter : FileFilter, FilenameFilter { - +object CbzFilter : FileFilter, FilenameFilter { override fun accept(dir: File, name: String): Boolean { return isFileSupported(name) } @@ -16,16 +15,13 @@ class CbzFilter : FileFilter, FilenameFilter { return isFileSupported(pathname?.name ?: return false) } - companion object { - - fun isFileSupported(name: String): Boolean { - val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT) - return ext == "cbz" || ext == "zip" - } + fun isFileSupported(name: String): Boolean { + val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT) + return ext == "cbz" || ext == "zip" + } - fun isUriSupported(uri: Uri): Boolean { - val scheme = uri.scheme?.lowercase(Locale.ROOT) - return scheme != null && scheme == "cbz" || scheme == "zip" - } + fun isUriSupported(uri: Uri): Boolean { + val scheme = uri.scheme?.lowercase(Locale.ROOT) + return scheme != null && scheme == "cbz" || scheme == "zip" } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/ImageFileFilter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/ImageFileFilter.kt index 4dc76c9f6..84771ed3f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/ImageFileFilter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/ImageFileFilter.kt @@ -6,8 +6,7 @@ import java.io.FilenameFilter import java.util.Locale import java.util.zip.ZipEntry -class ImageFileFilter : FilenameFilter, FileFilter { - +object ImageFileFilter : FilenameFilter, FileFilter { override fun accept(dir: File, name: String): Boolean { val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT) return isExtensionValid(ext) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/LocalMangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/LocalMangaRepository.kt index 439cdc847..990748e08 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/LocalMangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/LocalMangaRepository.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.runInterruptible import org.koitharu.kotatsu.core.model.isLocal import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.prefs.AppSettings +import org.koitharu.kotatsu.core.util.AlphanumComparator import org.koitharu.kotatsu.core.util.CompositeMutex import org.koitharu.kotatsu.core.util.ext.deleteAwait import org.koitharu.kotatsu.local.data.input.LocalMangaInput @@ -29,7 +30,6 @@ import org.koitharu.kotatsu.parsers.model.SortOrder import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import java.io.File -import java.io.FilenameFilter import java.util.EnumSet import javax.inject.Inject import javax.inject.Singleton @@ -74,12 +74,9 @@ class LocalMangaRepository @Inject constructor( list.retainAll { x -> x.containsTags(tags) } } when (sortOrder) { - SortOrder.ALPHABETICAL -> list.sortWith(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.manga.title }) + SortOrder.ALPHABETICAL -> list.sortWith(compareBy(AlphanumComparator) { x -> x.manga.title }) SortOrder.RATING -> list.sortByDescending { it.manga.rating } - SortOrder.NEWEST, - SortOrder.UPDATED, - -> list.sortByDescending { it.createdAt } - + SortOrder.NEWEST, SortOrder.UPDATED -> list.sortByDescending { it.createdAt } else -> Unit } return list.unwrap() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/MangaIndex.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/MangaIndex.kt index 6d08885af..7ca16797c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/MangaIndex.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/MangaIndex.kt @@ -4,6 +4,7 @@ import androidx.annotation.WorkerThread import org.json.JSONArray import org.json.JSONObject import org.koitharu.kotatsu.BuildConfig +import org.koitharu.kotatsu.core.util.AlphanumComparator import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaSource @@ -125,8 +126,7 @@ class MangaIndex(source: String?) { item.put("id", id) list.add(item) } - val comparator = org.koitharu.kotatsu.core.util.AlphanumComparator() - list.sortWith(compareBy(comparator) { it.getString("name") }) + list.sortWith(compareBy(AlphanumComparator) { it.getString("name") }) val newJo = JSONObject() list.forEachIndexed { i, obj -> obj.put("number", i + 1) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaDirInput.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaDirInput.kt index c2cac2c60..60bd93fe0 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaDirInput.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaDirInput.kt @@ -4,6 +4,7 @@ import androidx.core.net.toFile import androidx.core.net.toUri import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runInterruptible +import org.koitharu.kotatsu.core.util.AlphanumComparator import org.koitharu.kotatsu.core.util.ext.listFilesRecursive import org.koitharu.kotatsu.core.util.ext.longHashCode import org.koitharu.kotatsu.core.util.ext.toListSorted @@ -87,8 +88,8 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) { override suspend fun getPages(chapter: MangaChapter): List = runInterruptible(Dispatchers.IO) { val file = chapter.url.toUri().toFile() if (file.isDirectory) { - file.listFilesRecursive(ImageFileFilter()) - .toListSorted(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name }) + file.listFilesRecursive(ImageFileFilter) + .toListSorted(compareBy(AlphanumComparator) { x -> x.name }) .map { val pageUri = it.toUri().toString() MangaPage( @@ -104,7 +105,7 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) { .asSequence() .filter { x -> !x.isDirectory } .map { it.name } - .toListSorted(org.koitharu.kotatsu.core.util.AlphanumComparator()) + .toListSorted(AlphanumComparator) .map { val pageUri = zipUri(file, it) MangaPage( @@ -120,18 +121,17 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) { private fun String.toHumanReadable() = replace("_", " ").toCamelCase() - private fun getChaptersFiles(): List = root.listFilesRecursive(CbzFilter()) - .toListSorted(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name }) + private fun getChaptersFiles(): List = root.listFilesRecursive(CbzFilter) + .toListSorted(compareBy(AlphanumComparator) { x -> x.name }) private fun findFirstImageEntry(): String? { - val filter = ImageFileFilter() - root.listFilesRecursive(filter).firstOrNull()?.let { + root.listFilesRecursive(ImageFileFilter).firstOrNull()?.let { return it.toUri().toString() } - val cbz = root.listFilesRecursive(CbzFilter()).firstOrNull() ?: return null + val cbz = root.listFilesRecursive(CbzFilter).firstOrNull() ?: return null return ZipFile(cbz).use { zip -> zip.entries().asSequence() - .firstOrNull { x -> !x.isDirectory && filter.accept(x) } + .firstOrNull { x -> !x.isDirectory && ImageFileFilter.accept(x) } ?.let { entry -> zipUri(cbz, entry.name) } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaZipInput.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaZipInput.kt index f468c4647..05156af8a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaZipInput.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/data/input/LocalMangaZipInput.kt @@ -7,6 +7,7 @@ import androidx.core.net.toFile import androidx.core.net.toUri import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runInterruptible +import org.koitharu.kotatsu.core.util.AlphanumComparator import org.koitharu.kotatsu.core.util.ext.longHashCode import org.koitharu.kotatsu.core.util.ext.readText import org.koitharu.kotatsu.core.util.ext.toListSorted @@ -70,7 +71,7 @@ class LocalMangaZipInput(root: File) : LocalMangaInput(root) { publicUrl = fileUri, source = MangaSource.LOCAL, coverUrl = zipUri(root, findFirstImageEntry(zip.entries())?.name.orEmpty()), - chapters = chapters.sortedWith(org.koitharu.kotatsu.core.util.AlphanumComparator()) + chapters = chapters.sortedWith(AlphanumComparator) .mapIndexed { i, s -> MangaChapter( id = "$i$s".longHashCode(), @@ -125,7 +126,7 @@ class LocalMangaZipInput(root: File) : LocalMangaInput(root) { } } entries - .toListSorted(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name }) + .toListSorted(compareBy(AlphanumComparator) { x -> x.name }) .map { x -> val entryUri = zipUri(file, x.name) MangaPage( @@ -141,7 +142,7 @@ class LocalMangaZipInput(root: File) : LocalMangaInput(root) { private fun findFirstImageEntry(entries: Enumeration): ZipEntry? { val list = entries.toList() .filterNot { it.isDirectory } - .sortedWith(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name }) + .sortedWith(compareBy(AlphanumComparator) { x -> x.name }) val map = MimeTypeMap.getSingleton() return list.firstOrNull { map.getMimeTypeFromExtension(it.name.substringAfterLast('.'))