Prevent GoneOnInvisibleListener leak

This commit is contained in:
Koitharu
2022-09-12 15:43:42 +03:00
parent 39e574e9dc
commit 2d727a0da8
3 changed files with 21 additions and 4 deletions

View File

@@ -37,11 +37,13 @@ abstract class BasePageHolder<B : ViewBinding>(
protected abstract fun onBind(data: ReaderPage)
fun onAttachedToWindow() {
@CallSuper
open fun onAttachedToWindow() {
delegate.onAttachedToWindow()
}
fun onDetachedFromWindow() {
@CallSuper
open fun onDetachedFromWindow() {
delegate.onDetachedFromWindow()
}

View File

@@ -25,12 +25,12 @@ class WebtoonHolder(
View.OnClickListener {
private var scrollToRestore = 0
private val goneOnInvisibleListener = GoneOnInvisibleListener(bindingInfo.progressBar)
init {
binding.ssiv.regionDecoderFactory = SkiaPooledImageRegionDecoder.Factory()
binding.ssiv.addOnImageEventListener(delegate)
bindingInfo.buttonRetry.setOnClickListener(this)
GoneOnInvisibleListener(bindingInfo.progressBar).attach()
}
override fun onBind(data: ReaderPage) {
@@ -42,6 +42,16 @@ class WebtoonHolder(
binding.ssiv.recycle()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
goneOnInvisibleListener.attach()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
goneOnInvisibleListener.detach()
}
override fun onLoadingStarted() {
bindingInfo.layoutError.isVisible = false
bindingInfo.progressBar.showCompat()

View File

@@ -19,5 +19,10 @@ class GoneOnInvisibleListener(
fun attach() {
view.viewTreeObserver.addOnGlobalLayoutListener(this)
onGlobalLayout()
}
}
fun detach() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
}