Guest User

Untitled

a guest
Dec 28th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. package id.co.rumahcoding.neardeal;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10.  
  11. import butterknife.BindView;
  12. import butterknife.ButterKnife;
  13. import butterknife.OnClick;
  14. import id.co.rumahcoding.neardeal.responses.LoginResponse;
  15. import id.co.rumahcoding.neardeal.utils.PopupUtil;
  16. import retrofit2.Call;
  17. import retrofit2.Callback;
  18. import retrofit2.Response;
  19.  
  20. public class LoginActivity extends AppCompatActivity {
  21. @BindView(R.id.et_email)
  22. EditText usernameEditText;
  23.  
  24. @BindView(R.id.et_password)
  25. EditText passwordEditText;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_login);
  31. ButterKnife.bind(this);
  32.  
  33. SharedPreferences sharedPref = getSharedPreferences("id.co.rumahcoding.neardeal",
  34. Context.MODE_PRIVATE);
  35.  
  36. boolean isLoggedIn = sharedPref.getBoolean("isLoggedin", false);
  37.  
  38. if(isLoggedIn) {
  39. Intent intent = new Intent(this, MainActivity.class);
  40. startActivity(intent);
  41. finish();
  42. }
  43.  
  44.  
  45. /*SharedPreferences.Editor editor = sharedPref.edit();
  46. editor.putInt(getKey(mKey), state);
  47. editor.commit();*/
  48. }
  49.  
  50. @OnClick({R.id.btn_login})
  51. public void onClick(Button button) {
  52. PopupUtil.showLoading( this, "", "Please wait....");
  53. String username = usernameEditText.getText().toString();
  54. String password = passwordEditText.getText().toString();
  55.  
  56. ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
  57. Call<LoginResponse> call = apiEndPoint.login(username, password);
  58.  
  59. call.enqueue(new Callback<LoginResponse>() {
  60. @Override
  61. public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
  62. PopupUtil.dismissDialog();
  63. final LoginResponse loginResponse = response.body();
  64.  
  65. if (loginResponse.getSuccess()){
  66. SharedPreferences sharedPref = getSharedPreferences("id.co.rumahcoding.neardeal",
  67. Context.MODE_PRIVATE);
  68.  
  69. SharedPreferences.Editor editor = sharedPref.edit();
  70. editor.putBoolean("isLoggedin", true);
  71. editor.commit();
  72.  
  73. runOnUiThread(new Runnable() {
  74. @Override
  75. public void run() {
  76. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  77. startActivity(intent);
  78. finish();
  79. }
  80. });
  81. }
  82. else {
  83. PopupUtil.showMsg(LoginActivity.this, "User dan password salah", PopupUtil.SHORT);
  84. }
  85. }
  86.  
  87. @Override
  88. public void onFailure(Call<LoginResponse> call, Throwable t) {
  89. PopupUtil.dismissDialog();
  90. t.printStackTrace();
  91. }
  92. });
  93. }
  94. }
Add Comment
Please, Sign In to add comment