Davide Giuseppe Farella
03/05/2019, 2:59 AMAny
?
Let’s say that I got this class and I want the better way for ensure that image
is Drawable, Int, Bitmap, File, String or Uri
and placeholder is Drawable, Int, Bitmap or File
without crashing at runtimeLeoColman
03/05/2019, 3:00 AMLeoColman
03/05/2019, 3:00 AMLeoColman
03/05/2019, 3:02 AMDavide Giuseppe Farella
03/05/2019, 3:02 AMstreetsofboston
03/05/2019, 3:18 AMDavide Giuseppe Farella
03/05/2019, 3:20 AMandroidx.annotation.DrawableRes
would be greatLeoColman
03/05/2019, 4:30 AMLeoColman
03/05/2019, 4:30 AMDavide Giuseppe Farella
03/05/2019, 4:32 AMimageLoader {
image = item.image
placeHolder = item.imagePlaceHolder
target = tvChannelImage
}
would become this:
imageLoader {
image = ImageResource( item.image )
placeHolder = ImageResource( item.imagePlaceHolder )
target = tvChannelImage
}
which I don’t like 🙂Davide Giuseppe Farella
03/05/2019, 4:34 AMyou’re trying to escape the type system a little bit, and I don’t really understood whyWho don’t like to break the rules? 😄
ghedeon
03/05/2019, 6:27 AMInt
, let say a vector drawable that needs to be of different color, then you need to provide a color filter, along the Int
.ghedeon
03/05/2019, 6:29 AMitem.image.toRes()
Davide Giuseppe Farella
03/05/2019, 6:37 AMimageDrawable
, imageBitmap
, etc and add some boilerplate to the BuilderPaul Woitaschek
03/05/2019, 7:08 AMtseisel
03/05/2019, 7:09 AMinline var Builder.imageDrawableRes: Int
get() = throw Exception("No getter here")
set(value) = this.image = DrawableResource(value)
You can then use it in the DSL like this :
imageLoader {
imageDrawableRes = R.drawable.my_awesome_image
}
Davide Giuseppe Farella
03/05/2019, 7:10 AM