Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3.  
  4. private static final String TAG = "LoginActivity";
  5. // La respuesta del JSON es
  6. private static final String username = "success";
  7. private static final String TAG_MESSAGE = "message";
  8.  
  9. private Button btnLogin, btnLinkToSignUp;
  10. private ProgressBar progressBar;
  11. private FirebaseAuth auth;
  12. private EditText loginInputEmail, loginInputPassword;
  13. private TextInputLayout loginInputLayoutEmail, loginInputLayoutPassword;
  14.  
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_login);
  20. auth = FirebaseAuth.getInstance();
  21.  
  22. loginInputLayoutEmail = (TextInputLayout) findViewById(R.id.login_input_layout_email);
  23. loginInputLayoutPassword = (TextInputLayout) findViewById(R.id.login_input_layout_password);
  24. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  25.  
  26. loginInputEmail = (EditText) findViewById(R.id.login_input_email);
  27. loginInputPassword = (EditText) findViewById(R.id.login_input_password);
  28.  
  29. btnLogin = (Button) findViewById(R.id.btn_login);
  30. btnLinkToSignUp = (Button) findViewById(R.id.btn_link_signup);
  31.  
  32. btnLogin.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View view) {
  35. submitForm();
  36. }
  37. });
  38.  
  39. btnLinkToSignUp.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View view) {
  42. Intent intent = new Intent(appz.seoallinone.LoginActivity.this, SignupActivity.class);
  43. startActivity(intent);
  44. }
  45. });
  46. }
  47.  
  48. /**
  49. * Validating form
  50. */
  51. private void submitForm() {
  52. String email = loginInputEmail.getText().toString().trim();
  53. String password = loginInputPassword.getText().toString().trim();
  54.  
  55. if(!checkEmail()) {
  56. return;
  57. }
  58. if(!checkPassword()) {
  59. return;
  60. }
  61. loginInputLayoutEmail.setErrorEnabled(false);
  62. loginInputLayoutPassword.setErrorEnabled(false);
  63.  
  64. progressBar.setVisibility(View.VISIBLE);
  65. //authenticate user
  66. auth.signInWithEmailAndPassword(email, password)
  67. .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
  68. @Override
  69. public void onComplete(@NonNull Task<AuthResult> task) {
  70. // If sign in fails, Log a message to the LogCat. If sign in succeeds
  71. // the auth state listener will be notified and logic to handle the
  72. // signed in user can be handled in the listener.
  73. progressBar.setVisibility(View.GONE);
  74. if (!task.isSuccessful()) {
  75. // there was an error
  76. Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
  77.  
  78. } else {
  79. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  80. startActivity(intent);
  81. finish();
  82. }
  83. }
  84. });
  85. }
  86.  
  87. private boolean checkEmail() {
  88. String email = loginInputEmail.getText().toString().trim();
  89. if (email.isEmpty() || !isEmailValid(email)) {
  90.  
  91. loginInputLayoutEmail.setErrorEnabled(true);
  92. loginInputLayoutEmail.setError(getString(R.string.err_msg_email));
  93. loginInputEmail.setError(getString(R.string.err_msg_required));
  94. requestFocus(loginInputEmail);
  95. return false;
  96. }
  97. loginInputLayoutEmail.setErrorEnabled(false);
  98. return true;
  99. }
  100.  
  101. private boolean checkPassword() {
  102.  
  103. String password = loginInputPassword.getText().toString().trim();
  104. if (password.isEmpty() || !isPasswordValid(password)) {
  105.  
  106. loginInputLayoutPassword.setError(getString(R.string.err_msg_password));
  107. loginInputPassword.setError(getString(R.string.err_msg_required));
  108. requestFocus(loginInputPassword);
  109. return false;
  110. }
  111. loginInputLayoutPassword.setErrorEnabled(false);
  112. return true;
  113. }
  114.  
  115. private static boolean isEmailValid(String email) {
  116. return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
  117. }
  118.  
  119. private static boolean isPasswordValid(String password){
  120. return (password.length() >= 6);
  121. }
  122.  
  123. private void requestFocus(View view) {
  124. if (view.requestFocus()) {
  125. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  126. }
  127. }
  128.  
  129. @Override
  130. protected void onResume() {
  131. super.onResume();
  132. progressBar.setVisibility(View.GONE);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement