Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.jeff.eldoauto;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.support.annotation.NonNull;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AlertDialog;
- 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 com.example.jeff.eldoauto.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;
- public class MainActivity extends AppCompatActivity {
- Button btnSignUp,btnSignIn;
- RelativeLayout rootLayout;
- FirebaseAuth auth;
- FirebaseDatabase db;
- DatabaseReference users;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //init Firebase
- auth = FirebaseAuth.getInstance();
- db = FirebaseDatabase.getInstance();
- users = db.getReference("Users");
- //init View
- btnSignIn = (Button) findViewById(R.id.btnSignIn);
- btnSignUp = (Button) findViewById(R.id.btnSignUp);
- rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
- //call buttons
- btnSignIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showSignInDialog();
- }
- });
- btnSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showSignUpDialog();
- }
- });
- }
- private void showSignInDialog() {
- 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 signin_layout = inflater.inflate(R.layout.layout_signin,null);
- final MaterialEditText edtEmail = signin_layout.findViewById(R.id.edtEmail);
- final MaterialEditText edtPassword = signin_layout.findViewById(R.id.edtPassword);
- dialog.setView(signin_layout);
- //set button
- dialog.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- //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() < 6) {
- Snackbar.make(rootLayout, "Password too short!!", Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- //sign in
- auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()
- ).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- startActivity(new Intent(MainActivity.this,RiderMapActivity.class));
- finish();
- }
- }).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 dialog, int which) {
- }
- });
- dialog.show();
- }
- private void showSignUpDialog() {
- final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("Sign Up");
- dialog.setMessage("please use Email to Sign Up");
- LayoutInflater inflater = LayoutInflater.from(this);
- View signup_layout = inflater.inflate(R.layout.layout_signup,null);
- final MaterialEditText edtEmail = signup_layout.findViewById(R.id.edtEmail);
- final MaterialEditText edtPassword = signup_layout.findViewById(R.id.edtPassword);
- final MaterialEditText edtName = signup_layout.findViewById(R.id.edtName);
- final MaterialEditText edtPhone = signup_layout.findViewById(R.id.edtPhone);
- dialog.setView(signup_layout);
- //set button
- dialog.setPositiveButton("Sign Up", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.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() <6)
- {
- Snackbar.make(rootLayout,"Password too short!!",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- //Register new user
- auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()
- ).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- startActivity(new Intent(MainActivity.this,RiderMapActivity.class));
- finish();
- //save user to db
- User user = new User();
- user.setEmail(edtEmail.getText().toString());
- user.setPassword(edtPassword.getText().toString());
- user.setName(edtName.getText().toString());
- user.setPhone(edtPhone.getText().toString());
- //use email key
- users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
- .setValue(user)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Snackbar.make(rootLayout,"Sign Up successful",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 dialog, int which) {
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- }
Add Comment
Please, Sign In to add comment