From 46a4b0e0b676f72fa747a65bedbb00a68ebdb501 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 19 Nov 2021 16:42:19 -0500 Subject: [PATCH] Partially migrate LocalSource to 1.x methods --- .../eu/kanade/tachiyomi/source/LocalSource.kt | 93 +++++++++++-------- .../java/eu/kanade/tachiyomi/source/Source.kt | 6 +- 2 files changed, 56 insertions(+), 43 deletions(-) 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 6163675b57..b87e3a5e74 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt @@ -6,15 +6,19 @@ import eu.kanade.tachiyomi.R 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.source.model.toChapterInfo +import eu.kanade.tachiyomi.source.model.toMangaInfo +import eu.kanade.tachiyomi.source.model.toSChapter +import eu.kanade.tachiyomi.source.model.toSManga import eu.kanade.tachiyomi.util.chapter.ChapterRecognition import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.EpubFile import eu.kanade.tachiyomi.util.system.ImageUtil import eu.kanade.tachiyomi.util.system.logcat +import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.contentOrNull @@ -24,6 +28,8 @@ import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonPrimitive import logcat.LogPriority import rx.Observable +import tachiyomi.source.model.ChapterInfo +import tachiyomi.source.model.MangaInfo import uy.kohesive.injekt.injectLazy import java.io.File import java.io.FileInputStream @@ -133,23 +139,27 @@ class LocalSource(private val context: Context) : CatalogueSource { } } - val chapters = fetchChapterList(this).toBlocking().first() - if (chapters.isNotEmpty()) { - val chapter = chapters.last() - val format = getFormat(chapter) - if (format is Format.Epub) { - EpubFile(format.file).use { epub -> - epub.fillMangaMetadata(this) + val sManga = this + val mangaInfo = this.toMangaInfo() + runBlocking { + val chapters = getChapterList(mangaInfo) + if (chapters.isNotEmpty()) { + val chapter = chapters.last().toSChapter() + val format = getFormat(chapter) + if (format is Format.Epub) { + EpubFile(format.file).use { epub -> + epub.fillMangaMetadata(sManga) + } } - } - // Copy the cover from the first chapter found. - if (thumbnail_url == null) { - try { - val dest = updateCover(chapter, this) - thumbnail_url = dest?.absolutePath - } catch (e: Exception) { - logcat(LogPriority.ERROR, e) + // Copy the cover from the first chapter found. + if (thumbnail_url == null) { + try { + val dest = updateCover(chapter, sManga) + thumbnail_url = dest?.absolutePath + } catch (e: Exception) { + logcat(LogPriority.ERROR, e) + } } } } @@ -161,36 +171,40 @@ class LocalSource(private val context: Context) : CatalogueSource { override fun fetchLatestUpdates(page: Int) = fetchSearchManga(page, "", LATEST_FILTERS) - override fun fetchMangaDetails(manga: SManga): Observable { - getBaseDirectories(context) + override suspend fun getMangaDetails(manga: MangaInfo): MangaInfo { + val localDetails = getBaseDirectories(context) .asSequence() - .mapNotNull { File(it, manga.url).listFiles()?.toList() } + .mapNotNull { File(it, manga.key).listFiles()?.toList() } .flatten() .firstOrNull { it.extension.lowercase() == "json" } - ?.apply { - val obj = json.decodeFromStream(inputStream()) - manga.title = obj["title"]?.jsonPrimitive?.contentOrNull ?: manga.title - manga.author = obj["author"]?.jsonPrimitive?.contentOrNull ?: manga.author - manga.artist = obj["artist"]?.jsonPrimitive?.contentOrNull ?: manga.artist - manga.description = obj["description"]?.jsonPrimitive?.contentOrNull ?: manga.description - manga.genre = obj["genre"]?.jsonArray?.joinToString(", ") { it.jsonPrimitive.content } - ?: manga.genre - manga.status = obj["status"]?.jsonPrimitive?.intOrNull ?: manga.status - } + return if (localDetails != null) { + val obj = json.decodeFromStream(localDetails.inputStream()) - return Observable.just(manga) + manga.copy( + title = obj["title"]?.jsonPrimitive?.contentOrNull ?: manga.title, + author = obj["author"]?.jsonPrimitive?.contentOrNull ?: manga.author, + artist = obj["artist"]?.jsonPrimitive?.contentOrNull ?: manga.artist, + description = obj["description"]?.jsonPrimitive?.contentOrNull ?: manga.description, + genres = obj["genre"]?.jsonArray?.map { it.jsonPrimitive.content } ?: manga.genres, + status = obj["status"]?.jsonPrimitive?.intOrNull ?: manga.status, + ) + } else { + manga + } } - override fun fetchChapterList(manga: SManga): Observable> { + override suspend fun getChapterList(manga: MangaInfo): List { + val sManga = manga.toSManga() + val chapters = getBaseDirectories(context) .asSequence() - .mapNotNull { File(it, manga.url).listFiles()?.toList() } + .mapNotNull { File(it, manga.key).listFiles()?.toList() } .flatten() .filter { it.isDirectory || isSupportedFile(it.extension) } .map { chapterFile -> SChapter.create().apply { - url = "${manga.url}/${chapterFile.name}" + url = "${manga.key}/${chapterFile.name}" name = if (chapterFile.isDirectory) { chapterFile.name } else { @@ -206,18 +220,21 @@ class LocalSource(private val context: Context) : CatalogueSource { } name = getCleanChapterTitle(name, manga.title) - ChapterRecognition.parseChapterNumber(this, manga) + ChapterRecognition.parseChapterNumber(this, sManga) } } + .map { it.toChapterInfo() } .sortedWith { c1, c2 -> - val c = c2.chapter_number.compareTo(c1.chapter_number) + val c = c2.number.compareTo(c1.number) if (c == 0) c2.name.compareToCaseInsensitiveNaturalOrder(c1.name) else c } .toList() - return Observable.just(chapters) + return chapters } + override suspend fun getPageList(chapter: ChapterInfo) = throw Exception("Unused") + /** * Strips the manga title from a chapter name and trim whitespace/delimiter characters. */ @@ -227,10 +244,6 @@ class LocalSource(private val context: Context) : CatalogueSource { .trim(*WHITESPACE_CHARS.toCharArray(), '-', '_', ',', ':') } - override fun fetchPageList(chapter: SChapter): Observable> { - return Observable.error(Exception("Unused")) - } - private fun isSupportedFile(extension: String): Boolean { return extension.lowercase() in SUPPORTED_ARCHIVE_TYPES } diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt index 17ae6b7f42..b8543d3348 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt @@ -41,7 +41,7 @@ interface Source : tachiyomi.source.Source { * @param manga the manga to update. */ @Deprecated("Use getMangaDetails instead") - fun fetchMangaDetails(manga: SManga): Observable + fun fetchMangaDetails(manga: SManga): Observable = Observable.empty() /** * Returns an observable with all the available chapters for a manga. @@ -49,7 +49,7 @@ interface Source : tachiyomi.source.Source { * @param manga the manga to update. */ @Deprecated("Use getChapterList instead") - fun fetchChapterList(manga: SManga): Observable> + fun fetchChapterList(manga: SManga): Observable> = Observable.empty() /** * Returns an observable with the list of pages a chapter has. @@ -57,7 +57,7 @@ interface Source : tachiyomi.source.Source { * @param chapter the chapter. */ @Deprecated("Use getPageList instead") - fun fetchPageList(chapter: SChapter): Observable> + fun fetchPageList(chapter: SChapter): Observable> = Observable.empty() /** * [1.x API] Get the updated details for a manga.