nglauber
11/06/2019, 1:01 AMAdam Powell
11/06/2019, 1:02 AM@Model
, and return that from the effectnglauber
11/06/2019, 1:23 AMImage
using a Drawable
or Bitmap
? 🤔Leland Richardson [G]
11/06/2019, 1:53 AMAndroidImage
since it’s marked as internal. I think we should probably change thatnglauber
11/06/2019, 1:54 AMLeland Richardson [G]
11/06/2019, 1:54 AMImage
is an interface, so you can implement it yourself. the code for AndroidImage
is basically just a wrapper around Bitmap
so it’s not a lot of code to copynglauber
11/06/2019, 1:59 AMLeland Richardson [G]
11/06/2019, 1:59 AM@Composable
fun Image(url: String, aspectRatio: Float) {
var image by +state<Image?> { null }
var drawable by +state<Drawable?> { null }
+onCommit(url) {
val picasso = Picasso.get()
val target = object : Target {
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
// TODO(lmr): we could use the drawable below
drawable = placeHolderDrawable
}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
drawable = errorDrawable
}
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
image = bitmap?.let { AndroidImage(it) }
}
}
picasso
.load(url)
.into(target)
onDispose {
image = null
drawable = null
picasso.cancelRequest(target)
}
}
// TODO(lmr): what's the best way to do aspect ratio here, and have the image fill the available
// width?
Container(height = 100.dp * aspectRatio, width = 100.dp) {
val theImage = image
val theDrawable = drawable
if (theImage != null) {
DrawImage(image = theImage)
} else if (theDrawable != null) {
Draw { canvas, parentSize -> theDrawable.draw(canvas.nativeCanvas) }
}
}
}
nglauber
11/06/2019, 2:00 AMLeland Richardson [G]
11/06/2019, 2:00 AMnglauber
11/06/2019, 2:26 AMImage
component 😄
https://medium.com/@nglauber/jetpack-compose-part-ii-async-data-tabs-scroller-fab-state-efc8e267b914Leland Richardson [G]
11/06/2019, 7:33 PMnglauber
11/06/2019, 7:34 PMghedeon
11/09/2019, 2:00 PMitnoles
11/17/2019, 5:13 AMLeland Richardson [G]
11/18/2019, 7:30 AM