PageRenderTime 107ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/wasim/ksapp/ui/RegisterActivity.java

https://bitbucket.org/WasimShahul/ksapp
Java | 159 lines | 131 code | 22 blank | 6 comment | 4 complexity | 33630ccb601dd0daabdcc5a980bf9a5e MD5 | raw file
  1. package com.wasim.ksapp.ui;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.content.Intent;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.support.design.widget.FloatingActionButton;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.CardView;
  10. import android.transition.Transition;
  11. import android.transition.TransitionInflater;
  12. import android.view.View;
  13. import android.view.ViewAnimationUtils;
  14. import android.view.animation.AccelerateInterpolator;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17. import com.wasim.ksapp.R;
  18. import com.wasim.ksapp.data.StaticConfig;
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21. public class RegisterActivity extends AppCompatActivity {
  22. FloatingActionButton fab;
  23. CardView cvAdd;
  24. private final Pattern VALID_EMAIL_ADDRESS_REGEX =
  25. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  26. private EditText editTextUsername, editTextPassword, editTextRepeatPassword;
  27. public static String STR_EXTRA_ACTION_REGISTER = "register";
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_register);
  32. fab = (FloatingActionButton) findViewById(R.id.fab);
  33. cvAdd = (CardView) findViewById(R.id.cv_add);
  34. editTextUsername = (EditText) findViewById(R.id.et_username);
  35. editTextPassword = (EditText) findViewById(R.id.et_password);
  36. editTextRepeatPassword = (EditText) findViewById(R.id.et_repeatpassword);
  37. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  38. ShowEnterAnimation();
  39. }
  40. fab.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. animateRevealClose();
  44. }
  45. });
  46. }
  47. private void ShowEnterAnimation() {
  48. Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
  49. getWindow().setSharedElementEnterTransition(transition);
  50. transition.addListener(new Transition.TransitionListener() {
  51. @Override
  52. public void onTransitionStart(Transition transition) {
  53. cvAdd.setVisibility(View.GONE);
  54. }
  55. @Override
  56. public void onTransitionEnd(Transition transition) {
  57. transition.removeListener(this);
  58. animateRevealShow();
  59. }
  60. @Override
  61. public void onTransitionCancel(Transition transition) {
  62. }
  63. @Override
  64. public void onTransitionPause(Transition transition) {
  65. }
  66. @Override
  67. public void onTransitionResume(Transition transition) {
  68. }
  69. });
  70. }
  71. public void animateRevealShow() {
  72. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth()/2,0, fab.getWidth() / 2, cvAdd.getHeight());
  73. mAnimator.setDuration(500);
  74. mAnimator.setInterpolator(new AccelerateInterpolator());
  75. mAnimator.addListener(new AnimatorListenerAdapter() {
  76. @Override
  77. public void onAnimationEnd(Animator animation) {
  78. super.onAnimationEnd(animation);
  79. }
  80. @Override
  81. public void onAnimationStart(Animator animation) {
  82. cvAdd.setVisibility(View.VISIBLE);
  83. super.onAnimationStart(animation);
  84. }
  85. });
  86. mAnimator.start();
  87. }
  88. public void animateRevealClose() {
  89. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd,cvAdd.getWidth()/2,0, cvAdd.getHeight(), fab.getWidth() / 2);
  90. mAnimator.setDuration(500);
  91. mAnimator.setInterpolator(new AccelerateInterpolator());
  92. mAnimator.addListener(new AnimatorListenerAdapter() {
  93. @Override
  94. public void onAnimationEnd(Animator animation) {
  95. cvAdd.setVisibility(View.INVISIBLE);
  96. super.onAnimationEnd(animation);
  97. fab.setImageResource(R.drawable.ic_signup);
  98. RegisterActivity.super.onBackPressed();
  99. }
  100. @Override
  101. public void onAnimationStart(Animator animation) {
  102. super.onAnimationStart(animation);
  103. }
  104. });
  105. mAnimator.start();
  106. }
  107. @Override
  108. public void onBackPressed() {
  109. animateRevealClose();
  110. }
  111. public void clickRegister(View view) {
  112. Toast.makeText(this, "Register", Toast.LENGTH_SHORT).show();
  113. String username = editTextUsername.getText().toString();
  114. String password = editTextPassword.getText().toString();
  115. String repeatPassword = editTextRepeatPassword.getText().toString();
  116. if(validate(username, password, repeatPassword)){
  117. Intent data = new Intent();
  118. data.putExtra(StaticConfig.STR_EXTRA_USERNAME, username);
  119. data.putExtra(StaticConfig.STR_EXTRA_PASSWORD, password);
  120. data.putExtra(StaticConfig.STR_EXTRA_ACTION, STR_EXTRA_ACTION_REGISTER);
  121. setResult(RESULT_OK, data);
  122. finish();
  123. }else {
  124. Toast.makeText(this, "Invalid email or not match password", Toast.LENGTH_SHORT).show();
  125. }
  126. }
  127. /**
  128. * Validate email, pass == re_pass
  129. * @param emailStr
  130. * @param password
  131. * @return
  132. */
  133. private boolean validate(String emailStr, String password, String repeatPassword) {
  134. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
  135. return password.length() > 0 && repeatPassword.equals(password) && matcher.find();
  136. }
  137. }