Facebook
From Eratic Pheasant, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 300
  1. package com.example.garella.apps;
  2.  
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.support.annotation.NonNull;
  7. import android.support.design.widget.Snackbar;
  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. import android.app.AlertDialog;
  16.  
  17. import com.example.garella.apps.Common.Common;
  18. import com.example.garella.apps.Model.User;
  19. import com.google.android.gms.tasks.OnFailureListener;
  20. import com.google.android.gms.tasks.OnSuccessListener;
  21. import com.google.firebase.auth.AuthResult;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.rengwuxian.materialedittext.MaterialEditText;
  26.  
  27. import dmax.dialog.SpotsDialog;
  28. import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
  29. import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
  30.  
  31. public class MainActivity extends AppCompatActivity {
  32.  
  33.     Button btnRegister,btnSignIn;
  34.     RelativeLayout rootLayout;
  35.  
  36.     FirebaseAuth auth;
  37.     FirebaseDatabase db;
  38.     DatabaseReference users;
  39.     private User user;
  40.  
  41.     @Override
  42.     protected void attachBaseContext(Context newBase) {
  43.         super.attachBaseContext (CalligraphyContextWrapper.wrap (newBase));
  44.     }
  45.  
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.         super.onCreate (savedInstanceState);
  49.  
  50.         CalligraphyConfig.initDefault (new CalligraphyConfig.Builder()
  51.                                             .setDefaultFontPath ("fonts/Arkhip_font_0.ttf")
  52.                                             .setFontAttrId (R.attr.fontPath)
  53.                                             .build ());
  54.         setContentView (R.layout.activity_main);
  55.  
  56.         //Init Firebase
  57.         auth = FirebaseAuth.getInstance ();
  58.         db = FirebaseDatabase.getInstance ();
  59.         users = db.getReference ("Users");
  60.  
  61.         //init View
  62.         btnRegister = (Button)findViewById (R.id.btnRegister);
  63.         btnSignIn = (Button)findViewById (R.id.btnSignIn);
  64.         rootLayout = (RelativeLayout)findViewById (R.id.rootLayout);
  65.  
  66.         //Event
  67.         btnRegister.setOnClickListener (new View.OnClickListener () {
  68.             @Override
  69.             public void onClick(View v) {
  70.                 showRegisterDialog();
  71.             }
  72.         });
  73.  
  74.         btnSignIn.setOnClickListener (new View.OnClickListener () {
  75.             @Override
  76.             public void onClick(View v) {
  77.                 showLoginDialog();
  78.  
  79.             }
  80.         });
  81.  
  82.     }
  83.  
  84.     private void showLoginDialog() {
  85.         final AlertDialog.Builder dialog = new AlertDialog.Builder (this);
  86.         dialog.setTitle ("SIGN IN");
  87.         dialog.setMessage ("Please use email to sign in");
  88.  
  89.         LayoutInflater inflater = LayoutInflater.from (this);
  90.         View login_layout = inflater.inflate (R.layout.layout_login,null);
  91.  
  92.         final MaterialEditText edtEmail = login_layout.findViewById (R.id.edtEmail);
  93.         final MaterialEditText edtPassword = login_layout.findViewById (R.id.edtPassword);
  94.  
  95.         dialog.setView(login_layout);
  96.  
  97.         //set button
  98.         dialog.setPositiveButton ("SIGN IN", new DialogInterface.OnClickListener () {
  99.                     @Override
  100.                     public void onClick(DialogInterface dialogInterface, int i) {
  101.                         dialogInterface.dismiss ();
  102.  
  103.                         //set disable button Sign In if is processing
  104.  
  105.                         btnSignIn.setEnabled (false);
  106.  
  107.  
  108.                         //Check Validation
  109.                         if (TextUtils.isEmpty (edtEmail.getText ().toString ())) {
  110.                             Snackbar.make (rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT)
  111.                                     .show ();
  112.                             return;
  113.                         }
  114.  
  115.                         if (TextUtils.isEmpty (edtPassword.getText ().toString ())) {
  116.                             Snackbar.make (rootLayout, "Please enter password", Snackbar.LENGTH_SHORT)
  117.                                     .show ();
  118.                             return;
  119.                         }
  120.  
  121.                         if (edtPassword.getText ().toString ().length () < 8) {
  122.                             Snackbar.make (rootLayout, "Password to short !", Snackbar.LENGTH_SHORT)
  123.                                     .show ();
  124.                             return;
  125.                         }
  126.  
  127.                         final AlertDialog waitingDialog =  new SpotsDialog (MainActivity.this);
  128.                         waitingDialog.show ();
  129.  
  130.                         //Login
  131.                         auth.signInWithEmailAndPassword (edtEmail.getText ().toString (),edtPassword.getText ().toString ())
  132.                                 .addOnSuccessListener (new OnSuccessListener<AuthResult> () {
  133.                                     @Override
  134.                                     public void onSuccess(AuthResult authResult) {
  135.                                         waitingDialog.dismiss ();
  136.                                         Common.currentUser = user;
  137.                                         startActivity (new Intent (MainActivity.this,Home.class));
  138.                                         finish ();
  139.                                     }
  140.                                 }).addOnFailureListener (new OnFailureListener () {
  141.                             @Override
  142.                             public void onFailure(@NonNull Exception e) {
  143.                                 waitingDialog.dismiss ();
  144.                                 Snackbar.make (rootLayout,"Failed"+e.getMessage (),Snackbar.LENGTH_SHORT)
  145.                                         .show ();
  146.  
  147.                                 //Active button
  148.                                 btnSignIn.setEnabled (true);
  149.                             }
  150.                         });
  151.                     }
  152.                 });
  153.         dialog.setNegativeButton ("CANCEL", new DialogInterface.OnClickListener () {
  154.             @Override
  155.             public void onClick(DialogInterface dialogInterface, int which) {
  156.                 dialogInterface.dismiss ();
  157.             }
  158.         });
  159.  
  160.  
  161.  
  162.  
  163.         dialog.show ();
  164.     }
  165.  
  166.     private void showRegisterDialog() {
  167.         final AlertDialog.Builder dialog = new AlertDialog.Builder (this);
  168.         dialog.setTitle ("REGISTER");
  169.         dialog.setMessage ("Please use email to register");
  170.  
  171.         LayoutInflater inflater = LayoutInflater.from (this);
  172.         View register_layout = inflater.inflate (R.layout.layout_register,null);
  173.  
  174.         final MaterialEditText edtEmail = register_layout.findViewById (R.id.edtEmail);
  175.         final MaterialEditText edtPassword = register_layout.findViewById (R.id.edtPassword);
  176.         final MaterialEditText edtName = register_layout.findViewById (R.id.edtName);
  177.         final MaterialEditText edtPhone = register_layout.findViewById (R.id.edtPhone);
  178.  
  179.         dialog.setView(register_layout);
  180.  
  181.         //set button
  182.         dialog.setPositiveButton ("REGISTER", new DialogInterface.OnClickListener () {
  183.             @Override
  184.             public void onClick(DialogInterface dialogInterface, int i) {
  185.                 dialogInterface.dismiss ();
  186.  
  187.                 //Check Validation
  188.                 if(TextUtils.isEmpty (edtEmail.getText ().toString ()))
  189.                 {
  190.                     Snackbar.make (rootLayout,"Please enter email address",Snackbar.LENGTH_SHORT)
  191.                             .show ();
  192.                     return;
  193.                 }
  194.  
  195.                 if(TextUtils.isEmpty (edtPhone.getText ().toString ()))
  196.                 {
  197.                     Snackbar.make (rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT)
  198.                             .show ();
  199.                     return;
  200.                 }
  201.  
  202.                 if(TextUtils.isEmpty (edtPassword.getText ().toString ()))
  203.                 {
  204.                     Snackbar.make (rootLayout,"Please enter password",Snackbar.LENGTH_SHORT)
  205.                             .show ();
  206.                     return;
  207.                 }
  208.  
  209.                 if(edtPassword.getText ().toString ().length () < 8)
  210.                 {
  211.                     Snackbar.make (rootLayout,"Password to short !",Snackbar.LENGTH_SHORT)
  212.                             .show ();
  213.                     return;
  214.                 }
  215.  
  216.                 //Register new user
  217.                 auth.createUserWithEmailAndPassword (edtEmail.getText ().toString (),edtPassword.getText ().toString ())
  218.                         .addOnSuccessListener (new OnSuccessListener<AuthResult> () {
  219.                             @Override
  220.                             public void onSuccess(AuthResult authResult) {
  221.                                 //Save User to db
  222.                                 User user = new User ();
  223.                                 user.setEmail (edtEmail.getText ().toString ());
  224.                                 user.setName (edtName.getText ().toString ());
  225.                                 user.setPhone (edtPhone.getText ().toString ());
  226.                                 user.setPassword (edtPassword.getText ().toString ());
  227.  
  228.                                 //Use email to key
  229.                                 users.child (FirebaseAuth.getInstance ().getCurrentUser ().getUid ())
  230.                                         .setValue (user)
  231.                                         .addOnSuccessListener (new OnSuccessListener<Void> () {
  232.                                             @Override
  233.                                             public void onSuccess(Void aVoid) {
  234.                                                 Snackbar.make (rootLayout,"Register Successfully !",Snackbar.LENGTH_SHORT)
  235.                                                         .show ();
  236.                                             }
  237.                                         })
  238.                                 .addOnFailureListener (new OnFailureListener () {
  239.                                     @Override
  240.                                     public void onFailure(@NonNull Exception e) {
  241.  
  242.                                         Snackbar.make (rootLayout,"Failed "+e.getMessage (),Snackbar.LENGTH_SHORT)
  243.                                                 .show ();
  244.  
  245.                                     }
  246.                                 });
  247.  
  248.  
  249.                             }
  250.                         })
  251.                 .addOnFailureListener (new OnFailureListener () {
  252.                     @Override
  253.                     public void onFailure(@NonNull Exception e) {
  254.                         Snackbar.make (rootLayout,"Failed "+e.getMessage (),Snackbar.LENGTH_SHORT)
  255.                                 .show ();
  256.                     }
  257.                 });
  258.             }
  259.         });
  260.  
  261.         dialog.setNegativeButton ("CANCEL", new DialogInterface.OnClickListener () {
  262.             @Override
  263.             public void onClick(DialogInterface dialogInterface, int i) {
  264.                 dialogInterface.dismiss ();
  265.  
  266.             }
  267.         });
  268.  
  269.         dialog.show ();
  270.  
  271.  
  272.     }
  273. }