Best way to access Activity components - MVP

Asked

Viewed 119 times

0

I am using the MVP standard in my Android project.

In my mainactivity I have the following components:

private Paciente paciente;
private EditText nome, cpf, cpfTutor, telefone;
private CheckBox checkbox;

soon after I have this method that is started when clicking on a button:

private void initPaciente() {
    paciente = new Paciente();

    paciente.setNome(nome.getText().toString());
    paciente.setCpfTitular(cpf.getText().toString());
    paciente.setCpfTutor(cpfTutor.getText().toString());
    paciente.setTelefone(telefone.getText().toString());
    paciente.setDataCadastro("27/01/2017");
    paciente.setStatus(status.getSelectedItem().toString().toLowerCase());
    paciente.setTimestamp(new Timestamp(System.currentTimeMillis()).getTime());

    paciente.save;
}

Well, if I mean well the concept of MVP this type of direct access to the patient object is not valid.

How do I access these components EditText nome, cpf, cpfTutor, telefone; by the Presenter?

I tried to access it like this:

private PacienteActivity activity = (PacienteActivity) getContext();
Paciente paciente = new Paciente();
paciente.setNome(activity.nome.getText().toString());
// ...

But that way I have to leave the attributes as public (which I don’t think is cool), or create a lot of geters that will pollute my code.

  • i studied on Thiengo’s blog: http://www.thiengo.com.br/mvp-android, and has a video on YT: https://www.youtube.com/watch?v=WRGqmqZwEcQ, also on it, which explains better how to use these concepts.

  • If you’re concerned about following patterns and "good practices," review the fact that Patient is responsible for recording himself.

  • Yes, in the above example I had before following the standards. Today the object is responsible for only controlling your data, who persists now is the Model. But anyway, thank you for the tip.

1 answer

2

Within the MVP standard (Model-View-Presenter), the construction of the object Paciente should be made within your Presenter and its View (be she Activity or Fragment) is responsible only for cede the information for his Presenter.

First, you need to create a contract (interface) among his Presenter and View:

public interface PacienteView {
    String getNome();
    String getCpfTitular(); 
    ...
}

And implement it within your View:

public class PacienteFragment implements PacienteView {

    @Override
    void getNome() {
        return nome.getText().toString();
    }

    @Override
    void getCpfTitular() {
        return cpf.getText().toString();
    }

    ...
}

The "confused" part comes now: your Presenter needs to access this information in some way. You have several options to do this, but taking advantage of Java we can have an instance of an interface, we need an instance our interface PacienteView with our Presenter, so we can build the object Paciente:

public class PacientePresenter {

    private PacienteView view;

    public void configuraPaciente() {
        Paciente paciente = new Paciente ();

        paciente.setNome(view.getNome());
        paciente.setCpfTitular(view.getCpfTitular());
        ...
    }
}

Now, inside of our View, we need to attach the intância view (above) to our Presenter. To do this, it’s very quiet. It’s something like a setOnClickListener(this) that you must be used to using:

public class PacientePresenter {

    private PacienteView view;

    public void setView(PacienteView view) {
        this.view = view;
    }
}

...

public class PacienteFragment implements PacienteView {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        presenter = new SubscriptionsPresenter();

        presenter.setView(this);
    }
}

Ready! That way your Presenter will have the instance of View and you can add whatever is needed in the methods. Now, to save your object Paciente, would look something like:

public class PacienteFragment implements PacienteView {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        presenter = new SubscriptionsPresenter();

        presenter.setView(this);
    }

    ...

    private void initPaciente() {
        presenter.configuraPaciente();
    }
}
  • And when I am receiving data from a server, for example? It is the presenter that should make the request?

  • @Mr_anderson O Presenter is responsible for providing the information for the View. In that case, the Presenter must work together with other Patterns (such as Client or Repository) to carry out these requisitions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.