Best way to access activity components - MVP

0

I'm 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 starts when you click 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 understood the concept of MVP well, this type of direct access to the patient object is not valid.

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

I tried to access it like this:

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

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

    
asked by anonymous 02.02.2017 / 00:49

1 answer

2

Within the MVP (% with_%) pattern, the construction of the object Model-View-Presenter must be done within its Paciente and its Presenter (be it View or Activity ) is only responsible for < in> for your Fragment .

First, you need to create a contract ( Presenter ) between your interface and Presenter :

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 "confusing" part now comes: Your View needs to access this information in some way. You have several options to do this, but taking advantage of the Java we can have an instance of an interface , we need an instance of our Presenter interface with our PacienteView , so we can construct the object Presenter :

public class PacientePresenter {

    private PacienteView view;

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

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

Now, within our Paciente , we need to loop the View (instantiated above) to our view . To do that, it's pretty quiet. It's something like Presenter that you should be accustomed 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! This way your setOnClickListener(this) will have the Presenter instance and you can add whatever you need in the methods. Now, to save your View object, it 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();
    }
}
    
02.02.2017 / 12:17