Sam
09/11/2020, 5:44 AM<data>
<variable name="user" type="com.example.User"/>
</data>
2. We should build XML to be pure , it mean does not include code because easier to management. debug, troubleshoot (Kotlin file for code and XML for UI). we should use Databinding
for binding view to safety-reference. we still achieve the MVVM.
How do you think about these points?
Thanks.Desmond Teo
09/11/2020, 5:52 AMSam
09/11/2020, 5:55 AMgildor
09/11/2020, 6:52 AMmust have databinding in MVVMOf course itâs not a requirement
gildor
09/11/2020, 6:53 AMgildor
09/11/2020, 6:53 AMSam
09/11/2020, 7:01 AMmust
to should
.
Second point:
1. Should use ViewBinding/Databinding for generated view from XML for type safety.
2. XML and code mix into the same file XML, someone see it hard to management in multiple modules, troubleshoot bugs, sometimes it gets confusing if someone uses more comparison
, binding adapter etc.gildor
09/11/2020, 7:02 AMgildor
09/11/2020, 7:03 AMgildor
09/11/2020, 7:03 AMSam
09/11/2020, 7:03 AMtype safety I think not really related to architecture
Yup, I think that too.gildor
09/11/2020, 7:05 AMXML and code mix into the same fileYou do not mix code, you just bind some property to some view, I wouldnât call it mixing code
gildor
09/11/2020, 7:05 AMtroubleshoot bugsIt indeed may be a problem
Sam
09/11/2020, 7:10 AMXML and code mix into the same file XML
=> it looks like
android:text= "${Converter.dateToString(viewmodel.birthDate)}"
android:textColor='@{ notice.action == "continue" ? @color/enabledPurple : @color/disabledGray}'
gildor
09/11/2020, 7:10 AMgildor
09/11/2020, 7:10 AMgildor
09/11/2020, 7:10 AMgildor
09/11/2020, 7:11 AMgildor
09/11/2020, 7:11 AMandroid:text="@{viewmodel.birthDate}"
Do not expose, data which you donât want exposegildor
09/11/2020, 7:13 AMandroid:textColor='@color/purple_button_text'
android:enabled="@{notice.canContinue}
gildor
09/11/2020, 7:13 AMpurple_button_text
just a simple android color with enable/default selectorgildor
09/11/2020, 7:16 AMSam
09/11/2020, 7:16 AMSam
09/11/2020, 7:17 AMgildor
09/11/2020, 7:21 AMgildor
09/11/2020, 7:35 AMJoost Klitsie
09/11/2020, 8:26 AMJoost Klitsie
09/11/2020, 8:33 AMgildor
09/11/2020, 8:38 AMwithout strict developers it will be abused
a big risk to become a big messSame as any other approach
gildor
09/11/2020, 8:38 AMputting any kind of java code into XML is in my opinion a bad ideaItâs not âany java codeâ
gildor
09/11/2020, 8:39 AMgildor
09/11/2020, 8:40 AMGurupad Mamadapur [FH]
09/11/2020, 11:03 AMJoost Klitsie
09/11/2020, 11:06 AMJoost Klitsie
09/11/2020, 11:07 AMJoost Klitsie
09/11/2020, 11:07 AMJoost Klitsie
09/11/2020, 11:08 AMJoost Klitsie
09/11/2020, 11:09 AMgildor
09/11/2020, 1:08 PMbut databinding to me is very easy to use unwiselyCould you show an example?
Especially the problem that you cannot write unit tests for it is dangerousYou mentioned how manually written binding looks like, it's a fragment/activity with manual observing of data, how can you test it with unit tests? If you compare with databindings it actually better, less manually written code, less bugs. And as I mentioned previously, databinding also superior because it's much harder to mix you representation with your business logic, comparing to have everything in the fragment. bindings also often force you to extracting work with specific view apis to binding adapter, improving separation of concerns I don't want to say databindings is the best thing in the world, I worked on different architectures but bindings is definitely not the worst approach, they allow to have very clean code, remove boilerplate, have a nice border between layers, I see many problems too, but none of them is one of what is mentioned by you
Joost Klitsie
09/11/2020, 2:03 PMandroid:text= "${Converter.dateToString(viewmodel.birthDate)}"
android:textColor='@{ notice.action == "continue" ? @color/enabledPurple : @color/disabledGray}'
Like the example given by the original poster đ
Well if you use viewbinding, I am unsure as I do not, can't you mock it? I am unsure about it. In the old world with butterknife view mocking was super easy but I use kotlin synthetic things now (I know I shouldn't) and there I simply stopped writing tests. But anyway, I can imagine something like this:
var viewBinding: MyViewBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewBinding = MyViewBinding.inflate(view) // Or whatever it is
viewModel.viewState.observe(viewLifecycleOwner) { viewState ->
viewBinding?.apply {
error.apply {
isVisible = viewState.errorIsVisible
text = viewState.errorText
}
someText.text = viewState.someText
someButton.isEnabled = viewState.someButtonIsEnabled
// etc...
}
}
}
So in the case you can mock the ViewBinding, then if you would intercept the observer, you can trigger it and see if there is interaction with the views
In this case you have here in Java/Kotlin the setText(string)
instead of the android:text="string"
so in terms of code it is almost the same.
If you cannot mock the view layer, then this code is at least as testable as the databinding in xml đgildor
09/13/2020, 2:50 PMJoost Klitsie
09/13/2020, 3:00 PMgildor
09/13/2020, 3:03 PMgildor
09/13/2020, 3:03 PMgildor
09/13/2020, 3:04 PMgildor
09/13/2020, 3:04 PMdeviant
09/30/2020, 12:40 PM