0

I am new to Android and trying to learn MVP pattern. I have wrote below sample tutorial which fetches username and password from EditText and see if it matches "Don" and "Bradman". If yes it will show Toast message accordingly. My Question is

"Is it okay to get values from Edit Text in Activity and do comparison there? I do not think so because then business logic comes in Activity which is not the correct thing to do. If YES then how can I get those edit text values in Presenter class and match there and send message to Activity to show Toast. Below is my code, if someone can help":

MainInterface.java

public interface MainInterface {

    //Declare all Activity related methods here
    interface View{
        void checkLoginSuccessful();
    }

    //Declare all Model related methods here
    interface Model{
        String getUsername();
        String setUsername(String username);
        String getPassword();
        String setPassword(String password);
    }

    //Declare all Presenter related methods here -- All Business Logic comes here
    interface Presenter {
        void onClickLogin();
    }
}

PresenterClass.java

public class PresenterClass implements MainInterface.Presenter{

    //Now since presenter is going to interact with both classes Model and View so let's invoke both "DataModel" class and "MainActivity" class
    private MainInterface.View mview;


    public PresenterClass(MainInterface.View view) {
        this.mview = view;
    }


    @Override
    public void onClickLogin() {
        mview.checkLoginSuccessful();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements MainInterface.View {

    private EditText username;
    private EditText password;
    private Button loginbutton;

    PresenterClass presenter;

    //DataModel data;

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

        presenter = new PresenterClass(this);

        username = (EditText) findViewById(R.id.editusername);
        password = (EditText) findViewById(R.id.editpassword);
        loginbutton = (Button) findViewById(R.id.loginbutton);
        loginbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                presenter.onClickLogin();
            }
        });
    }

    @Override
    public void checkLoginSuccessful() {
        if (username.getText().toString().equals("Don") && password.getText().toString().equals("Bradman")){
            Toast.makeText(MainActivity.this, "You have successfully logged in", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Golmaal
  • 13
  • 5

1 Answers1

0

You need to add two methods to the view that the presenter calls to get the username and password, once obtained you do the operation you need and call the method to display the toast.

It doesn't make sense that you have an interface with the model, the model is an object of your domain, for example, user, and user has the username and password fields, then you would view the user's data and set them in the object, for example, to save it in the database.

I hope it helped you.

Daniel RL
  • 353
  • 1
  • 12