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