From 58ebf146919694170af0d0044a9dde56668fa543 Mon Sep 17 00:00:00 2001 From: Two-Ai <81279822+Two-Ai@users.noreply.github.com> Date: Sat, 14 Jan 2023 19:45:15 -0500 Subject: [PATCH] Convert PageLoader.getPages to suspending function (#8917) --- .../data/download/DownloadManager.kt | 25 ++++++++----------- .../ui/reader/loader/ChapterLoader.kt | 3 +-- .../ui/reader/loader/DirectoryPageLoader.kt | 8 +++--- .../ui/reader/loader/DownloadPageLoader.kt | 24 ++++++++---------- .../ui/reader/loader/EpubPageLoader.kt | 6 ++--- .../ui/reader/loader/HttpPageLoader.kt | 25 +++++++++++-------- .../tachiyomi/ui/reader/loader/PageLoader.kt | 5 ++-- .../ui/reader/loader/RarPageLoader.kt | 6 ++--- .../ui/reader/loader/ZipPageLoader.kt | 7 +++--- 9 files changed, 51 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index fe650d4b4..bb9cfcb2a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -15,7 +15,6 @@ import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.system.logcat import kotlinx.coroutines.runBlocking import logcat.LogPriority -import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -165,23 +164,21 @@ class DownloadManager( * @param source the source of the chapter. * @param manga the manga of the chapter. * @param chapter the downloaded chapter. - * @return an observable containing the list of pages from the chapter. + * @return the list of pages from the chapter. */ - fun buildPageList(source: Source, manga: Manga, chapter: Chapter): Observable> { + fun buildPageList(source: Source, manga: Manga, chapter: Chapter): List { val chapterDir = provider.findChapterDir(chapter.name, chapter.scanlator, manga.title, source) - return Observable.fromCallable { - val files = chapterDir?.listFiles().orEmpty() - .filter { "image" in it.type.orEmpty() } + val files = chapterDir?.listFiles().orEmpty() + .filter { "image" in it.type.orEmpty() } - if (files.isEmpty()) { - throw Exception(context.getString(R.string.page_list_empty_error)) - } - - files.sortedBy { it.name } - .mapIndexed { i, file -> - Page(i, uri = file.uri).apply { status = Page.State.READY } - } + if (files.isEmpty()) { + throw Exception(context.getString(R.string.page_list_empty_error)) } + + return files.sortedBy { it.name } + .mapIndexed { i, file -> + Page(i, uri = file.uri).apply { status = Page.State.READY } + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt index f17a26978..9617032d9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt @@ -11,7 +11,6 @@ import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter -import eu.kanade.tachiyomi.util.lang.awaitSingle import eu.kanade.tachiyomi.util.lang.withIOContext import eu.kanade.tachiyomi.util.system.logcat @@ -42,7 +41,7 @@ class ChapterLoader( val loader = getPageLoader(chapter) chapter.pageLoader = loader - val pages = loader.getPages().awaitSingle() + val pages = loader.getPages() .onEach { it.chapter = chapter } if (pages.isEmpty()) { 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 3de2de03b..8d4e19e7c 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 @@ -14,10 +14,9 @@ import java.io.FileInputStream class DirectoryPageLoader(val file: File) : PageLoader() { /** - * Returns an observable containing the pages found on this directory ordered with a natural - * comparator. + * Returns the pages found on this directory ordered with a natural comparator. */ - override fun getPages(): Observable> { + override suspend fun getPages(): List { return file.listFiles() ?.filter { !it.isDirectory && ImageUtil.isImage(it.name) { FileInputStream(it) } } ?.sortedWith { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) } @@ -27,8 +26,7 @@ class DirectoryPageLoader(val file: File) : PageLoader() { stream = streamFn status = Page.State.READY } - } - .let { Observable.just(it) } + } ?: emptyList() } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt index 8326c1422..ba03a3a5a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt @@ -37,9 +37,9 @@ class DownloadPageLoader( } /** - * Returns an observable containing the pages found on this downloaded chapter. + * Returns the pages found on this downloaded chapter. */ - override fun getPages(): Observable> { + override suspend fun getPages(): List { val dbChapter = chapter.chapter val chapterPath = downloadProvider.findChapterDir(dbChapter.name, dbChapter.scanlator, manga.title, source) return if (chapterPath?.isFile == true) { @@ -49,22 +49,20 @@ class DownloadPageLoader( } } - private fun getPagesFromArchive(chapterPath: UniFile): Observable> { + private suspend fun getPagesFromArchive(chapterPath: UniFile): List { val loader = ZipPageLoader(File(chapterPath.filePath!!)).also { zipPageLoader = it } return loader.getPages() } - private fun getPagesFromDirectory(): Observable> { - return downloadManager.buildPageList(source, manga, chapter.chapter.toDomainChapter()!!) - .map { pages -> - pages.map { page -> - ReaderPage(page.index, page.url, page.imageUrl) { - context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!! - }.apply { - status = Page.State.READY - } - } + private fun getPagesFromDirectory(): List { + val pages = downloadManager.buildPageList(source, manga, chapter.chapter.toDomainChapter()!!) + return pages.map { page -> + ReaderPage(page.index, page.url, page.imageUrl) { + context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!! + }.apply { + status = Page.State.READY } + } } override fun getPage(page: ReaderPage): Observable { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt index 3aafabe65..93f931c50 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt @@ -25,10 +25,9 @@ class EpubPageLoader(file: File) : PageLoader() { } /** - * Returns an observable containing the pages found on this zip archive ordered with a natural - * comparator. + * Returns the pages found on this zip archive ordered with a natural comparator. */ - override fun getPages(): Observable> { + override suspend fun getPages(): List { return epub.getImagesFromPages() .mapIndexed { i, path -> val streamFn = { epub.getInputStream(epub.getEntry(path)!!) } @@ -37,7 +36,6 @@ class EpubPageLoader(file: File) : PageLoader() { status = Page.State.READY } } - .let { Observable.just(it) } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index b0442ad80..2dfb45b16 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.system.logcat +import kotlinx.coroutines.CancellationException import logcat.LogPriority import rx.Completable import rx.Observable @@ -83,18 +84,22 @@ class HttpPageLoader( } /** - * Returns an observable with the page list for a chapter. It tries to return the page list from - * the local cache, otherwise fallbacks to network. + * Returns the page list for a chapter. It tries to return the page list from the local cache, + * otherwise fallbacks to network. */ - override fun getPages(): Observable> { - return Observable.fromCallable { chapterCache.getPageListFromCache(chapter.chapter.toDomainChapter()!!) } - .onErrorResumeNext { source.fetchPageList(chapter.chapter) } - .map { pages -> - pages.mapIndexed { index, page -> - // Don't trust sources and use our own indexing - ReaderPage(index, page.url, page.imageUrl) - } + override suspend fun getPages(): List { + val pages = try { + chapterCache.getPageListFromCache(chapter.chapter.toDomainChapter()!!) + } catch (e: Throwable) { + if (e is CancellationException) { + throw e } + source.getPageList(chapter.chapter) + } + return pages.mapIndexed { index, page -> + // Don't trust sources and use our own indexing + ReaderPage(index, page.url, page.imageUrl) + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt index ea224bd9b..c9417ae70 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt @@ -27,10 +27,9 @@ abstract class PageLoader { } /** - * Returns an observable containing the list of pages of a chapter. Only the first emission - * will be used. + * Returns the list of pages of a chapter. */ - abstract fun getPages(): Observable> + abstract suspend fun getPages(): List /** * Returns an observable that should inform of the progress of the page 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 22269fd57..bf3fd0178 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 @@ -38,10 +38,10 @@ class RarPageLoader(file: File) : PageLoader() { } /** - * Returns an observable containing the pages found on this rar archive ordered with a natural + * Returns an RxJava Single containing the pages found on this rar archive ordered with a natural * comparator. */ - override fun getPages(): Observable> { + override suspend fun getPages(): List { return archive.fileHeaders.asSequence() .filter { !it.isDirectory && ImageUtil.isImage(it.fileName) { archive.getInputStream(it) } } .sortedWith { f1, f2 -> f1.fileName.compareToCaseInsensitiveNaturalOrder(f2.fileName) } @@ -51,7 +51,7 @@ class RarPageLoader(file: File) : PageLoader() { status = Page.State.READY } } - .let { Observable.just(it.toList()) } + .toList() } /** 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 bb8fcc408..62b11bfc8 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 @@ -33,10 +33,9 @@ class ZipPageLoader(file: File) : PageLoader() { } /** - * Returns an observable containing the pages found on this zip archive ordered with a natural - * comparator. + * Returns the pages found on this zip archive ordered with a natural comparator. */ - override fun getPages(): Observable> { + override suspend fun getPages(): List { return zip.entries().asSequence() .filter { !it.isDirectory && ImageUtil.isImage(it.name) { zip.getInputStream(it) } } .sortedWith { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) } @@ -46,7 +45,7 @@ class ZipPageLoader(file: File) : PageLoader() { status = Page.State.READY } } - .let { Observable.just(it.toList()) } + .toList() } /**