Guest User

Untitled

a guest
Jan 28th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. package com.example.jeff.eldoauto;
  2.  
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.support.annotation.NonNull;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AlertDialog;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.text.TextUtils;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.RelativeLayout;
  15.  
  16. import com.example.jeff.eldoauto.Model.User;
  17. import com.google.android.gms.tasks.OnFailureListener;
  18. import com.google.android.gms.tasks.OnSuccessListener;
  19. import com.google.firebase.auth.AuthResult;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.google.firebase.database.DatabaseReference;
  22. import com.google.firebase.database.FirebaseDatabase;
  23. import com.rengwuxian.materialedittext.MaterialEditText;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.  
  27. Button btnSignUp,btnSignIn;
  28. RelativeLayout rootLayout;
  29.  
  30. FirebaseAuth auth;
  31. FirebaseDatabase db;
  32. DatabaseReference users;
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38.  
  39. //init Firebase
  40. auth = FirebaseAuth.getInstance();
  41. db = FirebaseDatabase.getInstance();
  42. users = db.getReference("Users");
  43.  
  44. //init View
  45. btnSignIn = (Button) findViewById(R.id.btnSignIn);
  46. btnSignUp = (Button) findViewById(R.id.btnSignUp);
  47. rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
  48.  
  49. //call buttons
  50. btnSignIn.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v) {
  53. showSignInDialog();
  54.  
  55. }
  56. });
  57.  
  58. btnSignUp.setOnClickListener(new View.OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. showSignUpDialog();
  62.  
  63.  
  64. }
  65. });
  66.  
  67.  
  68. }
  69.  
  70.  
  71.  
  72. private void showSignInDialog() {
  73. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  74. dialog.setTitle("Sign In");
  75. dialog.setMessage("please use Email to Sign In");
  76.  
  77. LayoutInflater inflater = LayoutInflater.from(this);
  78. View signin_layout = inflater.inflate(R.layout.layout_signin,null);
  79.  
  80. final MaterialEditText edtEmail = signin_layout.findViewById(R.id.edtEmail);
  81. final MaterialEditText edtPassword = signin_layout.findViewById(R.id.edtPassword);
  82.  
  83. dialog.setView(signin_layout);
  84.  
  85. //set button
  86. dialog.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
  87. @Override
  88. public void onClick(DialogInterface dialog, int which) {
  89. dialog.dismiss();
  90.  
  91.  
  92. //check validation
  93. if (TextUtils.isEmpty(edtEmail.getText().toString())) {
  94. Snackbar.make(rootLayout, "Please enter Email address", Snackbar.LENGTH_SHORT)
  95. .show();
  96. return;
  97. }
  98.  
  99. if (TextUtils.isEmpty(edtPassword.getText().toString())) {
  100. Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT)
  101. .show();
  102. return;
  103. }
  104. if (edtPassword.getText().toString().length() < 6) {
  105. Snackbar.make(rootLayout, "Password too short!!", Snackbar.LENGTH_SHORT)
  106. .show();
  107. return;
  108. }
  109.  
  110.  
  111. //sign in
  112. auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()
  113. ).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  114. @Override
  115. public void onSuccess(AuthResult authResult) {
  116. startActivity(new Intent(MainActivity.this,RiderMapActivity.class));
  117. finish();
  118. }
  119. }).addOnFailureListener(new OnFailureListener() {
  120. @Override
  121. public void onFailure(@NonNull Exception e) {
  122. Snackbar.make(rootLayout,"failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
  123. .show();
  124. }
  125. });
  126.  
  127. }
  128.  
  129. });
  130.  
  131.  
  132. dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  133. @Override
  134. public void onClick(DialogInterface dialog, int which) {
  135.  
  136. }
  137. });
  138.  
  139.  
  140. dialog.show();
  141.  
  142.  
  143. }
  144.  
  145. private void showSignUpDialog() {
  146. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  147. dialog.setTitle("Sign Up");
  148. dialog.setMessage("please use Email to Sign Up");
  149.  
  150. LayoutInflater inflater = LayoutInflater.from(this);
  151. View signup_layout = inflater.inflate(R.layout.layout_signup,null);
  152.  
  153. final MaterialEditText edtEmail = signup_layout.findViewById(R.id.edtEmail);
  154. final MaterialEditText edtPassword = signup_layout.findViewById(R.id.edtPassword);
  155. final MaterialEditText edtName = signup_layout.findViewById(R.id.edtName);
  156. final MaterialEditText edtPhone = signup_layout.findViewById(R.id.edtPhone);
  157.  
  158. dialog.setView(signup_layout);
  159.  
  160. //set button
  161. dialog.setPositiveButton("Sign Up", new DialogInterface.OnClickListener() {
  162. @Override
  163. public void onClick(DialogInterface dialog, int which) {
  164. dialog.dismiss();
  165.  
  166.  
  167. //check validation
  168. if (TextUtils.isEmpty(edtEmail.getText().toString()))
  169. {
  170. Snackbar.make(rootLayout,"Please enter Email address",Snackbar.LENGTH_SHORT)
  171. .show();
  172. return;
  173. }
  174. if (TextUtils.isEmpty(edtPhone.getText().toString()))
  175. {
  176. Snackbar.make(rootLayout,"Please enter Phone number",Snackbar.LENGTH_SHORT)
  177. .show();
  178. return;
  179. }
  180. if (TextUtils.isEmpty(edtPassword.getText().toString()))
  181. {
  182. Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_SHORT)
  183. .show();
  184. return;
  185. }
  186. if (edtPassword.getText().toString().length() <6)
  187. {
  188. Snackbar.make(rootLayout,"Password too short!!",Snackbar.LENGTH_SHORT)
  189. .show();
  190. return;
  191. }
  192.  
  193. //Register new user
  194. auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()
  195. ).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  196. @Override
  197. public void onSuccess(AuthResult authResult) {
  198. startActivity(new Intent(MainActivity.this,RiderMapActivity.class));
  199. finish();
  200.  
  201.  
  202. //save user to db
  203. User user = new User();
  204. user.setEmail(edtEmail.getText().toString());
  205. user.setPassword(edtPassword.getText().toString());
  206. user.setName(edtName.getText().toString());
  207. user.setPhone(edtPhone.getText().toString());
  208.  
  209. //use email key
  210. users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
  211. .setValue(user)
  212. .addOnSuccessListener(new OnSuccessListener<Void>() {
  213. @Override
  214. public void onSuccess(Void aVoid) {
  215. Snackbar.make(rootLayout,"Sign Up successful",Snackbar.LENGTH_SHORT)
  216. .show();
  217.  
  218. }
  219. })
  220. .addOnFailureListener(new OnFailureListener() {
  221. @Override
  222. public void onFailure(@NonNull Exception e) {
  223. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
  224. .show();
  225.  
  226.  
  227. }
  228. });
  229.  
  230. }
  231. })
  232. .addOnFailureListener(new OnFailureListener() {
  233. @Override
  234. public void onFailure(@NonNull Exception e) {
  235. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
  236. .show();
  237.  
  238. }
  239. });
  240.  
  241.  
  242.  
  243. }
  244. });
  245.  
  246. dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  247. @Override
  248. public void onClick(DialogInterface dialog, int which) {
  249. dialog.dismiss();
  250.  
  251. }
  252. });
  253. dialog.show();
  254.  
  255. }
  256. }
Add Comment
Please, Sign In to add comment