Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private RestServiceInterface restServiceInterface;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_features_login_login);
- restServiceInterface = RestServiceClass.getClient().create(RestServiceInterface.class);
- }
- /**
- * Attempts to sign in or register the account specified by the login form.
- * If there are form errors (invalid email, missing fields, etc.), the
- * errors are presented and no actual login attempt is made.
- */
- public void attemptLogin(String email, String password) {
- if(!isEmailValid(email)) {
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(getResources().getString(R.string.error_invalid_email)).show();
- } else if(!isPasswordValid(password)) {
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(getResources().getString(R.string.error_invalid_password)).show();
- } else {
- pDialog.show();
- if(RestServiceClass.isNetworkAvailable(LoginActivity.this)){
- login(email, password);
- }else{
- pDialog.dismiss();
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(getResources().getString(R.string.pastikan_internet_label)).show();
- }
- }
- }
- private void login(String email, String password) {
- Call<LoginUser> call1 = restServiceInterface.userLogin(email,password);
- call1.enqueue(new Callback<LoginUser>() {
- @Override
- public void onResponse(Call<LoginUser> call, Response<LoginUser> response) {
- LoginUser result = response.body();
- if(result != null){
- if(result.getStatus().equals("success")){
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(getString(R.string.PREFS_TOKEN), result.getData().getToken());
- editor.putString(getString(R.string.PREFS_USER_ID), result.getData().getUser().get_id());
- editor.putString(getString(R.string.PREFS_PROFILE), new Gson().toJson(result));
- //editor.putString("prefs_user_role", "");
- editor.putString(getString(R.string.PREFS_IS_LOGIN), "1");
- editor.commit();
- pDialog.dismiss();
- startActivity(new Intent(LoginActivity.this, MainActivity.class));
- ActivityCompat.finishAffinity(LoginActivity.this);
- }else if(result.getStatus().equals("error")){
- pDialog.dismiss();
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(result.getMessage()).show();
- }
- } else {
- pDialog.dismiss();
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(result.getMessage()).show();
- }
- }
- @Override
- public void onFailure(Call<LoginUser> call, Throwable t) {
- pDialog.dismiss();
- new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
- .setTitleText("Error")
- .setContentText(getResources().getString(R.string.error_jaringan)).show();
- call.cancel();
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement