mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(reader/webtoon): Simplify page holder
This commit is contained in:
parent
4f9ee5dade
commit
0f609ab0c7
1 changed files with 16 additions and 42 deletions
|
@ -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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue