Cleanup Page status (#8696)

* Cleanup Page statusSubject and statusCallback

* Convert Page status from Int to enum
This commit is contained in:
Two-Ai 2022-12-07 18:28:38 -05:00 committed by GitHub
parent f05e251991
commit 6ca32710be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 74 additions and 90 deletions

View File

@ -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 }
}
}
}

View File

@ -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<UniFile> {
page.status = Page.DOWNLOAD_IMAGE
page.status = Page.State.DOWNLOAD_IMAGE
page.progress = 0
return source.fetchImage(page)
.map { response ->

View File

@ -100,11 +100,11 @@ class DownloadQueue(
.startWith(getActiveDownloads())
.flatMap { download ->
if (download.status == Download.State.DOWNLOADING) {
val pageStatusSubject = PublishSubject.create<Int>()
val pageStatusSubject = PublishSubject.create<Page.State>()
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<Page>?, subject: PublishSubject<Int>?) {
pages?.forEach { it.setStatusSubject(subject) }
private fun setPagesSubject(pages: List<Page>?, subject: PublishSubject<Page.State>?) {
pages?.forEach { it.statusSubject = subject }
}
}

View File

@ -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)

View File

@ -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<Application>()
@ -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<Application>()
@ -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

View File

@ -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<Int> {
return Observable.just(Page.READY)
override fun getPage(page: ReaderPage): Observable<Page.State> {
return Observable.just(Page.State.READY)
}
}

View File

@ -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<Int> {
return Observable.just(Page.READY)
override fun getPage(page: ReaderPage): Observable<Page.State> {
return Observable.just(Page.State.READY)
}
}

View File

@ -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<Int> {
override fun getPage(page: ReaderPage): Observable<Page.State> {
return Observable.just(
if (isRecycled) {
Page.ERROR
Page.State.ERROR
} else {
Page.READY
Page.State.READY
},
)
}

View File

@ -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<Int> {
override fun getPage(page: ReaderPage): Observable<Page.State> {
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<Int>())
page.setStatusSubject(statusSubject)
val statusSubject = SerializedSubject(PublishSubject.create<Page.State>())
page.statusSubject = statusSubject
val queuedPages = mutableListOf<PriorityPage>()
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<ReaderPage> {
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<ReaderPage> {
page.status = Page.DOWNLOAD_IMAGE
page.status = Page.State.DOWNLOAD_IMAGE
return fetchImage(page)
.doOnNext { chapterCache.putImageToCache(page.imageUrl!!, it) }
.map { page }

View File

@ -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<List<ReaderPage>>
/**
* 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<Int>
abstract fun getPage(page: ReaderPage): Observable<Page.State>
/**
* Retries the given [page] in case it failed to load. This method only makes sense when an

View File

@ -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<Int> {
override fun getPage(page: ReaderPage): Observable<Page.State> {
return Observable.just(
if (isRecycled) {
Page.ERROR
Page.State.ERROR
} else {
Page.READY
Page.State.READY
},
)
}

View File

@ -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<Int> {
override fun getPage(page: ReaderPage): Observable<Page.State> {
return Observable.just(
if (isRecycled) {
Page.ERROR
Page.State.ERROR
} else {
Page.READY
Page.State.READY
},
)
}

View File

@ -10,7 +10,7 @@ class StencilPage(
override var chapter: ReaderChapter = parent.chapter
init {
status = READY
status = State.READY
stream = stencilStream
}
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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<Int, Int>? = null
@Transient
private var statusCallback: ((Page) -> Unit)? = null
var statusSubject: Subject<State, State>? = 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<Int, Int>?) {
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,
}
}

View File

@ -4,9 +4,9 @@ import eu.kanade.tachiyomi.source.model.Page
import rx.Observable
fun HttpSource.getImageUrl(page: Page): Observable<Page> {
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 }