PageRenderTime 31ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/j2ptechnologies/itwtravelgo/Activity/RegisterActivity.java

https://bitbucket.org/itwgoandroid/itw-go-android
Java | 291 lines | 237 code | 53 blank | 1 comment | 14 complexity | b8e33647296804e09714699ea8f4f240 MD5 | raw file
  1. package com.j2ptechnologies.itwtravelgo.Activity;
  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.annotation.RequiresApi;
  8. import android.support.design.widget.FloatingActionButton;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v7.widget.CardView;
  11. import android.transition.Transition;
  12. import android.transition.TransitionInflater;
  13. import android.view.Gravity;
  14. import android.view.View;
  15. import android.view.ViewAnimationUtils;
  16. import android.view.animation.AccelerateInterpolator;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import com.android.volley.Request;
  20. import com.android.volley.Response;
  21. import com.android.volley.VolleyError;
  22. import com.j2ptechnologies.itwtravelgo.R;
  23. import com.j2ptechnologies.itwtravelgo.Utils.CatLoadingView;
  24. import com.j2ptechnologies.itwtravelgo.Utils.CommonMethods;
  25. import com.j2ptechnologies.itwtravelgo.Utils.CommonMethodsFragments;
  26. import com.j2ptechnologies.itwtravelgo.Utils.Constants;
  27. import com.j2ptechnologies.itwtravelgo.Utils.LovelyInfoDialog;
  28. import com.j2ptechnologies.itwtravelgo.api.PostApi;
  29. import com.j2ptechnologies.itwtravelgo.app.App;
  30. import com.j2ptechnologies.itwtravelgo.db.DatabaseHandler;
  31. import com.j2ptechnologies.itwtravelgo.util.Util;
  32. import org.json.JSONException;
  33. import org.json.JSONObject;
  34. import java.util.regex.Pattern;
  35. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  36. public class RegisterActivity extends CommonMethods {
  37. FloatingActionButton fab;
  38. CardView cvAdd;
  39. private final Pattern VALID_EMAIL_ADDRESS_REGEX =
  40. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  41. private EditText firstName, lastName, mobile, email, password;
  42. Button bt_go;
  43. public static String STR_EXTRA_ACTION_REGISTER = "register";
  44. CatLoadingView mView;
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
  49. setContentView(R.layout.activity_register);
  50. Util.getSoftButtonsBarSizePort(RegisterActivity.this);
  51. mView = new CatLoadingView();
  52. fab = (FloatingActionButton) findViewById(R.id.fab);
  53. cvAdd = (CardView) findViewById(R.id.cv_add);
  54. firstName = (EditText) findViewById(R.id.firstName);
  55. lastName = (EditText) findViewById(R.id.lastName);
  56. mobile = (EditText) findViewById(R.id.mobile);
  57. email = (EditText) findViewById(R.id.email);
  58. password = (EditText) findViewById(R.id.password);
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  60. ShowEnterAnimation();
  61. }
  62. fab.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. animateRevealClose();
  66. }
  67. });
  68. bt_go = (Button) findViewById(R.id.bt_go);
  69. bt_go.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View view) {
  72. if (firstName.getText().toString().trim().length() == 0) {
  73. firstName.setError("Please enter First Name");
  74. return;
  75. }
  76. if (lastName.getText().toString().trim().length() == 0) {
  77. lastName.setError("Please enter Last Name");
  78. return;
  79. }
  80. if (mobile.getText().toString().trim().length() < 0) {
  81. mobile.setError("Please enter valid Mobile Number");
  82. return;
  83. }
  84. if (password.getText().toString().trim().length() < 6) {
  85. password.setError("Password lenght must be min 6");
  86. return;
  87. }
  88. if (!validateEmail((email.getText().toString().trim()))) {
  89. email.setError("Please enter valid Email");
  90. return;
  91. }
  92. JSONObject json = new JSONObject();
  93. try {
  94. json.put("firstName", firstName.getText().toString().trim());
  95. json.put("lastName", lastName.getText().toString().trim());
  96. json.put("mobileNumber", mobile.getText().toString().trim());
  97. json.put("email", email.getText().toString().trim());
  98. json.put("password", password.getText().toString().trim());
  99. } catch (JSONException e) {
  100. e.printStackTrace();
  101. }
  102. JSONObject users = new JSONObject();
  103. try {
  104. users.put("users", json);
  105. } catch (JSONException e) {
  106. e.printStackTrace();
  107. }
  108. saveUser(users, email.getText().toString().trim());
  109. }
  110. });
  111. }
  112. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  113. private void ShowEnterAnimation() {
  114. Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
  115. getWindow().setSharedElementEnterTransition(transition);
  116. transition.addListener(new Transition.TransitionListener() {
  117. @Override
  118. public void onTransitionStart(Transition transition) {
  119. cvAdd.setVisibility(View.GONE);
  120. }
  121. @Override
  122. public void onTransitionEnd(Transition transition) {
  123. transition.removeListener(this);
  124. animateRevealShow();
  125. }
  126. @Override
  127. public void onTransitionCancel(Transition transition) {
  128. }
  129. @Override
  130. public void onTransitionPause(Transition transition) {
  131. }
  132. @Override
  133. public void onTransitionResume(Transition transition) {
  134. }
  135. });
  136. }
  137. public void animateRevealShow() {
  138. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, fab.getWidth() / 2, cvAdd.getHeight());
  139. mAnimator.setDuration(500);
  140. mAnimator.setInterpolator(new AccelerateInterpolator());
  141. mAnimator.addListener(new AnimatorListenerAdapter() {
  142. @Override
  143. public void onAnimationEnd(Animator animation) {
  144. super.onAnimationEnd(animation);
  145. }
  146. @Override
  147. public void onAnimationStart(Animator animation) {
  148. cvAdd.setVisibility(View.VISIBLE);
  149. super.onAnimationStart(animation);
  150. }
  151. });
  152. mAnimator.start();
  153. }
  154. public void animateRevealClose() {
  155. Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, cvAdd.getHeight(), fab.getWidth() / 2);
  156. mAnimator.setDuration(500);
  157. mAnimator.setInterpolator(new AccelerateInterpolator());
  158. mAnimator.addListener(new AnimatorListenerAdapter() {
  159. @Override
  160. public void onAnimationEnd(Animator animation) {
  161. cvAdd.setVisibility(View.INVISIBLE);
  162. super.onAnimationEnd(animation);
  163. fab.setImageResource(R.drawable.ic_signup);
  164. RegisterActivity.super.onBackPressed();
  165. }
  166. @Override
  167. public void onAnimationStart(Animator animation) {
  168. super.onAnimationStart(animation);
  169. }
  170. });
  171. mAnimator.start();
  172. }
  173. @Override
  174. public void onBackPressed() {
  175. animateRevealClose();
  176. }
  177. public void saveUser(JSONObject json, final String email) {
  178. String URL = Constants.saveUser;
  179. showDialog();
  180. PostApi api = new PostApi(Request.Method.POST, URL, json, new Response.Listener<JSONObject>() {
  181. @Override
  182. public void onResponse(JSONObject jsonObject) {
  183. dismissDialog();
  184. try {
  185. int code = jsonObject.getInt("code");
  186. if (code == 200) {
  187. DatabaseHandler db = new DatabaseHandler(getBaseContext());
  188. CommonMethodsFragments.addDetails("currentEmail", email, db);
  189. CommonMethodsFragments.addDetails("isRegistered", "true", db);
  190. Intent i = new Intent(getBaseContext(), Dashboard.class);
  191. i.putExtra("from", "register");
  192. startActivity(i);
  193. } else if (code == 500) {
  194. DatabaseHandler db = new DatabaseHandler(getBaseContext());
  195. Intent i = new Intent(getBaseContext(), ResetPassword.class);
  196. i.putExtra("currentEmail", email);
  197. startActivity(i);
  198. } else {
  199. new LovelyInfoDialog(getBaseContext())
  200. .setTopColorRes(R.color.colorAccent)
  201. .setIcon(R.drawable.ic_tab_infor)
  202. //This will add Don't show again checkbox to the dialog. You can pass any ID as argument
  203. .setTitleGravity(Gravity.CENTER)
  204. .setNotShowAgainOptionChecked(false)
  205. .setTitle("Alert!")
  206. .setMessage("This user already exist. Please Register as different user")
  207. .show();
  208. }
  209. } catch (JSONException e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. }, new Response.ErrorListener() {
  214. @Override
  215. public void onErrorResponse(VolleyError volleyError) {
  216. Snackbar snackbar = null;
  217. dismissDialog();
  218. final Snackbar finalSnackbar = snackbar;
  219. snackbar = Snackbar
  220. .make(getWindow().getDecorView(), "Something went wrong", Snackbar.LENGTH_LONG)
  221. .setAction("", new View.OnClickListener() {
  222. @Override
  223. public void onClick(View view) {
  224. }
  225. });
  226. snackbar.show();
  227. }
  228. });
  229. App.getVolleyQueue().add(api);
  230. }
  231. public void showDialog() {
  232. mView.setClickCancelAble(false);
  233. mView.show(getSupportFragmentManager(), "");
  234. }
  235. public void dismissDialog() {
  236. mView.dismiss();
  237. }
  238. }