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();
}
}
}