Guest User

Untitled

a guest
Feb 26th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package com.anuragdhunna.www.sessionmangementsharedpref.activities;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import com.anuragdhunna.www.sessionmangementsharedpref.utils.LoginServices;
  13. import com.anuragdhunna.www.sessionmangementsharedpref.R;
  14. import com.anuragdhunna.www.sessionmangementsharedpref.utils.RetrofitClient;
  15. import com.anuragdhunna.www.sessionmangementsharedpref.utils.SaveSharedPreference;
  16.  
  17. import retrofit2.Call;
  18. import retrofit2.Callback;
  19. import retrofit2.Response;
  20. import retrofit2.Retrofit;
  21.  
  22. import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
  23.  
  24. /**
  25. * @author anuragdhunna
  26. */
  27. public class LoginActivity extends AppCompatActivity {
  28.  
  29. EditText username,password;
  30. Button submitBtn;
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_login);
  36.  
  37. username = findViewById(R.id.usernameText);
  38. password = findViewById(R.id.passwordText);
  39. submitBtn = findViewById(R.id.submit);
  40.  
  41. // Check if UserResponse is Already Logged In
  42. if(SaveSharedPreference.getLoggedStatus(getApplicationContext())) {
  43. Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
  44. startActivity(intent);
  45. } else {
  46. submitBtn.setOnClickListener(new View.OnClickListener() {
  47. public void onClick(View v) {
  48.  
  49. userLogin(username.getText().toString(), password.getText().toString());
  50. }
  51. });
  52. }
  53. }
  54.  
  55. /**
  56. * Login API call
  57. * TODO: Please modify according to your need it is just an example
  58. * @param username
  59. * @param password
  60. */
  61. private void userLogin(String username, String password) {
  62.  
  63. Retrofit retrofit = RetrofitClient.getClient();
  64. final LoginServices loginServices = retrofit.create(LoginServices.class);
  65. Call<Void> call = loginServices.userLogin(username, password);
  66.  
  67. call.enqueue(new Callback<Void>() {
  68. @Override
  69. public void onResponse(Call<Void> call, Response<Void> response) {
  70.  
  71. if (response.isSuccessful()) {
  72. // Set Logged In statue to 'true'
  73. SaveSharedPreference.setLoggedIn(getApplicationContext(), true);
  74. Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
  75. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK |FLAG_ACTIVITY_CLEAR_TASK);
  76. startActivity(intent);
  77. } else {
  78. Toast.makeText(getApplicationContext(), "Credentials are not Valid.",
  79. Toast.LENGTH_SHORT).show();
  80. }
  81. }
  82.  
  83. @Override
  84. public void onFailure(Call<Void> call, Throwable t) {
  85. Log.e("TAG", "=======onFailure: " + t.toString());
  86. t.printStackTrace();
  87. // Log error here since request failed
  88. }
  89. });
  90. }
  91. }
Add Comment
Please, Sign In to add comment