mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor: Refactor PagerPageHolder
This commit is contained in:
parent
644032190c
commit
9857ff943e
1 changed files with 52 additions and 86 deletions
|
@ -33,11 +33,13 @@ import eu.kanade.tachiyomi.util.system.ImageUtil.isPagePadded
|
|||
import eu.kanade.tachiyomi.util.system.ThemeUtil
|
||||
import eu.kanade.tachiyomi.util.system.bottomCutoutInset
|
||||
import eu.kanade.tachiyomi.util.system.dpToPx
|
||||
import eu.kanade.tachiyomi.util.system.e
|
||||
import eu.kanade.tachiyomi.util.system.getResourceColor
|
||||
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.topCutoutInset
|
||||
import eu.kanade.tachiyomi.util.system.withIOContext
|
||||
import eu.kanade.tachiyomi.util.system.withUIContext
|
||||
import eu.kanade.tachiyomi.util.view.backgroundColor
|
||||
import eu.kanade.tachiyomi.util.view.isVisibleOnScreen
|
||||
|
@ -87,41 +89,12 @@ class PagerPageHolder(
|
|||
*/
|
||||
private var loadJob: Job? = null
|
||||
|
||||
/**
|
||||
* Job for status changes of the page.
|
||||
*/
|
||||
private var statusJob: Job? = null
|
||||
|
||||
/**
|
||||
* Job for progress changes of the page.
|
||||
*/
|
||||
private var progressJob: Job? = null
|
||||
|
||||
/**
|
||||
* Job for loading the page.
|
||||
*/
|
||||
private var extraLoadJob: Job? = null
|
||||
|
||||
/**
|
||||
* Job for status changes of the page.
|
||||
*/
|
||||
private var extraStatusJob: Job? = null
|
||||
|
||||
/**
|
||||
* Job for progress changes of the page.
|
||||
*/
|
||||
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 extraStatus = Page.State.READY
|
||||
private var progress: Int = 0
|
||||
private var extraProgress: Int = 0
|
||||
|
||||
private var scope = MainScope()
|
||||
|
||||
|
@ -190,12 +163,12 @@ class PagerPageHolder(
|
|||
*/
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
|
||||
loadJob?.cancel()
|
||||
loadJob = null
|
||||
extraLoadJob?.cancel()
|
||||
extraLoadJob = null
|
||||
|
||||
cancelReadImageHeader()
|
||||
(pageView as? SubsamplingScaleImageView)?.setOnImageEventListener(null)
|
||||
}
|
||||
|
||||
|
@ -334,14 +307,6 @@ class PagerPageHolder(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes from the read image header subscription.
|
||||
*/
|
||||
private fun cancelReadImageHeader() {
|
||||
readImageHeaderJob?.cancel()
|
||||
readImageHeaderJob = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the page is queued.
|
||||
*/
|
||||
|
@ -369,7 +334,7 @@ class PagerPageHolder(
|
|||
/**
|
||||
* Called when the page is ready.
|
||||
*/
|
||||
private fun setImage() {
|
||||
private suspend fun setImage() {
|
||||
progressBar.isVisible = true
|
||||
if (extraPage == null) {
|
||||
progressBar.completeAndFadeOut()
|
||||
|
@ -378,59 +343,60 @@ class PagerPageHolder(
|
|||
}
|
||||
errorLayout?.isVisible = false
|
||||
|
||||
cancelReadImageHeader()
|
||||
val streamFn = page.stream ?: return
|
||||
val streamFn2 = extraPage?.stream
|
||||
|
||||
readImageHeaderJob = scope.launchIO {
|
||||
val streamFn = page.stream ?: return@launchIO
|
||||
val streamFn2 = extraPage?.stream
|
||||
|
||||
var actualSource: BufferedSource? = null
|
||||
try {
|
||||
val source1 = streamFn().buffered(16).use { Buffer().readFrom(it) }
|
||||
val source2 = streamFn2?.invoke()?.buffered(16)?.use { Buffer().readFrom(it) }
|
||||
|
||||
actualSource = this@PagerPageHolder.mergeOrSplitPages(source1, source2)
|
||||
val isAnimated = ImageUtil.isAnimatedAndSupported(source1) ||
|
||||
(source2?.let { ImageUtil.isAnimatedAndSupported(source2) } ?: false)
|
||||
withUIContext {
|
||||
val bgColor = ReaderBackgroundColor.fromPreference(viewer.config.readerTheme)
|
||||
if (!isAnimated) {
|
||||
if (bgColor.isSmartColor) {
|
||||
val bgType = getBGType(viewer.config.readerTheme, context)
|
||||
if (page.bg != null && page.bgType == bgType) {
|
||||
setImage(actualSource, false, imageConfig)
|
||||
pageView?.background = page.bg
|
||||
}
|
||||
// if the user switches to automatic when pages are already cached, the bg needs to be loaded
|
||||
else {
|
||||
val background =
|
||||
try {
|
||||
setBG(actualSource.peek().inputStream())
|
||||
} catch (e: Exception) {
|
||||
Logger.e(e) { e.localizedMessage?.toString() ?: "" }
|
||||
ColorDrawable(Color.WHITE)
|
||||
}
|
||||
setImage(actualSource, false, imageConfig)
|
||||
|
||||
pageView?.background = background
|
||||
page.bg = pageView?.background
|
||||
page.bgType = bgType
|
||||
}
|
||||
} else {
|
||||
setImage(actualSource, false, imageConfig)
|
||||
}
|
||||
try {
|
||||
val (source, isAnimated, _bg) = withIOContext {
|
||||
streamFn().buffered(16).use { source1 ->
|
||||
if (extraPage != null) {
|
||||
streamFn2?.invoke()
|
||||
?.buffered(16)
|
||||
} else {
|
||||
setImage(actualSource, true, imageConfig)
|
||||
if (bgColor.isSmartColor && page.bg != null) {
|
||||
pageView?.background = page.bg
|
||||
null
|
||||
}.use { source2 ->
|
||||
val actualSource = this@PagerPageHolder.mergeOrSplitPages(
|
||||
Buffer().readFrom(source1),
|
||||
source2?.let { Buffer().readFrom(it) },
|
||||
)
|
||||
|
||||
val isAnimated = ImageUtil.isAnimatedAndSupported(actualSource)
|
||||
val bgColor = ReaderBackgroundColor.fromPreference(viewer.config.readerTheme)
|
||||
val bgType = getBGType(viewer.config.readerTheme, context)
|
||||
val background = if (!isAnimated && bgColor.isSmartColor) {
|
||||
if (page.bg != null && page.bgType == bgType) page.bg
|
||||
else {
|
||||
try {
|
||||
setBG(actualSource.peek().inputStream())
|
||||
} catch (e: Exception) {
|
||||
Logger.e(e) { e.localizedMessage?.toString() ?: "" }
|
||||
ColorDrawable(Color.WHITE)
|
||||
}
|
||||
}
|
||||
} else if (bgColor.isSmartColor && page.bg != null) {
|
||||
page.bg
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
Triple(actualSource, isAnimated, Pair(background, bgType))
|
||||
}
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
try {
|
||||
actualSource?.let { closeSources(it) }
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
|
||||
withUIContext {
|
||||
val (bg, bgType) = _bg
|
||||
if (bg != null) pageView?.background = bg
|
||||
if (!isAnimated && page.bg == null && page.bgType != bgType) {
|
||||
page.bg = pageView?.background
|
||||
page.bgType = bgType
|
||||
}
|
||||
setImage(source, isAnimated, imageConfig)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.e(e)
|
||||
withUIContext {
|
||||
setError()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue