Uczę się właśnie MVP dla Androida. Zastanawiam się czy to co napłodziłem jest poprawne w kontekście tego wzorca czy jeszcze coś można udoskonalić, a może jest całkowicie do bani:
public interface CredentialsContract {
interface Model {
interface OnFinishedLoginListener {
void onFinishedLogin(String body);
void onFailureLogin(Throwable t);
}
void checkLogin(OnFinishedLoginListener onFinishedListener, String login, String pass);
}
interface View {
void showBigProgressDialog();
void hideBigProgressDialog();
void setPropertiesWhenLoginIsTrue();
void setPropertiesWhenLoginIsFalse();
void setLoginTextViewText(String response);
}
interface Presenter {
void saveSomeCredentials(String nameLogin, String passLogin);
void saveEmptyCredentials();
void checkLoginPresenter(String login, String pass);
}
}
public class RestData implements CredentialsContract.Model {
@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);
}
});
}
}
public class CredentialsPresenter implements CredentialsContract.Presenter, CredentialsContract.Model.OnFinishedLoginListener {
CredentialsContract.Model model;
CredentialsContract.View view;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
public CredentialsPresenter(CredentialsContract.View view, SharedPreferences sharedPreferences) {
model = new RestData();
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();
}
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);
SharedPreferences pref = getApplicationContext().getSharedPreferences("Preferences", 0); // 0 - for private mode
credentialsPresenter = new CredentialsPresenter(this, pref);
initUIFields(pref);
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);
etNameLogin.setText(pref.getString("userLogin", ""));
etPassLogin = findViewById(R.id.etPassLogin);
etPassLogin.setText(pref.getString("userPassword", ""));
cbSaveCredentials = findViewById(R.id.cbSaveCredentials);
cbSaveCredentials.setChecked(pref.getBoolean("cbSaveCredentials", false));
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);
}
}