https://kotlinlang.org logo
#android
Title
# android
l

Loboda Deni

07/14/2020, 6:40 AM
What do you think, to what extent is it permissible to load dataBinding with logic? for example, if I need to process a click on a button, should I create a separate interface for this event, or is it permissible to directly call viewModel methods to handle this event? for example, if all the presentation logic is one single click, would it not be unnecessary to create a separate interface for this?
g

gildor

07/14/2020, 7:10 AM
why do you need interface for this at all? Just use binding to call m,ethod of your model
l

Loboda Deni

07/14/2020, 7:29 AM
I was told that it is worth protecting the view from logic as much as possible and that it is not correct to call methods for example viewModel from xml
all the same, in this case we get a rigid binding to a specific viewModel and reusing this xml will be problematic
g

gildor

07/14/2020, 7:39 AM
but it’s just a method in interface, if you have multiple VMs which implement the same interface
l

Loboda Deni

07/14/2020, 7:43 AM
that is, it turns out all the same separate interfaces are needed?
g

gildor

07/14/2020, 7:53 AM
still not sure what you mean
if you want to reuse one binding layout for multiple view models you have 2 choices: pass 1 view model interface as
model
variable and bind it, so every view model have to implement it pass multiple variables to layout and bind each of them, so it’s not necessary to implement any particular interface But option 1 looks better for me and this what we use mostly
l

Loboda Deni

07/14/2020, 7:56 AM
look, for example, I have a viewHolder, I can specify viewModel as the click event handler, but then this xml cannot be reused, but I can create a separate interface but then we get an extra class in the project structure .. and which approach is better I haven't understand
I just don't want to clog the project with interfaces in which there is only one method, for example, click processing
g

gildor

07/14/2020, 7:58 AM
Do you really have many view holders with only onclick method and no other things to bind?
like actual data of item
Usually we have view model for list item and layout for list item
l

Loboda Deni

07/14/2020, 8:01 AM
not. I have data as a separate object, event processing - as a separate one. but yes, it is often found that for one holder only one event is somehow
sorry for the tautology
g

gildor

07/14/2020, 8:03 AM
if you think it works better for you, okay, but we usually just have all listeners on view model, it makes bind code a lot easier
but in this case I still don’t see any issue, pass your event listener interface as separate variable
it can be one interface for all cases
l

Loboda Deni

07/14/2020, 8:14 AM
My question was, should there be a viewHolder event logic in a separate interface, or can it be combined with viewModel?
g

gildor

07/14/2020, 8:15 AM
I personally combine with viewModel (probably always)
l

Loboda Deni

07/14/2020, 8:16 AM
understood thanks
g

gildor

07/14/2020, 2:48 PM
Also on our project we now discourage using pure data classes for models, because it too restrictive and not flexible. you very often want to have additional logic now on in future, and usage of data class doesn't allow change logic of view model and would require refactoring even if very small behavior change is needed So we always wrap it to view model class
2 Views