Skip to content

Instantly share code, notes, and snippets.

@bherbst
Last active December 17, 2021 20:34
Show Gist options
  • Save bherbst/d50fd7758013a3269b83c2356b509b23 to your computer and use it in GitHub Desktop.
Save bherbst/d50fd7758013a3269b83c2356b509b23 to your computer and use it in GitHub Desktop.
HackyGlanceNetworkImage
@Composable
fun NetworkImage(imageUrl: String, description: String) {
val context = LocalContext.current
LaunchedEffect(imageUrl) {
Coil.execute(
ImageRequest.Builder(context)
.data(imageUrl)
.target { result ->
loadedBitmaps[imageUrl] = (result as BitmapDrawable).bitmap
}
.build()
)
}
Image(
provider = if (loadedBitmaps.containsKey(imageUrl)) {
ImageProvider(loadedBitmaps[imageUrl]!!)
} else {
ImageProvider(R.drawable.placeholder_image)
},
contentDescription = description
)
}
private var loadedBitmaps: MutableMap<String, Bitmap> = mutableMapOf()
@bherbst
Copy link
Author

bherbst commented Dec 17, 2021

Please don't actually use this, it was for an innovation week project and was a pragmatic but relatively fragile way to load images for a Glance widget :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment