From 6ca32710be5bfed851ab695a0d9438c7ab609b24 Mon Sep 17 00:00:00 2001 From: Two-Ai <81279822+Two-Ai@users.noreply.github.com> Date: Wed, 7 Dec 2022 18:28:38 -0500 Subject: [PATCH] Cleanup Page status (#8696) * Cleanup Page statusSubject and statusCallback * Convert Page status from Int to enum --- .../data/download/DownloadManager.kt | 2 +- .../tachiyomi/data/download/Downloader.kt | 6 ++-- .../data/download/model/DownloadQueue.kt | 8 ++--- .../tachiyomi/ui/reader/ReaderPageSheet.kt | 2 +- .../tachiyomi/ui/reader/ReaderPresenter.kt | 6 ++-- .../ui/reader/loader/DirectoryPageLoader.kt | 6 ++-- .../ui/reader/loader/DownloadPageLoader.kt | 6 ++-- .../ui/reader/loader/EpubPageLoader.kt | 8 ++--- .../ui/reader/loader/HttpPageLoader.kt | 36 +++++++++---------- .../tachiyomi/ui/reader/loader/PageLoader.kt | 6 ++-- .../ui/reader/loader/RarPageLoader.kt | 8 ++--- .../ui/reader/loader/ZipPageLoader.kt | 8 ++--- .../tachiyomi/ui/reader/model/StencilPage.kt | 2 +- .../ui/reader/viewer/pager/PagerPageHolder.kt | 12 +++---- .../viewer/webtoon/WebtoonPageHolder.kt | 12 +++---- .../eu/kanade/tachiyomi/source/model/Page.kt | 32 +++++------------ .../source/online/HttpSourceFetcher.kt | 4 +-- 17 files changed, 74 insertions(+), 90 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 10b0ea74d7..fe650d4b4a 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 @@ -179,7 +179,7 @@ class DownloadManager( files.sortedBy { it.name } .mapIndexed { i, file -> - Page(i, uri = file.uri).apply { status = Page.READY } + Page(i, uri = file.uri).apply { status = Page.State.READY } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 8ace3d89e1..0c0c2265d3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -402,13 +402,13 @@ class Downloader( page.uri = file.uri page.progress = 100 download.downloadedImages++ - page.status = Page.READY + page.status = Page.State.READY } .map { page } // Mark this page as error and allow to download the remaining .onErrorReturn { page.progress = 0 - page.status = Page.ERROR + page.status = Page.State.ERROR notifier.onError(it.message, download.chapter.name, download.manga.title) page } @@ -423,7 +423,7 @@ class Downloader( * @param filename the filename of the image. */ private fun downloadImage(page: Page, source: HttpSource, tmpDir: UniFile, filename: String): Observable { - page.status = Page.DOWNLOAD_IMAGE + page.status = Page.State.DOWNLOAD_IMAGE page.progress = 0 return source.fetchImage(page) .map { response -> diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt index 07c81075fa..6a9245460c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt @@ -100,11 +100,11 @@ class DownloadQueue( .startWith(getActiveDownloads()) .flatMap { download -> if (download.status == Download.State.DOWNLOADING) { - val pageStatusSubject = PublishSubject.create() + val pageStatusSubject = PublishSubject.create() setPagesSubject(download.pages, pageStatusSubject) return@flatMap pageStatusSubject .onBackpressureBuffer() - .filter { it == Page.READY } + .filter { it == Page.State.READY } .map { download } } else if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) { setPagesSubject(download.pages, null) @@ -120,7 +120,7 @@ class DownloadQueue( } } - private fun setPagesSubject(pages: List?, subject: PublishSubject?) { - pages?.forEach { it.setStatusSubject(subject) } + private fun setPagesSubject(pages: List?, subject: PublishSubject?) { + pages?.forEach { it.statusSubject = subject } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt index 5e8aee63b1..9dff32a8e9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt @@ -33,7 +33,7 @@ class ReaderPageSheet( * Sets the image of this page as the cover of the manga. */ private fun setAsCover() { - if (page.status != Page.READY) return + if (page.status != Page.State.READY) return MaterialAlertDialogBuilder(activity) .setMessage(R.string.confirm_set_image_as_cover) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index c8b07a2271..296031dae7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -720,7 +720,7 @@ class ReaderPresenter( * There's also a notification to allow sharing the image somewhere else or deleting it. */ fun saveImage(page: ReaderPage) { - if (page.status != Page.READY) return + if (page.status != Page.State.READY) return val manga = manga ?: return val context = Injekt.get() @@ -761,7 +761,7 @@ class ReaderPresenter( * image will be kept so it won't be taking lots of internal disk space. */ fun shareImage(page: ReaderPage) { - if (page.status != Page.READY) return + if (page.status != Page.State.READY) return val manga = manga ?: return val context = Injekt.get() @@ -792,7 +792,7 @@ class ReaderPresenter( * Sets the image of this [page] as cover and notifies the UI of the result. */ fun setAsCover(context: Context, page: ReaderPage) { - if (page.status != Page.READY) return + if (page.status != Page.State.READY) return val manga = manga?.toDomainManga() ?: return val stream = page.stream ?: return 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 aba79147f7..3de2de03bf 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 @@ -25,7 +25,7 @@ class DirectoryPageLoader(val file: File) : PageLoader() { val streamFn = { FileInputStream(file) } ReaderPage(i).apply { stream = streamFn - status = Page.READY + status = Page.State.READY } } .let { Observable.just(it) } @@ -34,7 +34,7 @@ class DirectoryPageLoader(val file: File) : PageLoader() { /** * Returns an observable that emits a ready state. */ - override fun getPage(page: ReaderPage): Observable { - return Observable.just(Page.READY) + override fun getPage(page: ReaderPage): Observable { + return Observable.just(Page.State.READY) } } 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 d9779cc500..1e02e0f305 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 @@ -54,13 +54,13 @@ class DownloadPageLoader( ReaderPage(page.index, page.url, page.imageUrl) { context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!! }.apply { - status = Page.READY + status = Page.State.READY } } } } - override fun getPage(page: ReaderPage): Observable { - return Observable.just(Page.READY) + override fun getPage(page: ReaderPage): Observable { + return Observable.just(Page.State.READY) } } 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 611d21882c..3aafabe655 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 @@ -34,7 +34,7 @@ class EpubPageLoader(file: File) : PageLoader() { val streamFn = { epub.getInputStream(epub.getEntry(path)!!) } ReaderPage(i).apply { stream = streamFn - status = Page.READY + status = Page.State.READY } } .let { Observable.just(it) } @@ -43,12 +43,12 @@ class EpubPageLoader(file: File) : PageLoader() { /** * Returns an observable that emits a ready state unless the loader was recycled. */ - override fun getPage(page: ReaderPage): Observable { + override fun getPage(page: ReaderPage): Observable { return Observable.just( if (isRecycled) { - Page.ERROR + Page.State.ERROR } else { - Page.READY + Page.State.READY }, ) } 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 0046ec9588..b0442ad80d 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 @@ -44,7 +44,7 @@ class HttpPageLoader( init { subscriptions += Observable.defer { Observable.just(queue.take().page) } - .filter { it.status == Page.QUEUE } + .filter { it.status == Page.State.QUEUE } .concatMap { source.fetchImageFromCacheThenNet(it) } .repeat() .subscribeOn(Schedulers.io()) @@ -101,25 +101,25 @@ class HttpPageLoader( * Returns an observable that loads a page through the queue and listens to its result to * emit new states. It handles re-enqueueing pages if they were evicted from the cache. */ - override fun getPage(page: ReaderPage): Observable { + override fun getPage(page: ReaderPage): Observable { return Observable.defer { val imageUrl = page.imageUrl // Check if the image has been deleted - if (page.status == Page.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) { - page.status = Page.QUEUE + if (page.status == Page.State.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) { + page.status = Page.State.QUEUE } // Automatically retry failed pages when subscribed to this page - if (page.status == Page.ERROR) { - page.status = Page.QUEUE + if (page.status == Page.State.ERROR) { + page.status = Page.State.QUEUE } - val statusSubject = SerializedSubject(PublishSubject.create()) - page.setStatusSubject(statusSubject) + val statusSubject = SerializedSubject(PublishSubject.create()) + page.statusSubject = statusSubject val queuedPages = mutableListOf() - if (page.status == Page.QUEUE) { + if (page.status == Page.State.QUEUE) { queuedPages += PriorityPage(page, 1).also { queue.offer(it) } } queuedPages += preloadNextPages(page, preloadSize) @@ -127,7 +127,7 @@ class HttpPageLoader( statusSubject.startWith(page.status) .doOnUnsubscribe { queuedPages.forEach { - if (it.page.status == Page.QUEUE) { + if (it.page.status == Page.State.QUEUE) { queue.remove(it) } } @@ -149,7 +149,7 @@ class HttpPageLoader( return pages .subList(pageIndex + 1, min(pageIndex + 1 + amount, pages.size)) .mapNotNull { - if (it.status == Page.QUEUE) { + if (it.status == Page.State.QUEUE) { PriorityPage(it, 0).apply { queue.offer(this) } } else { null @@ -161,8 +161,8 @@ class HttpPageLoader( * Retries a page. This method is only called from user interaction on the viewer. */ override fun retryPage(page: ReaderPage) { - if (page.status == Page.ERROR) { - page.status = Page.QUEUE + if (page.status == Page.State.ERROR) { + page.status = Page.State.QUEUE } queue.offer(PriorityPage(page, 2)) } @@ -200,9 +200,9 @@ class HttpPageLoader( } private fun HttpSource.getImageUrl(page: ReaderPage): Observable { - page.status = Page.LOAD_PAGE + page.status = Page.State.LOAD_PAGE return fetchImageUrl(page) - .doOnError { page.status = Page.ERROR } + .doOnError { page.status = Page.State.ERROR } .onErrorReturn { null } .doOnNext { page.imageUrl = it } .map { page } @@ -227,9 +227,9 @@ class HttpPageLoader( } .doOnNext { page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } - page.status = Page.READY + page.status = Page.State.READY } - .doOnError { page.status = Page.ERROR } + .doOnError { page.status = Page.State.ERROR } .onErrorReturn { page } } @@ -239,7 +239,7 @@ class HttpPageLoader( * @param page the page. */ private fun HttpSource.cacheImage(page: ReaderPage): Observable { - page.status = Page.DOWNLOAD_IMAGE + page.status = Page.State.DOWNLOAD_IMAGE return fetchImage(page) .doOnNext { chapterCache.putImageToCache(page.imageUrl!!, it) } .map { page } 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 de7e4e5410..ea224bd9bb 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 @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.reader.loader import androidx.annotation.CallSuper +import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import rx.Observable @@ -32,10 +33,9 @@ abstract class PageLoader { abstract fun getPages(): Observable> /** - * Returns an observable that should inform of the progress of the page (see the Page class - * for the available states) + * Returns an observable that should inform of the progress of the page */ - abstract fun getPage(page: ReaderPage): Observable + abstract fun getPage(page: ReaderPage): Observable /** * Retries the given [page] in case it failed to load. This method only makes sense when an 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 5d94893179..22269fd57d 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 @@ -48,7 +48,7 @@ class RarPageLoader(file: File) : PageLoader() { .mapIndexed { i, header -> ReaderPage(i).apply { stream = { getStream(header) } - status = Page.READY + status = Page.State.READY } } .let { Observable.just(it.toList()) } @@ -57,12 +57,12 @@ class RarPageLoader(file: File) : PageLoader() { /** * Returns an observable that emits a ready state unless the loader was recycled. */ - override fun getPage(page: ReaderPage): Observable { + override fun getPage(page: ReaderPage): Observable { return Observable.just( if (isRecycled) { - Page.ERROR + Page.State.ERROR } else { - Page.READY + Page.State.READY }, ) } 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 3db97c8fa9..bb8fcc4088 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 @@ -43,7 +43,7 @@ class ZipPageLoader(file: File) : PageLoader() { .mapIndexed { i, entry -> ReaderPage(i).apply { stream = { zip.getInputStream(entry) } - status = Page.READY + status = Page.State.READY } } .let { Observable.just(it.toList()) } @@ -52,12 +52,12 @@ class ZipPageLoader(file: File) : PageLoader() { /** * Returns an observable that emits a ready state unless the loader was recycled. */ - override fun getPage(page: ReaderPage): Observable { + override fun getPage(page: ReaderPage): Observable { return Observable.just( if (isRecycled) { - Page.ERROR + Page.State.ERROR } else { - Page.READY + Page.State.READY }, ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt index 9bd24a344f..c5a7ee79f3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt @@ -10,7 +10,7 @@ class StencilPage( override var chapter: ReaderChapter = parent.chapter init { - status = READY + status = State.READY stream = stencilStream } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt index 694c47da6b..04a45defde 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt @@ -119,19 +119,19 @@ class PagerPageHolder( * * @param status the new status of the page. */ - private fun processStatus(status: Int) { + private fun processStatus(status: Page.State) { when (status) { - Page.QUEUE -> setQueued() - Page.LOAD_PAGE -> setLoading() - Page.DOWNLOAD_IMAGE -> { + Page.State.QUEUE -> setQueued() + Page.State.LOAD_PAGE -> setLoading() + Page.State.DOWNLOAD_IMAGE -> { observeProgress() setDownloading() } - Page.READY -> { + Page.State.READY -> { setImage() unsubscribeProgress() } - Page.ERROR -> { + Page.State.ERROR -> { setError() unsubscribeProgress() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt index 6221c65b36..a2d0c5baee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt @@ -165,19 +165,19 @@ class WebtoonPageHolder( * * @param status the new status of the page. */ - private fun processStatus(status: Int) { + private fun processStatus(status: Page.State) { when (status) { - Page.QUEUE -> setQueued() - Page.LOAD_PAGE -> setLoading() - Page.DOWNLOAD_IMAGE -> { + Page.State.QUEUE -> setQueued() + Page.State.LOAD_PAGE -> setLoading() + Page.State.DOWNLOAD_IMAGE -> { observeProgress() setDownloading() } - Page.READY -> { + Page.State.READY -> { setImage() unsubscribeProgress() } - Page.ERROR -> { + Page.State.ERROR -> { setError() unsubscribeProgress() } diff --git a/source-api/src/main/java/eu/kanade/tachiyomi/source/model/Page.kt b/source-api/src/main/java/eu/kanade/tachiyomi/source/model/Page.kt index be5ab89d71..0651ce89d3 100644 --- a/source-api/src/main/java/eu/kanade/tachiyomi/source/model/Page.kt +++ b/source-api/src/main/java/eu/kanade/tachiyomi/source/model/Page.kt @@ -19,26 +19,18 @@ open class Page( @Transient @Volatile - var status: Int = 0 + var status: State = State.QUEUE set(value) { field = value statusSubject?.onNext(value) - statusCallback?.invoke(this) } @Transient @Volatile var progress: Int = 0 - set(value) { - field = value - statusCallback?.invoke(this) - } @Transient - private var statusSubject: Subject? = null - - @Transient - private var statusCallback: ((Page) -> Unit)? = null + var statusSubject: Subject? = null override fun update(bytesRead: Long, contentLength: Long, done: Boolean) { progress = if (contentLength > 0) { @@ -48,19 +40,11 @@ open class Page( } } - fun setStatusSubject(subject: Subject?) { - this.statusSubject = subject - } - - fun setStatusCallback(f: ((Page) -> Unit)?) { - statusCallback = f - } - - companion object { - const val QUEUE = 0 - const val LOAD_PAGE = 1 - const val DOWNLOAD_IMAGE = 2 - const val READY = 3 - const val ERROR = 4 + enum class State { + QUEUE, + LOAD_PAGE, + DOWNLOAD_IMAGE, + READY, + ERROR, } } diff --git a/source-api/src/main/java/eu/kanade/tachiyomi/source/online/HttpSourceFetcher.kt b/source-api/src/main/java/eu/kanade/tachiyomi/source/online/HttpSourceFetcher.kt index 7b3ea4bdec..6b5e4a6381 100644 --- a/source-api/src/main/java/eu/kanade/tachiyomi/source/online/HttpSourceFetcher.kt +++ b/source-api/src/main/java/eu/kanade/tachiyomi/source/online/HttpSourceFetcher.kt @@ -4,9 +4,9 @@ import eu.kanade.tachiyomi.source.model.Page import rx.Observable fun HttpSource.getImageUrl(page: Page): Observable { - page.status = Page.LOAD_PAGE + page.status = Page.State.LOAD_PAGE return fetchImageUrl(page) - .doOnError { page.status = Page.ERROR } + .doOnError { page.status = Page.State.ERROR } .onErrorReturn { null } .doOnNext { page.imageUrl = it } .map { page }