package com.example.garella.apps; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.support.annotation.NonNull; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.app.AlertDialog; import com.example.garella.apps.Common.Common; import com.example.garella.apps.Model.User; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.rengwuxian.materialedittext.MaterialEditText; import dmax.dialog.SpotsDialog; import uk.co.chrisjenx.calligraphy.CalligraphyConfig; import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; public class MainActivity extends AppCompatActivity { Button btnRegister,btnSignIn; RelativeLayout rootLayout; FirebaseAuth auth; FirebaseDatabase db; DatabaseReference users; private User user; @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext (CalligraphyContextWrapper.wrap (newBase)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); CalligraphyConfig.initDefault (new CalligraphyConfig.Builder() .setDefaultFontPath ("fonts/Arkhip_font_0.ttf") .setFontAttrId (R.attr.fontPath) .build ()); setContentView (R.layout.activity_main); //Init Firebase auth = FirebaseAuth.getInstance (); db = FirebaseDatabase.getInstance (); users = db.getReference ("Users"); //init View btnRegister = (Button)findViewById (R.id.btnRegister); btnSignIn = (Button)findViewById (R.id.btnSignIn); rootLayout = (RelativeLayout)findViewById (R.id.rootLayout); //Event btnRegister.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { showRegisterDialog(); } }); btnSignIn.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { showLoginDialog(); } }); } private void showLoginDialog() { final AlertDialog.Builder dialog = new AlertDialog.Builder (this); dialog.setTitle ("SIGN IN"); dialog.setMessage ("Please use email to sign in"); LayoutInflater inflater = LayoutInflater.from (this); View login_layout = inflater.inflate (R.layout.layout_login,null); final MaterialEditText edtEmail = login_layout.findViewById (R.id.edtEmail); final MaterialEditText edtPassword = login_layout.findViewById (R.id.edtPassword); dialog.setView(login_layout); //set button dialog.setPositiveButton ("SIGN IN", new DialogInterface.OnClickListener () { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss (); //set disable button Sign In if is processing btnSignIn.setEnabled (false); //Check Validation if (TextUtils.isEmpty (edtEmail.getText ().toString ())) { Snackbar.make (rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT) .show (); return; } if (TextUtils.isEmpty (edtPassword.getText ().toString ())) { Snackbar.make (rootLayout, "Please enter password", Snackbar.LENGTH_SHORT) .show (); return; } if (edtPassword.getText ().toString ().length () < 8) { Snackbar.make (rootLayout, "Password to short !", Snackbar.LENGTH_SHORT) .show (); return; } final AlertDialog waitingDialog = new SpotsDialog (MainActivity.this); waitingDialog.show (); //Login auth.signInWithEmailAndPassword (edtEmail.getText ().toString (),edtPassword.getText ().toString ()) .addOnSuccessListener (new OnSuccessListener () { @Override public void onSuccess(AuthResult authResult) { waitingDialog.dismiss (); Common.currentUser = user; startActivity (new Intent (MainActivity.this,Home.class)); finish (); } }).addOnFailureListener (new OnFailureListener () { @Override public void onFailure(@NonNull Exception e) { waitingDialog.dismiss (); Snackbar.make (rootLayout,"Failed"+e.getMessage (),Snackbar.LENGTH_SHORT) .show (); //Active button btnSignIn.setEnabled (true); } }); } }); dialog.setNegativeButton ("CANCEL", new DialogInterface.OnClickListener () { @Override public void onClick(DialogInterface dialogInterface, int which) { dialogInterface.dismiss (); } }); dialog.show (); } private void showRegisterDialog() { final AlertDialog.Builder dialog = new AlertDialog.Builder (this); dialog.setTitle ("REGISTER"); dialog.setMessage ("Please use email to register"); LayoutInflater inflater = LayoutInflater.from (this); View register_layout = inflater.inflate (R.layout.layout_register,null); final MaterialEditText edtEmail = register_layout.findViewById (R.id.edtEmail); final MaterialEditText edtPassword = register_layout.findViewById (R.id.edtPassword); final MaterialEditText edtName = register_layout.findViewById (R.id.edtName); final MaterialEditText edtPhone = register_layout.findViewById (R.id.edtPhone); dialog.setView(register_layout); //set button dialog.setPositiveButton ("REGISTER", new DialogInterface.OnClickListener () { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss (); //Check Validation if(TextUtils.isEmpty (edtEmail.getText ().toString ())) { Snackbar.make (rootLayout,"Please enter email address",Snackbar.LENGTH_SHORT) .show (); return; } if(TextUtils.isEmpty (edtPhone.getText ().toString ())) { Snackbar.make (rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT) .show (); return; } if(TextUtils.isEmpty (edtPassword.getText ().toString ())) { Snackbar.make (rootLayout,"Please enter password",Snackbar.LENGTH_SHORT) .show (); return; } if(edtPassword.getText ().toString ().length () < 8) { Snackbar.make (rootLayout,"Password to short !",Snackbar.LENGTH_SHORT) .show (); return; } //Register new user auth.createUserWithEmailAndPassword (edtEmail.getText ().toString (),edtPassword.getText ().toString ()) .addOnSuccessListener (new OnSuccessListener () { @Override public void onSuccess(AuthResult authResult) { //Save User to db User user = new User (); user.setEmail (edtEmail.getText ().toString ()); user.setName (edtName.getText ().toString ()); user.setPhone (edtPhone.getText ().toString ()); user.setPassword (edtPassword.getText ().toString ()); //Use email to key users.child (FirebaseAuth.getInstance ().getCurrentUser ().getUid ()) .setValue (user) .addOnSuccessListener (new OnSuccessListener () { @Override public void onSuccess(Void aVoid) { Snackbar.make (rootLayout,"Register Successfully !",Snackbar.LENGTH_SHORT) .show (); } }) .addOnFailureListener (new OnFailureListener () { @Override public void onFailure(@NonNull Exception e) { Snackbar.make (rootLayout,"Failed "+e.getMessage (),Snackbar.LENGTH_SHORT) .show (); } }); } }) .addOnFailureListener (new OnFailureListener () { @Override public void onFailure(@NonNull Exception e) { Snackbar.make (rootLayout,"Failed "+e.getMessage (),Snackbar.LENGTH_SHORT) .show (); } }); } }); dialog.setNegativeButton ("CANCEL", new DialogInterface.OnClickListener () { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss (); } }); dialog.show (); } }