Search in sources :

Example 1 with ChangePasswordBinding

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()));
            }
        });
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ChangePasswordBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ChangePasswordBinding) ContextThemeWrapper(android.view.ContextThemeWrapper) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs)

Aggregations

ContextThemeWrapper (android.view.ContextThemeWrapper)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 ChangePasswordBinding (de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ChangePasswordBinding)1 SimpleDialogs (de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1