mardi 16 février 2016

How does the Presenter know which Model is manipulated through it's showing View?

I have started to study MVP on Android framework since I would like to start my future works in some design patters and I read that MVP is more than appropriate for building Android apps.

I have a question about an MVP principle:

The example is:

I have a View that have 2 interaction options:

  • first is listing items on the screen, for example: fruits.
  • second is when the fruits are listed user can click to it and it should print out the number of letters in the word

The Presenter should catch the event when a fruit is clicked, count the letters, and call the View's print function.

With MVP I tried this with this approach:

My View:

public class MainActivity extends Activity implements MainView, View.OnClickListener {

ViewGroup fruitsContainer;
Button    listFruitsBtn;

MyPresenter myPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Making my presenter, its only parameter is a MainView, this MainView.
    myPresenter = new MyPresenter(this);

    fruitsContainer = (ViewGroup) findViewById(R.id.fruitsContainer);
    listFruitsBtn = (Button) findViewById(R.id.list_fruits_btn);
    listFruitsBtn.setOnClickListener(this);

}

/**
 * Showing fruits, making a TextView for each.
 */
@Override
public void showFruits(List<String> fruits) {

    fruitsContainer.removeAllViews();

    for (String fruit : fruits) {
        TextView textView = new TextView(this);
        textView.setText(fruit);

        //Question part 1: How does the presenter will know WHICH fruit is clicked?
        textView.setOnClickListener(this);

        fruitsContainer.addView(textView);
    }
}

/**
 * When user clicks on the "show fruits" button, the view calls for presenter to take action.
 */
@Override
public void onClick(View v) {

    if (v.equals(listFruitsBtn)) {
        myPresenter.fruitButtonClicked();
    }
    else if(v instanceof TextView)
    {
        TextView textView = (TextView) v;
        //Question part 2: How does the presenter will know WHICH fruit is clicked?
        //In theory this operation is forbidden as far as I know.
        //MVP does not supports views to know anything about the Model that they are showing.
        myPresenter.fruitIsClicked(textView.getText().toString());
    }

    }
}

My Presenter:

public class MyPresenter {

MainView mainView;

public MyPresenter(MainView mainView) {
    this.mainView = mainView;
}

/**
 * Got a callback for "show fruits" event invoked
 * so we call the mainView's showFruits function with the model's data parameter
 */
public void fruitButtonClicked() {
    this.mainView.showFruits(MyModel.getFruitsList());
}

//In theory this operation is forbidden as far as I know.
//MVP does not supports views to know anything about the Model that they are showing.
public void fruitIsClicked(String fruit) {

    //Clicked operation - counting letters - call print operation of myView



   }
}

Attention: The Model being a String here is only because for simplifying the example, it could be any robust class.

As I understood one of the MVP's principle that Views does not know anything about the Model they are showing. So one does not okay to save the Model instance as an attribute of the View instance, and later on when needed (when the interaction made) passing the Model instance to Presenter to do further business logic. (counting letters)

So my question is in situations like this, when Views are represents a particular Model instance, how does the presenter knows exactly which one of the Model instance is being in interaction?

If i misunderstood something please care to elaborate an explanation to me.

(I have learnt from this site);

Aucun commentaire:

Enregistrer un commentaire