I'm new to android and this is the first time trying to use MVP pattern in my code ... as I understand so far is that my view should talk to the presenter and the presenter to the model > then the presenter will talk again to view.. I wish I'm right with that !!! as shown below in my simple code example I'm trying to return a result value from the model to the presenter and then I will use this result value in the presenter to decide which method should I call in the view..I have 2 questions and I hope some helps. 1) The enqueue method is working async and the result value will always return empty or fail or whatever.. because it works alone... and when I try to use the execute method instead I'm facing a NetworkOnMainThreadException error... so how can I make the right way ? 2) Is this a right way in using MVP pattern ?
This is the SignupContract class
public class SignupContract {
public interface IView{
void signupSuccess();
void signupFailed(String message);
}
public interface IPresenter{
void signup(UserProfile userProfile);
}
public interface IModel{
String signup(UserProfile userProfile);
}
}
This is the View code..
public class SignupActivity extends AppCompatActivity implements SignupContract.IView {
//some code
@Override
protected void onCreate(Bundle savedInstanceState) {
//some code
createAccountBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//some code
presenter.signup(userProfile);
}
});
}
@Override
public void signupSuccess() {
/*AppUtils.dismissLoadingDialog(SignupActivity.this,"","");*/
Intent intent = new Intent(SignupActivity.this, SigninActivity.class);
startActivity(intent);
finish();
}
@Override
public void signupFailed(String message) {
/*AppUtils.dismissLoadingDialog(SignupActivity.this,"","");*/
AppUtils.showErrorMessage(SignupActivity.this, message);
}
and this is the presenter
public class SignupPresenter implements SignupContract.IPresenter {
SignupContract.IView view;
SignupContract.IModel model;
public SignupPresenter(SignupContract.IView view){
model = new SignupModel();
this.view = view;
}
@Override
public void signup(UserProfile userProfile) {
userProfile = UserProfileCleaner.clean(userProfile, "signup");
UserProfileDTO dto = new UserProfileDTO();
String validationMessage = dto.validateUserProfile(userProfile, "signup");
if(validationMessage != null && !validationMessage.equals("")){
view.signupFailed(validationMessage);
}else{
String signupResult = model.signup(userProfile);
if(signupResult.equals("success")){
view.signupSuccess();
}else {
view.signupFailed(signupResult);
}
}
}
}
and this is the model class
public class SignupModel implements SignupContract.IModel {
private String TAG = "SignupModel";
private String result = "";
@Override
public String signup(UserProfile userProfile) {
final Context context = DKApp.getContext();
ServiceWrapper serviceWrapper = new ServiceWrapper(null);
Call<SignupResponse> userSignUpCall = serviceWrapper.userSignUpCall(userProfile.getUser().getUsername(),
userProfile.getUser().getPassword(),userProfile.getPhoneNumber(), userProfile.getEmailAddress(),
userProfile.getFullName());
userSignUpCall.enqueue(new Callback<SignupResponse>() {
@Override
public void onResponse(Call<SignupResponse> call, Response<SignupResponse> response) {
if( response.body() != null && response.isSuccessful() ){
Log.e(TAG,response.body().toString());
if(response.body().getStatus() == 1){
//some code
result = "success";
}else{
result = response.body().getMessage();
}
}else{
result = context.getResources().getString(R.string.request_failed);
}
}
@Override
public void onFailure(Call<SignupResponse> call, Throwable t) {
Log.e(TAG, "Failure : " + t.toString());
result = context.getResources().getString(R.string.request_failed);
}
});
return result;
}
}