Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void showLogInDialog() {
- final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("Log In ");
- dialog.setMessage("Please use email to log 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("Log In", 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",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 is too short",Snackbar.LENGTH_SHORT).show();
- return;
- }
- //Login
- auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
- .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- startActivity(new Intent(MainActivity.this, Welcome.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 dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- dialog.show();
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private void showRgisterDialog() {
- 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 edtFullName = register_layout.findViewById(R.id.edtFullName);
- final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
- final MaterialEditText edtAge = register_layout.findViewById(R.id.edtAge);
- final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
- final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);
- final MaterialEditText edtCity = register_layout.findViewById(R.id.edtCity);
- final MaterialEditText edtStreet = register_layout.findViewById(R.id.edtStreet);
- final MaterialEditText edtHouseNum = register_layout.findViewById(R.id.edtHouseNum);
- final Switch newDriver = register_layout.findViewById(R.id.newDriver);
- newDriver.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // do something, the isChecked will be
- // true if the switch is in the On position
- isNewDriver = "true";
- }
- });
- 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(edtFullName.getText().toString())){
- Snackbar.make(rootLayout,"Please enter first name",Snackbar.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(edtEmail.getText().toString())){
- Snackbar.make(rootLayout,"Please enter email",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 is too short",Snackbar.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(edtPhone.getText().toString())){
- Snackbar.make(rootLayout,"Please enter phone number",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){
- //Save user to db
- User user = new User(edtFullName.getText().toString(),edtEmail.getText().toString(),edtPassword.getText().toString(),edtPhone.getText().toString(),edtAge.getText().toString(),edtCity.getText().toString(),edtStreet.getText().toString(),edtHouseNum.getText().toString(), image, isNewDriver);
- //Use email to key
- users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
- .setValue(user)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Snackbar.make(rootLayout,"Registeration made 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement