Skip to content

Commit

Permalink
210 fix placeholder in android (#211)
Browse files Browse the repository at this point in the history
* fix: blurhash

* refactor: circleProgressDrawable
  • Loading branch information
duguyihou authored May 29, 2024
1 parent c834a5c commit 3ff9fd8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
35 changes: 35 additions & 0 deletions android/src/main/java/com/turboimage/TurboImageView.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<Transformation>
get() {
val list = mutableListOf<Transformation>()
Expand Down Expand Up @@ -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)
}
}
40 changes: 11 additions & 29 deletions android/src/main/java/com/turboimage/TurboImageViewManager.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,8 +23,6 @@ import com.facebook.react.uimanager.annotations.ReactProp
class TurboImageViewManager : SimpleViewManager<TurboImageView>() {
override fun getName() = REACT_CLASS
private var disposable: Disposable? = null
private var circleProgressDrawable: CircularProgressDrawable? = null
private var blurHashDrawable: Drawable? = null

override fun getExportedCustomBubblingEventTypeConstants(): Map<String, Any> {
return mapOf(
Expand Down Expand Up @@ -72,19 +68,20 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {
}
}.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()

Expand All @@ -105,9 +102,7 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {

@ReactProp(name = "blurhash")
fun setBlurHash(view: TurboImageView, blurhash: String?) {
blurhash?.let {
blurHashDrawable = drawBlurHash(view, it)
}
view.blurHash = blurhash
}

@ReactProp(name = "cachePolicy")
Expand All @@ -122,15 +117,7 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {

@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")
Expand Down Expand Up @@ -173,11 +160,6 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {
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(
Expand Down

0 comments on commit 3ff9fd8

Please sign in to comment.