Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Created December 17, 2020 19:58
Show Gist options
  • Save mitchtabian/2f88f09138fa154461840c50c25c9b01 to your computer and use it in GitHub Desktop.
Save mitchtabian/2f88f09138fa154461840c50c25c9b01 to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
@Composable
fun loadPicture(url: String, @DrawableRes defaultImage: Int): MutableState<Bitmap?> {
val bitmapState: MutableState<Bitmap?> = mutableStateOf(null)
// show default image while image loads
Glide.with(ContextAmbient.current)
.asBitmap()
.load(defaultImage)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) { }
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
bitmapState.value = resource
}
})
// get network image
Glide.with(ContextAmbient.current)
.asBitmap()
.load(url)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) { }
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
bitmapState.value = resource
}
})
return bitmapState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment