Advertisement
faresaa

Untitled

Feb 28th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1.  
  2. SharedPrefManager sharedPrefManager;
  3. TextInputLayout etEmail;
  4. TextInputLayout etPassword;
  5. Button btnLogin;
  6. ProgressDialog loading;
  7.  
  8. Context mContext = this;
  9. Service mApiService;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_login);
  14. mContext = this;
  15. mApiService = Client.getClient().create(Service.class);
  16.  
  17. initComponents();
  18.  
  19. sharedPrefManager = new SharedPrefManager(this);
  20. if (sharedPrefManager.getSPSudahLogin()){
  21. startActivity(new Intent(LoginActivity.this, MainActivity.class)
  22. .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
  23. finish();
  24. }
  25. }
  26. private void initComponents(){
  27. etEmail = findViewById(R.id.etEmail);
  28. etPassword = findViewById(R.id.etPassword);
  29. btnLogin = findViewById(R.id.btnLogin);
  30.  
  31.  
  32. btnLogin.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. if (!validateEmail() | !validatePassword()) {
  36. return;
  37. }
  38. loading = ProgressDialog.show(mContext, null, "Harap Tunggu...", true, false);
  39. requestLogin();
  40.  
  41.  
  42. }
  43. });
  44.  
  45. }
  46. private boolean validateEmail() {
  47. String email = (etEmail.getEditText()).getText().toString().trim();
  48.  
  49. if (email.isEmpty()) {
  50. etEmail.setError("Email tidak boleh kosong");
  51. return false;
  52. } else {
  53. etEmail.setError(null);
  54. return true;
  55. }
  56. }
  57.  
  58. private boolean validatePassword() {
  59. String pw = etPassword.getEditText().getText().toString().trim();
  60.  
  61. if (pw.isEmpty()) {
  62. etPassword.setError("Password tidak boleh kosong");
  63. return false;
  64. } else {
  65. etPassword.setError(null);
  66. return true;
  67. }
  68. }
  69.  
  70. private void requestLogin(){
  71. mApiService.loginRequest(etEmail.getEditText().getText().toString(), etPassword.getEditText().getText().toString())
  72. .enqueue(new Callback<TokenResponse>()
  73. {
  74.  
  75. @Override
  76. public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) {
  77. if (response.isSuccessful()){
  78. loading.dismiss();
  79. TokenResponse tokenResponse = response.body();
  80. Log.d("tes", tokenResponse.getToken());
  81.  
  82. sharedPrefManager.saveToken(SharedPrefManager.SP_TOKEN,tokenResponse.getToken());
  83. sharedPrefManager.getSpToken();
  84. Log.d("token", sharedPrefManager.getSpToken());
  85.  
  86.  
  87. sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
  88. Intent intent = new Intent(LoginActivity.this,MainActivity.class);
  89. startActivity(intent);
  90. } else {
  91. AlertDialog.Builder dlgAlert = new AlertDialog.Builder(mContext);
  92.  
  93. dlgAlert.setMessage("Mohon Periksa Kembali");
  94. dlgAlert.setTitle("Email Atau Password Anda Salah");
  95. dlgAlert.setPositiveButton("OK", null);
  96. dlgAlert.setCancelable(true);
  97. dlgAlert.create().show();
  98.  
  99.  
  100. dlgAlert.setPositiveButton("Ok",
  101. new DialogInterface.OnClickListener() {
  102. public void onClick(DialogInterface dialog, int which) {
  103.  
  104. }
  105. });
  106. loading.dismiss();
  107. }
  108. }
  109.  
  110. @Override
  111. public void onFailure(Call<TokenResponse> call, Throwable t) {
  112. Log.e("debug", "onFailure: ERROR > " + t.toString());
  113. loading.dismiss();
  114. }
  115. });
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement