mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(PagerPageHolder): Simplify setImage()
This commit is contained in:
parent
a41cecb41e
commit
19ab667eac
1 changed files with 57 additions and 67 deletions
|
@ -30,9 +30,9 @@ import eu.kanade.tachiyomi.util.system.ImageUtil.isPagePadded
|
||||||
import eu.kanade.tachiyomi.util.system.ThemeUtil
|
import eu.kanade.tachiyomi.util.system.ThemeUtil
|
||||||
import eu.kanade.tachiyomi.util.system.bottomCutoutInset
|
import eu.kanade.tachiyomi.util.system.bottomCutoutInset
|
||||||
import eu.kanade.tachiyomi.util.system.isInNightMode
|
import eu.kanade.tachiyomi.util.system.isInNightMode
|
||||||
import eu.kanade.tachiyomi.util.system.launchIO
|
|
||||||
import eu.kanade.tachiyomi.util.system.launchUI
|
import eu.kanade.tachiyomi.util.system.launchUI
|
||||||
import eu.kanade.tachiyomi.util.system.topCutoutInset
|
import eu.kanade.tachiyomi.util.system.topCutoutInset
|
||||||
|
import eu.kanade.tachiyomi.util.system.withIOContext
|
||||||
import eu.kanade.tachiyomi.util.system.withUIContext
|
import eu.kanade.tachiyomi.util.system.withUIContext
|
||||||
import eu.kanade.tachiyomi.util.view.backgroundColor
|
import eu.kanade.tachiyomi.util.view.backgroundColor
|
||||||
import eu.kanade.tachiyomi.util.view.isVisibleOnScreen
|
import eu.kanade.tachiyomi.util.view.isVisibleOnScreen
|
||||||
|
@ -107,12 +107,6 @@ class PagerPageHolder(
|
||||||
*/
|
*/
|
||||||
private var extraProgressJob: Job? = null
|
private var extraProgressJob: Job? = null
|
||||||
|
|
||||||
/**
|
|
||||||
* Job used to read the header of the image. This is needed in order to instantiate
|
|
||||||
* the appropriate image view depending if the image is animated (GIF).
|
|
||||||
*/
|
|
||||||
private var readImageHeaderJob: Job? = null
|
|
||||||
|
|
||||||
private var status = Page.State.READY
|
private var status = Page.State.READY
|
||||||
private var extraStatus = Page.State.READY
|
private var extraStatus = Page.State.READY
|
||||||
private var progress: Int = 0
|
private var progress: Int = 0
|
||||||
|
@ -180,7 +174,6 @@ class PagerPageHolder(
|
||||||
cancelLoadJob(1)
|
cancelLoadJob(1)
|
||||||
cancelProgressJob(2)
|
cancelProgressJob(2)
|
||||||
cancelLoadJob(2)
|
cancelLoadJob(2)
|
||||||
cancelReadImageHeader()
|
|
||||||
(pageView as? SubsamplingScaleImageView)?.setOnImageEventListener(null)
|
(pageView as? SubsamplingScaleImageView)?.setOnImageEventListener(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -349,7 +342,7 @@ class PagerPageHolder(
|
||||||
*
|
*
|
||||||
* @param status the new status of the page.
|
* @param status the new status of the page.
|
||||||
*/
|
*/
|
||||||
private fun processStatus(status: Page.State) {
|
private suspend fun processStatus(status: Page.State) {
|
||||||
when (status) {
|
when (status) {
|
||||||
Page.State.QUEUE -> setQueued()
|
Page.State.QUEUE -> setQueued()
|
||||||
Page.State.LOAD_PAGE -> setLoading()
|
Page.State.LOAD_PAGE -> setLoading()
|
||||||
|
@ -375,7 +368,7 @@ class PagerPageHolder(
|
||||||
*
|
*
|
||||||
* @param status the new status of the page.
|
* @param status the new status of the page.
|
||||||
*/
|
*/
|
||||||
private fun processStatus2(status: Page.State) {
|
private suspend fun processStatus2(status: Page.State) {
|
||||||
when (status) {
|
when (status) {
|
||||||
Page.State.QUEUE -> setQueued()
|
Page.State.QUEUE -> setQueued()
|
||||||
Page.State.LOAD_PAGE -> setLoading()
|
Page.State.LOAD_PAGE -> setLoading()
|
||||||
|
@ -422,14 +415,6 @@ class PagerPageHolder(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Unsubscribes from the read image header subscription.
|
|
||||||
*/
|
|
||||||
private fun cancelReadImageHeader() {
|
|
||||||
readImageHeaderJob?.cancel()
|
|
||||||
readImageHeaderJob = null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the page is queued.
|
* Called when the page is queued.
|
||||||
*/
|
*/
|
||||||
|
@ -457,7 +442,7 @@ class PagerPageHolder(
|
||||||
/**
|
/**
|
||||||
* Called when the page is ready.
|
* Called when the page is ready.
|
||||||
*/
|
*/
|
||||||
private fun setImage() {
|
private suspend fun setImage() {
|
||||||
progressIndicator.show()
|
progressIndicator.show()
|
||||||
if (extraPage == null) {
|
if (extraPage == null) {
|
||||||
progressIndicator.completeAndFadeOut()
|
progressIndicator.completeAndFadeOut()
|
||||||
|
@ -466,60 +451,65 @@ class PagerPageHolder(
|
||||||
}
|
}
|
||||||
errorLayout?.isVisible = false
|
errorLayout?.isVisible = false
|
||||||
|
|
||||||
cancelReadImageHeader()
|
val streamFn = page.stream ?: return
|
||||||
|
|
||||||
readImageHeaderJob = scope.launchIO {
|
|
||||||
val streamFn = page.stream ?: return@launchIO
|
|
||||||
val streamFn2 = extraPage?.stream
|
val streamFn2 = extraPage?.stream
|
||||||
|
|
||||||
var actualSource: BufferedSource? = null
|
|
||||||
try {
|
try {
|
||||||
val source1 = streamFn().buffered(16).use { Buffer().readFrom(it) }
|
val (source, isAnimated) = withIOContext {
|
||||||
val source2 = streamFn2?.invoke()?.buffered(16)?.use { Buffer().readFrom(it) }
|
streamFn().buffered(16).use { source1 ->
|
||||||
|
if (extraPage != null) {
|
||||||
|
streamFn2?.invoke()
|
||||||
|
?.buffered(16)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}.use { source2 ->
|
||||||
|
val actualSource = this@PagerPageHolder.mergeOrSplitPages(
|
||||||
|
Buffer().readFrom(source1),
|
||||||
|
source2?.let { Buffer().readFrom(it) },
|
||||||
|
)
|
||||||
|
|
||||||
|
val isAnimated = ImageUtil.isAnimatedAndSupported(actualSource)
|
||||||
|
Pair(actualSource, isAnimated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
actualSource = this@PagerPageHolder.mergeOrSplitPages(source1, source2)
|
|
||||||
val isAnimated = ImageUtil.isAnimatedAndSupported(source1) ||
|
|
||||||
(source2?.let { ImageUtil.isAnimatedAndSupported(source2) } ?: false)
|
|
||||||
withUIContext {
|
withUIContext {
|
||||||
val bgColor = ReaderBackgroundColor.fromPreference(viewer.config.readerTheme)
|
val bgColor = ReaderBackgroundColor.fromPreference(viewer.config.readerTheme)
|
||||||
if (!isAnimated) {
|
if (!isAnimated) {
|
||||||
if (bgColor.isSmartColor) {
|
if (bgColor.isSmartColor) {
|
||||||
val bgType = getBGType(viewer.config.readerTheme, context)
|
val bgType = getBGType(viewer.config.readerTheme, context)
|
||||||
if (page.bg != null && page.bgType == bgType) {
|
if (page.bg != null && page.bgType == bgType) {
|
||||||
setImage(actualSource, false, imageConfig)
|
setImage(source, false, imageConfig)
|
||||||
pageView?.background = page.bg
|
pageView?.background = page.bg
|
||||||
}
|
}
|
||||||
// if the user switches to automatic when pages are already cached, the bg needs to be loaded
|
// if the user switches to automatic when pages are already cached, the bg needs to be loaded
|
||||||
else {
|
else {
|
||||||
val background =
|
val background =
|
||||||
try {
|
try {
|
||||||
setBG(actualSource.peek().inputStream())
|
setBG(source.peek().inputStream())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Logger.e(e) { e.localizedMessage?.toString() ?: "" }
|
Logger.e(e) { e.localizedMessage?.toString() ?: "" }
|
||||||
ColorDrawable(Color.WHITE)
|
ColorDrawable(Color.WHITE)
|
||||||
}
|
}
|
||||||
setImage(actualSource, false, imageConfig)
|
setImage(source, false, imageConfig)
|
||||||
|
|
||||||
pageView?.background = background
|
pageView?.background = background
|
||||||
page.bg = pageView?.background
|
page.bg = pageView?.background
|
||||||
page.bgType = bgType
|
page.bgType = bgType
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setImage(actualSource, false, imageConfig)
|
setImage(source, false, imageConfig)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setImage(actualSource, true, imageConfig)
|
setImage(source, true, imageConfig)
|
||||||
if (bgColor.isSmartColor && page.bg != null) {
|
if (bgColor.isSmartColor && page.bg != null) {
|
||||||
pageView?.background = page.bg
|
pageView?.background = page.bg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
} catch (e: Exception) {
|
||||||
try {
|
Logger.e(e) { "Failed to set reader page image" }
|
||||||
actualSource?.let { closeSources(it) }
|
|
||||||
} catch (_: Exception) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue