refactor(reader/webtoon): Simplify page holder

This commit is contained in:
Two-Ai 2024-08-07 12:40:44 +07:00 committed by Ahmad Ansori Palembani
parent 4f9ee5dade
commit 0f609ab0c7
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -78,11 +78,6 @@ class WebtoonPageHolder(
*/ */
private var loadJob: Job? = null private var loadJob: Job? = null
/**
* Job for progress changes of the page.
*/
private var progressJob: Job? = null
init { init {
refreshLayoutParams() refreshLayoutParams()
@ -118,7 +113,7 @@ class WebtoonPageHolder(
*/ */
override fun recycle() { override fun recycle() {
loadJob?.cancel() loadJob?.cancel()
progressJob?.cancel() loadJob = null
removeErrorLayout() removeErrorLayout()
frame.recycle() frame.recycle()
@ -131,42 +126,21 @@ class WebtoonPageHolder(
val loader = page.chapter.pageLoader ?: return val loader = page.chapter.pageLoader ?: return
supervisorScope { supervisorScope {
launchIO { loader.loadPage(page) } launchIO { loader.loadPage(page) }
page.statusFlow.collectLatest { processStatus(it) } page.statusFlow.collectLatest { status ->
} when (status) {
} Page.State.QUEUE -> setQueued()
Page.State.LOAD_PAGE -> setLoading()
/** Page.State.DOWNLOAD_IMAGE -> {
* Observes the progress of the page and updates view. setDownloading()
*/ scope.launch {
private fun launchProgressJob() { page.progressFlow.collectLatest { value ->
progressJob?.cancel() progressIndicator.setProgress(value)
}
val page = page ?: return }
progressJob = scope.launch { }
page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) } Page.State.READY -> setImage()
} Page.State.ERROR -> setError()
} }
/**
* Called when the status of the page changes.
*
* @param status the new status of the page.
*/
private suspend fun processStatus(status: Page.State) {
when (status) {
Page.State.QUEUE -> setQueued()
Page.State.LOAD_PAGE -> setLoading()
Page.State.DOWNLOAD_IMAGE -> {
launchProgressJob()
setDownloading()
}
Page.State.READY -> {
setImage()
progressJob?.cancel()
}
Page.State.ERROR -> {
setError()
progressJob?.cancel()
} }
} }
} }