I'm learning MVP for Android and am wondering if that piece of code that I wrote is correct in terms of this pattern or maybe is completely messed up.
I'm wondering if there can be return type other than void in contract interface like I have in Model interface, secondly context shouldn't be passed in constructor of presenter, then how should the model instance be created in presenter's constructor other way? This is my code:
public interface CredentialsContract {
interface Model {
interface OnFinishedLoginListener {
void onFinishedLogin(String body);
void onFailureLogin(Throwable t);
}
void saveSomeCredentials(String nameLogin, String passLogin);
void saveEmptyCredentials();
String getNameCredential();
String getPassCredential();
boolean getCbCredential();
void checkLogin(OnFinishedLoginListener onFinishedListener, String login, String pass);
}
interface View {
void showBigProgressDialog();
void hideBigProgressDialog();
void setPropertiesWhenLoginIsTrue();
void setPropertiesWhenLoginIsFalse();
void setLoginTextViewText(String response);
void setNameCredential(String name);
void setPassCredential(String pass);
void setCbCredential(boolean cbState);
}
interface Presenter {
void saveSomeCredentials(String nameLogin, String passLogin);
void saveEmptyCredentials();
void checkLoginPresenter(String login, String pass);
void setNameCredential();
void setPassCredential();
void setCbCredential();
}
}
public class RestData implements CredentialsContract.Model {
private Context context;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
public RestData(Context context) {
this.context = context;
pref = context.getSharedPreferences("Preferences", 0);
editor = pref.edit();
}
@Override
public void saveSomeCredentials(String nameLogin, String passLogin) {
editor.putString("userLogin", nameLogin); // Storing string
editor.putString("userPassword", passLogin); // Storing string
editor.putBoolean("cbSaveCredentials", true);
editor.apply();
}
@Override
public void saveEmptyCredentials() {
editor.putString("userLogin", ""); // Storing string
editor.putString("userPassword", ""); // Storing string
editor.putBoolean("cbSaveCredentials", false);
editor.apply();
}
@Override
public void checkLogin(final OnFinishedLoginListener onFinishedListener, String login, String passWhenLogin) {
Call<String> result = Api.getClient().checkLogin(login, passWhenLogin);
result.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
onFinishedListener.onFinished(response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("tag", t.toString());
onFinishedListener.onFailure(t);
}
});
}
@Override
public String getNameCredential() {
return pref.getString("userLogin", "");
}
@Override
public String getPassCredential() {
return pref.getString("userPassword", "");
}
@Override
public boolean getCbCredential() {
return pref.getBoolean("cbSaveCredentials", false);
}
}
public class Credentials extends AppCompatActivity implements CredentialsContract.View {
private EditText etNameLogin, etPassLogin;
private CheckBox cbSaveCredentials, cbTerms;
private TextView tvLoginStatus, tvRegistrationStatus;
private ProgressBar pbBig;
private CredentialsPresenter credentialsPresenter;
private Button bOk;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.credentials_layout);
credentialsPresenter = new CredentialsPresenter(this, this);
initUIFields(pref);
credentialsPresenter.setNameCredential();
credentialsPresenter.setPassCredential();
credentialsPresenter.setCbCredential();
findViewById(R.id.tvTerms).setOnClickListener(v -> initRegulationsDialog());
bOk = findViewById(R.id.bOk);
bOk.setOnClickListener(v -> {
if (cbSaveCredentials.isChecked())
credentialsPresenter.saveSomeCredentials(etNameLogin.getText().toString(), etPassLogin.getText().toString());
else
credentialsPresenter.saveEmptyCredentials();
Intent i = new Intent(getApplicationContext(), Gra.class);
i.putExtra("userLogin", etNameLogin.getText().toString());
i.putExtra("onlineGame", true);
startActivity(i);
});
findViewById(R.id.bLogin).setOnClickListener(v -> credentialsPresenter.checkLoginPresenter(etNameLogin.getText().toString(), etPassLogin.getText().toString()));
}
public void initUIFields(SharedPreferences pref) {
etNameLogin = findViewById(R.id.etNameLogin);
etPassLogin = findViewById(R.id.etPassLogin);
cbSaveCredentials = findViewById(R.id.cbSaveCredentials);
etNameRegistration = findViewById(R.id.etName);
etEmailRegistration = findViewById(R.id.etEmailRegistration);
etPassRegistration = findViewById(R.id.etPassRegistration);
etPassConfirmationRegistration = findViewById(R.id.etPassConfirmationRegistration);
cbTerms = findViewById(R.id.cbTerms);
tvLoginStatus = findViewById(R.id.tvLoginStatus);
tvRegistrationStatus = findViewById(R.id.tvRegistrationStatus);
pbBig = findViewById(R.id.bigProgressBar);
}
public void initRegulationsDialog() {
AlertDialog.Builder dialogRegulations = new AlertDialog.Builder(Credentials.this);
dialogRegulations.setTitle("Terms and conditions");
dialogRegulations.setMessage("Content");
dialogRegulations.setCancelable(false);
dialogRegulations.setNeutralButton("OK", (dialog1, which) -> dialog1.dismiss());
dialogRegulations.show();
}
@Override
public void showBigProgressDialog() {
pbBig.setVisibility(View.VISIBLE);
}
@Override
public void hideBigProgressDialog() {
pbBig.setVisibility(View.INVISIBLE);
}
@Override
public void setPropertiesWhenLoginIsTrue() {
bOk.setEnabled(true);
tvLoginStatus.setTextColor(ContextCompat.getColor(this, R.color.green));
}
@Override
public void setPropertiesWhenLoginIsFalse() {
bOk.setEnabled(false);
tvLoginStatus.setTextColor(ContextCompat.getColor(this, R.color.red));
}
@Override
public void setLoginTextViewText(String response) {
tvLoginStatus.setText(response);
}
@Override
public void setNameCredential(String name) {
etNameLogin.setText(name);
}
@Override
public void setPassCredential(String pass) {
etPassLogin.setText(pass);
}
@Override
public void setCbCredential(boolean cbState) {
cbSaveCredentials.setChecked(cbState);
}
}
public class CredentialsPresenter implements CredentialsContract.Presenter, CredentialsContract.Model.OnFinishedLoginListener {
CredentialsContract.Model model;
CredentialsContract.View view;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
public CredentialsPresenter(Context context, CredentialsContract.View view) {
model = new RestData(context);
this.view = view;
this.sharedPreferences = sharedPreferences;
editor = sharedPreferences.edit();
}
@Override
public void saveSomeCredentials(String nameLogin, String passLogin) {
editor.putString("userLogin", nameLogin);
editor.putString("userPassword", passLogin);
editor.putBoolean("cbSaveCredentials", true);
editor.apply();
}
@Override
public void saveEmptyCredentials() {
editor.putString("userLogin", "");
editor.putString("userPassword", "");
editor.putBoolean("cbSaveCredentials", false);
editor.apply();
}
@Override
public void checkLoginPresenter(String login, String passWhenLogin) {
view.showBigProgressDialog();
model.checkLogin(this, login, passWhenLogin);
}
@Override
public void onFinishedLogin(String body) {
view.setLoginTextViewText(body);
if(body.contains("true"))
view.setPropertiesWhenLoginIsTrue();
if(body.contains("false"))
view.setPropertiesWhenLoginIsFalse();
view.hideBigProgressDialog();
}
@Override
public void onFailureLogin(Throwable t) {
view.hideBigProgressDialog();
}
@Override
public void setNameCredential() {
view.setNameCredential(model.getNameCredential());
}
@Override
public void setPassCredential() {
view.setPassCredential(model.getPassCredential());
}
@Override
public void setCbCredential() {
view.setCbCredential(model.getCbCredential());
}
}