use of de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ChangePasswordBinding in project RSAndroidApp by RailwayStations.
the class MyDataActivity method changePassword.
public void changePassword(final View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
final ChangePasswordBinding passwordBinding = ChangePasswordBinding.inflate(getLayoutInflater());
builder.setTitle(R.string.bt_change_password).setView(passwordBinding.getRoot()).setIcon(R.mipmap.ic_launcher).setPositiveButton(android.R.string.ok, null).setNegativeButton(android.R.string.cancel, (dialog, id) -> dialog.cancel());
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
String newPassword = getValidPassword(passwordBinding.password, passwordBinding.passwordRepeat);
if (newPassword == null) {
return;
}
alertDialog.dismiss();
try {
newPassword = URLEncoder.encode(newPassword, String.valueOf(StandardCharsets.UTF_8));
} catch (final UnsupportedEncodingException e) {
Log.e(TAG, "Error encoding new password", e);
}
rsapiClient.changePassword(newPassword).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<Void> call, @NonNull final Response<Void> response) {
switch(response.code()) {
case 200:
Log.i(TAG, "Successfully changed password");
binding.myData.etPassword.setText(passwordBinding.password.getText());
baseApplication.setPassword(passwordBinding.password.getText().toString());
rsapiClient.setCredentials(profile.getEmail(), passwordBinding.password.getText().toString());
new SimpleDialogs().confirm(MyDataActivity.this, R.string.password_changed);
break;
case 401:
rsapiClient.clearCredentials();
new SimpleDialogs().confirm(MyDataActivity.this, R.string.authorization_failed);
break;
default:
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.change_password_failed).toString(), response.code()));
}
}
@Override
public void onFailure(@NonNull final Call<Void> call, @NonNull final Throwable t) {
Log.e(TAG, "Error changing password", t);
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.change_password_failed).toString(), t.getMessage()));
}
});
});
}
Aggregations