-4

I am trying to understand how MVVM works. Here are two examples (in Andrdoid) on how I understand it should work, please tell me if I understood correctly.

I currently want to make a list of radiobutton "Sex" with currently two possible values (Male, female). I expect it to change to "Gender" and three possible values (Male, female, other). I have the label as a TextView in my View that references the 'label' field of my viewModel. The label of my view model itself is referencing a field of my Model. Right ? Also I should write the view as able to print a list of radio buttons from a table. That way, if I add a sex/gender, the View does not have to change. What I am wondering is where this list "Male, Female" should be stored. Is it in the ViewModel or is it in the Model ? If this list is stored in a database, who should extract it ? The ViewModel or the Model ?

I have a "new password" field. The password have to respect certain conditions (length, special numbers...). How are those condition checking handled ? The View will contain the EditText field password. When the user types the password, the view calls the ViewModel which itself will call the Model to check. If the password doesn't fit, the model will return one or more warning values (length_too_short, no_special_char), the viewModel will then translates those warnings ("The password has to be 8 characters minimum", "At least one special character") and will call the View to show password error (the password field becomes red and under it the view writes the error message it receives).

Thank you in advance for your help

2
0

Yes, you are understanding the MVVM pattern correctly.

Also I should write the view as able to print a list of radio buttons from a table. That way, if I add a sex/gender, the View does not have to change.

That is not a requirement out of the MVVM model. It is very well possible that for UX reasons you decide to change the View to use a drop-down list rather than a set of radio buttons.

What I am wondering is where this list "Male, Female" should be stored. Is it in the ViewModel or is it in the Model ? If this list is stored in a database, who should extract it ? The ViewModel or the Model ?

This depends on how the Model deals with those values. If the Model just has a fixed hardcoded enumeration, then the ViewModel would be responsible for having a mapping between those enumerators and whatever way the View shows them. It would be the responsibility of the View to show a correct textual representation of the values.

If the Model is more flexible and the allowed values are, for example, stored in a database, then the Model should have a method to provide that list, along with the metadata that is also in the database such as translation keys, to the ViewModel.

0
0

Also I should write the view as able to print a list of radio buttons from a table. That way, if I add a sex/gender, the View does not have to change.

The view is responsible for changing the view layer so even if you make this enum/table it is belonging to the view; not the model, controller or view model. So if you need to change the view, it should be impossible to do it without changing something in the view layer.

I have a "new password" field. The password have to respect certain conditions (length, special numbers...). How are those condition checking handled ?

The view model handles data in the life-cycle. That's it. Not validation. Validation is for a controller. The controller receives the input, it can then call on the view layer to do a .setError() on the view object using listeners/observable pattern.

In Android most who use Mvvm has a architecture where there is an activity, view model and various xml layouts. If you are using this then realize the activity is a view-controller, the view model is a view model and the model is the database/usecase/API call/services ect. You can certainly decouple activity from the view, but the activity is really required to be a controller due to the life-cycle and other responsibilities (notice there is no constructor in Activity, Android made the first method onCreate()). You can also decouple the controller from activity and make it blank. This makes it easier to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.