PagerPageHolder: lazy init loading indicator

Co-authored-by: ivan <12537387+ivaniskandar@users.noreply.github.com>
This commit is contained in:
AntsyLich 2024-09-02 21:13:52 +06:00
parent 8f9a325895
commit a45eb5e528
No known key found for this signature in database

View File

@ -45,7 +45,7 @@ class PagerPageHolder(
/** /**
* Loading progress bar to indicate the current progress. * Loading progress bar to indicate the current progress.
*/ */
private val progressIndicator: ReaderProgressIndicator = ReaderProgressIndicator(readerThemedContext) private var progressIndicator: ReaderProgressIndicator? = null // = ReaderProgressIndicator(readerThemedContext)
/** /**
* Error layout to show when the image fails to load. * Error layout to show when the image fails to load.
@ -60,7 +60,6 @@ class PagerPageHolder(
private var loadJob: Job? = null private var loadJob: Job? = null
init { init {
addView(progressIndicator)
loadJob = scope.launch { loadPageAndProcessStatus() } loadJob = scope.launch { loadPageAndProcessStatus() }
} }
@ -74,6 +73,13 @@ class PagerPageHolder(
loadJob = null loadJob = null
} }
private fun initProgressIndicator() {
if (progressIndicator == null) {
progressIndicator = ReaderProgressIndicator(context)
addView(progressIndicator)
}
}
/** /**
* Loads the page and processes changes to the page's status. * Loads the page and processes changes to the page's status.
* *
@ -95,7 +101,7 @@ class PagerPageHolder(
Page.State.DOWNLOAD_IMAGE -> { Page.State.DOWNLOAD_IMAGE -> {
setDownloading() setDownloading()
page.progressFlow.collectLatest { value -> page.progressFlow.collectLatest { value ->
progressIndicator.setProgress(value) progressIndicator?.setProgress(value)
} }
} }
Page.State.READY -> setImage() Page.State.READY -> setImage()
@ -109,7 +115,8 @@ class PagerPageHolder(
* Called when the page is queued. * Called when the page is queued.
*/ */
private fun setQueued() { private fun setQueued() {
progressIndicator.show() initProgressIndicator()
progressIndicator?.show()
removeErrorLayout() removeErrorLayout()
} }
@ -117,7 +124,8 @@ class PagerPageHolder(
* Called when the page is loading. * Called when the page is loading.
*/ */
private fun setLoading() { private fun setLoading() {
progressIndicator.show() initProgressIndicator()
progressIndicator?.show()
removeErrorLayout() removeErrorLayout()
} }
@ -125,7 +133,8 @@ class PagerPageHolder(
* Called when the page is downloading. * Called when the page is downloading.
*/ */
private fun setDownloading() { private fun setDownloading() {
progressIndicator.show() initProgressIndicator()
progressIndicator?.show()
removeErrorLayout() removeErrorLayout()
} }
@ -133,7 +142,7 @@ class PagerPageHolder(
* Called when the page is ready. * Called when the page is ready.
*/ */
private suspend fun setImage() { private suspend fun setImage() {
progressIndicator.setProgress(0) progressIndicator?.setProgress(0)
val streamFn = page.stream ?: return val streamFn = page.stream ?: return
@ -234,13 +243,13 @@ class PagerPageHolder(
* Called when the page has an error. * Called when the page has an error.
*/ */
private fun setError() { private fun setError() {
progressIndicator.hide() progressIndicator?.hide()
showErrorLayout() showErrorLayout()
} }
override fun onImageLoaded() { override fun onImageLoaded() {
super.onImageLoaded() super.onImageLoaded()
progressIndicator.hide() progressIndicator?.hide()
} }
/** /**