Home > database >  Android: Applying MVVM the correct way (Part III)
Android: Applying MVVM the correct way (Part III)

Time:10-27

I received valuable help from the community in order to make the best possible with my mvvm migration, but still in doubt with some questions, for example:

I've moved the next method from the activity to the ViewModel (because it's mainly bussiness), but not sure if it is at all correct because it handles a view. One of the parameters is a programmatically created ImageView and I would say even if it is programmatically created it is still a view, and indirectly I'm passing a context to the VM (and directly passing a view element) when the VM is not supossed to handle views.

In short, I'm not sure -being mvvm strict- if could be acceptable to move this method (or any method that handles a programmatically created view) to the VM.

public void setCheckBoxCheckedState_TestMode(List<AnswerDTO> answers,
                 int pageNumber, int cbSelectedId, ImageView cbAnswer)
{
    for (int j = 0; j < answers.size(); j  ) {
        int cbIndex = answers.size() * pageNumber   j;
        if (cbIndex != cbSelectedId) {
            setCheckBoxUnchecked_TestMode(cbIndex);
            String strRest = cbAnswer.getTag().toString().substring(1);
            cbAnswer.setTag("0"   strRest);
        } else {
            String cbAnswerJTag = cbAnswer.getTag().toString();
            String cbAnswerJState = cbAnswerJTag.split("@")[0];

            if (cbAnswerJState.equals("1")) { // 0=>unchecked|1=>checked
                setCheckBoxUnchecked_TestMode(cbIndex);
                cbAnswer.setTag("0@"   cbAnswerJTag.split("@")[1]);
            } else {
                setCheckBoxChecked_TestMode(cbIndex);
                cbAnswer.setTag("1@"   cbAnswerJTag.split("@")[1]);
            }
        }
    }
}

CodePudding user response:

First off, any time you're programmatically creating Views, you're probably on the wrong track. Chances are you want a RecyclerView to do this for you. Another huge red flag is that you're storing information in your ImageView using getTag(). Whatever this information is, it should be stored in a List or some other data structure (probably just in the List<AnswerDTO> you already have).

Basically, you're trying to implement MVVM when you should be learning more core Android concepts first. Start with a class like this one.

CodePudding user response:

Really you should be telling the view model what event has changed (for example viewModel.onSelectionChange(itemDataId)) and the view model should then know what "data" it needs to change in order to update the view model internal state, which should be observed by the view, usually with data binding.

Currently you are providing the view model with "view" related objects - ImageView - it should not be setting state on view objects directly, that is the views concern, based on view model state. It looks like you just moved a view function to the view model, and it doesn't belong there.

Something like this :

View <-- data binding --> ViewModel <------> Model

Or without data binding :

View -- event ---> ViewModel <------> Model

View <---- data updates --- ViewModel <------> Model

  • Related