Created
December 17, 2020 19:58
-
-
Save mitchtabian/2f88f09138fa154461840c50c25c9b01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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