Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Attempt to recycle last bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jan 15, 2023
1 parent ca9c4cc commit 40df0ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
24 changes: 9 additions & 15 deletions image/src/main/java/com/kylecorry/ceres/image/AsyncImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class AsyncImageView(context: Context, attrs: AttributeSet?) : AppCompatImageVie
private var imageLoader: ControlledRunner<Unit> = ControlledRunner()
private var lastBitmap: Bitmap? = null

private var shouldClearBitmap = true

fun setImageBitmap(lifecycleOwner: LifecycleOwner, provider: suspend () -> Bitmap) {
lifecycleOwner.lifecycle.removeObserver(this)
lifecycleOwner.lifecycle.addObserver(this)
Expand All @@ -38,54 +36,50 @@ class AsyncImageView(context: Context, attrs: AttributeSet?) : AppCompatImageVie
}
withContext(Dispatchers.Main) {
if (lastBitmap?.isRecycled == false) {
shouldClearBitmap = false
super.setImageBitmap(lastBitmap)
shouldClearBitmap = true
}
}
}
}
}

fun recycleLastBitmap(clearView: Boolean = true) {
if (clearView){
setImageDrawable(null)
}
lastBitmap?.recycle()
lastBitmap = null
}

override fun setImageBitmap(bm: Bitmap?) {
imageLoader.cancel()
super.setImageBitmap(bm)
tryClear()
}

override fun setImageDrawable(drawable: Drawable?) {
imageLoader.cancel()
super.setImageDrawable(drawable)
tryClear()
}

override fun setImageResource(resId: Int) {
imageLoader.cancel()
super.setImageResource(resId)
tryClear()
}

override fun setImageURI(uri: Uri?) {
imageLoader.cancel()
super.setImageURI(uri)
tryClear()
}

override fun setImageIcon(icon: Icon?) {
imageLoader.cancel()
super.setImageIcon(icon)
tryClear()
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == Lifecycle.Event.ON_DESTROY) {
imageLoader.cancel()
}
}

private fun tryClear(){
if (shouldClearBitmap) {
lastBitmap?.recycle()
recycleLastBitmap(true)
}
}

Expand Down
4 changes: 3 additions & 1 deletion list/src/main/java/com/kylecorry/ceres/list/ListItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.core.view.setPadding
import androidx.lifecycle.LifecycleOwner
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.tryOrLog
import com.kylecorry.andromeda.core.tryOrNothing
import com.kylecorry.andromeda.core.ui.Colors
import com.kylecorry.andromeda.core.ui.setCompoundDrawables
import com.kylecorry.ceres.image.AsyncImageView
Expand Down Expand Up @@ -61,6 +60,9 @@ data class ResourceListIcon(
override fun apply(image: ImageView) {
image.isVisible = true
image.setImageResource(id)
if (image is AsyncImageView){
image.recycleLastBitmap(false)
}
Colors.setImageColor(image, tint)

image.scaleType = scaleType
Expand Down

0 comments on commit 40df0ab

Please sign in to comment.