Last active
December 17, 2021 20:34
-
-
Save bherbst/d50fd7758013a3269b83c2356b509b23 to your computer and use it in GitHub Desktop.
HackyGlanceNetworkImage
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
@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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)