From 3ff9fd8478a8a83ae485b7458bfd885efedb1044 Mon Sep 17 00:00:00 2001 From: duguyihou Date: Thu, 30 May 2024 07:54:21 +1000 Subject: [PATCH] 210 fix placeholder in android (#211) * fix: blurhash * refactor: circleProgressDrawable --- .../java/com/turboimage/TurboImageView.kt | 35 ++++++++++++++++ .../com/turboimage/TurboImageViewManager.kt | 40 +++++-------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/android/src/main/java/com/turboimage/TurboImageView.kt b/android/src/main/java/com/turboimage/TurboImageView.kt index ca29448..aa7005d 100644 --- a/android/src/main/java/com/turboimage/TurboImageView.kt +++ b/android/src/main/java/com/turboimage/TurboImageView.kt @@ -1,7 +1,10 @@ package com.turboimage import android.annotation.SuppressLint +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable import androidx.appcompat.widget.AppCompatImageView +import androidx.swiperefreshlayout.widget.CircularProgressDrawable import coil.size.Size import coil.transform.CircleCropTransformation import coil.transform.RoundedCornersTransformation @@ -18,6 +21,8 @@ class TurboImageView(private val reactContext: ThemedReactContext) : var src: String? = null var cachePolicy: String? = "memory" var crossfade: Int? = null + var blurHash: String? = null + var indicator: String? = null var resize: Size? = null var borderRadius: Int? = null @@ -26,6 +31,31 @@ class TurboImageView(private val reactContext: ThemedReactContext) : var monochrome: Int? = null var tint: Int? = null + val blurHashDrawable: Drawable? + get() { + return blurHash?.let { + drawBlurHash(this, it) + } + } + + val circleProgressDrawable: CircularProgressDrawable? + get() { + indicator?.let { + when (it) { + "medium" -> return CircularProgressDrawable(context).apply { + setStyle(CircularProgressDrawable.DEFAULT) + } + + "large" -> return CircularProgressDrawable(context).apply { + setStyle(CircularProgressDrawable.LARGE) + } + + else -> return null + } + } + return null + } + val transformations: MutableList get() { val list = mutableListOf() @@ -53,4 +83,9 @@ class TurboImageView(private val reactContext: ThemedReactContext) : return list } + + private fun drawBlurHash(view: TurboImageView, blurHash: String): Drawable { + val bitmap = BlurHashDecoder.decode(blurHash, 8, 8) + return BitmapDrawable(view.context.resources, bitmap) + } } diff --git a/android/src/main/java/com/turboimage/TurboImageViewManager.kt b/android/src/main/java/com/turboimage/TurboImageViewManager.kt index d2e98c0..f7a2b07 100644 --- a/android/src/main/java/com/turboimage/TurboImageViewManager.kt +++ b/android/src/main/java/com/turboimage/TurboImageViewManager.kt @@ -1,12 +1,10 @@ package com.turboimage -import android.graphics.drawable.BitmapDrawable -import android.graphics.drawable.Drawable import android.os.Build.VERSION.SDK_INT import android.widget.ImageView.ScaleType import androidx.swiperefreshlayout.widget.CircularProgressDrawable -import coil.Coil.setImageLoader import coil.Coil.imageLoader +import coil.Coil.setImageLoader import coil.ImageLoader import coil.decode.GifDecoder import coil.decode.ImageDecoderDecoder @@ -25,8 +23,6 @@ import com.facebook.react.uimanager.annotations.ReactProp class TurboImageViewManager : SimpleViewManager() { override fun getName() = REACT_CLASS private var disposable: Disposable? = null - private var circleProgressDrawable: CircularProgressDrawable? = null - private var blurHashDrawable: Drawable? = null override fun getExportedCustomBubblingEventTypeConstants(): Map { return mapOf( @@ -72,19 +68,20 @@ class TurboImageViewManager : SimpleViewManager() { } }.build().apply { setImageLoader(this) } - val diskCacheEnabled = - if (view.cachePolicy != "memory") CachePolicy.ENABLED else CachePolicy.DISABLED - val request = ImageRequest.Builder(view.context) .data(view.src) .target(view) .listener(TurboImageListener(view)) - .memoryCachePolicy(CachePolicy.ENABLED) - .diskCachePolicy(diskCacheEnabled) - .placeholder(blurHashDrawable ?: circleProgressDrawable) + .diskCachePolicy( + if (view.cachePolicy != "memory") + CachePolicy.ENABLED + else + CachePolicy.DISABLED + ) + .placeholder(view.blurHashDrawable ?: view.circleProgressDrawable) .transformations(view.transformations) .crossfade(view.crossfade ?: CrossfadeDrawable.DEFAULT_DURATION) - .error(blurHashDrawable) + .error(view.blurHashDrawable) .size(view.resize ?: Size.ORIGINAL) .build() @@ -105,9 +102,7 @@ class TurboImageViewManager : SimpleViewManager() { @ReactProp(name = "blurhash") fun setBlurHash(view: TurboImageView, blurhash: String?) { - blurhash?.let { - blurHashDrawable = drawBlurHash(view, it) - } + view.blurHash = blurhash } @ReactProp(name = "cachePolicy") @@ -122,15 +117,7 @@ class TurboImageViewManager : SimpleViewManager() { @ReactProp(name = "indicator") fun setIndicator(view: TurboImageView, indicator: String?) { - circleProgressDrawable = if (indicator == "medium") { - CircularProgressDrawable(view.context).apply { - setStyle(CircularProgressDrawable.DEFAULT) - } - } else { - CircularProgressDrawable(view.context).apply { - setStyle(CircularProgressDrawable.LARGE) - } - } + view.indicator = indicator } @ReactProp(name = "fadeDuration") @@ -173,11 +160,6 @@ class TurboImageViewManager : SimpleViewManager() { view.tint = tint } - private fun drawBlurHash(view: TurboImageView, blurHash: String): Drawable { - val bitmap = BlurHashDecoder.decode(blurHash, 8, 8) - return BitmapDrawable(view.context.resources, bitmap) - } - companion object { private const val REACT_CLASS = "TurboImageView" private val RESIZE_MODE = mapOf(