'Usecase inside a custom view
First of all, sorry for my english, this is google translator.
I am learning mvvm, maybe I have some misconception but I think I can understand it well.
My goal is to load an image in an imageview, but the final path of the image is built with the width of the imageview
GET /images
"images": {
"avatar": [64, 128],
"xxxxxx": [512, 640, 1024]
}
GET /user/1234567
"user": {
"username": "john",
"avatar": "123123.png"
}
Now if I want to display john's avatar in a 64px image the final path would be:
myserver.com/images/64/123123.png
If on the other hand, the size of the imageview is 600px I would use:
myserver.com/images/128/123123.png
To do this I have created a customview that extends an imageview and I use a usecase
@AndroidEntryPoint
class MyImageView(
context: Context, attrs: AttributeSet? = null,
) : ImageView(context, attrs) {
@Inject
lateinit var pictureUseCase: PictureUseCase
fun build(type: Picture.Type, endpoint: String) {
val type = when (type) {
0 -> Picture.Type.AVATAR
1 -> Picture.Type.XXXXXX
else -> throw Exception("unexpected")
}
val base = pictureUseCase(type, measuredWidth)
val awesomeurl = base + endpoint // Here i have the final url
// Picasso...
}
}
This code generates an error without importance in the execution of the app but it breaks the preview of the view
_layoutlib_._internal_.kotlin.UninitializedPropertyAccessException: lateinit property pictureUseCase has not been initialized
Is there an annotation to disable it in the preview?
Can I use a viewmodel inside a view?
Would there be a problem if I use this customview + viewmodel inside an adapter with hundreds of elements?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|