What do you think, to what extent is it permissibl...
# android
l
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
why do you need interface for this at all? Just use binding to call m,ethod of your model
l
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
but it’s just a method in interface, if you have multiple VMs which implement the same interface
l
that is, it turns out all the same separate interfaces are needed?
g
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
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
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
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
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
My question was, should there be a viewHolder event logic in a separate interface, or can it be combined with viewModel?
g
I personally combine with viewModel (probably always)
l
understood thanks
g
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