Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. package com.example.ronel.finalapp;
  2.  
  3. import android.content.DialogInterface;
  4. import android.os.Build;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AlertDialog;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.RadioButton;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.gms.tasks.OnCompleteListener;
  18. import com.google.android.gms.tasks.Task;
  19. import com.google.firebase.auth.AuthResult;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.google.firebase.auth.FirebaseUser;
  22. import com.google.firebase.database.DatabaseReference;
  23. import com.google.firebase.database.FirebaseDatabase;
  24.  
  25. public class Register extends AppCompatActivity {
  26.  
  27.     RadioButton rb1,rb2;
  28.     EditText etName,etMail,etPass,etPhone;
  29.     Button btn;
  30.     String name,mail,pass,phone;
  31.     Boolean sex;
  32.     private FirebaseAuth mAuth;
  33.     DatabaseReference myRef;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_register);
  39.         rb1 = findViewById(R.id.rb1);
  40.         rb2 = findViewById(R.id.rb2);
  41.  
  42.         btn = findViewById(R.id.btn);
  43.  
  44.         etName = findViewById(R.id.etName);
  45.         etMail = findViewById(R.id.etMail);
  46.         etPass = findViewById(R.id.etPass);
  47.         etPhone = findViewById(R.id.etPhone);
  48.  
  49.         mAuth = FirebaseAuth.getInstance();
  50.     }
  51.  
  52.     public void function(View view) {
  53.         //Works good - no bugs
  54.         name = etName.getText().toString();
  55.         mail = etMail.getText().toString();
  56.         pass = etPass.getText().toString();
  57.         phone = etPhone.getText().toString();
  58.  
  59.         if (TextUtils.isEmpty(name)) {
  60.             Toast.makeText(this, "Enter name", Toast.LENGTH_SHORT).show();
  61.             return;
  62.         }
  63.         if (name.length() < 2) {
  64.             Toast.makeText(this, "Name to short", Toast.LENGTH_SHORT).show();
  65.             return;
  66.         }
  67.         if (TextUtils.isEmpty(mail)) {
  68.             Toast.makeText(this, "Enter mail", Toast.LENGTH_SHORT).show();
  69.             return;
  70.         }
  71.         if (TextUtils.isEmpty(pass)) {
  72.             Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
  73.             return;
  74.         }
  75.         if (TextUtils.isEmpty(phone)) {
  76.             Toast.makeText(this, "Enter phone", Toast.LENGTH_SHORT).show();
  77.             return;
  78.         }
  79.         if (phone.length() != 10) {
  80.             Toast.makeText(this, "Phone number needs to be 10 digits", Toast.LENGTH_SHORT).show();
  81.             return;
  82.         }
  83.         if ((!rb1.isChecked()) && (!rb2.isChecked())) {
  84.             Toast.makeText(this, "Choose gender", Toast.LENGTH_SHORT).show();
  85.             return;
  86.         }
  87.         if (rb1.isChecked()) {
  88.             sex = true;
  89.         }
  90.         else {
  91.             sex = false;
  92.         }
  93.         Log.d(name,"name is:");
  94.         Log.d(mail,"mail is:");
  95.         Log.d(pass,"password is:");
  96.         Log.d(phone,"phone is:");
  97.         Log.d(String.valueOf(sex),"gender is:");
  98.         //End of first section
  99.        
  100.         //Alert dialog
  101.         AlertDialog.Builder builder;
  102.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  103.             builder = new AlertDialog.Builder(Register.this, android.R.style.Theme_Material_Dialog_Alert);
  104.         } else {
  105.             builder = new AlertDialog.Builder(Register.this);
  106.         }
  107.         builder.setTitle("Send data")
  108.                 .setMessage("Are you sure you want to send data?" + "Email: " + mail + " Password: " + pass)
  109.                 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
  110.                     public void onClick(DialogInterface dialog, int which) {
  111.                         // continue with delete
  112.                     }
  113.                 })
  114.                 .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
  115.                     public void onClick(DialogInterface dialog, int which) {
  116.                         // do nothing
  117.                     }
  118.                 })
  119.                 .setIcon(android.R.drawable.ic_dialog_alert)
  120.                 .show();
  121.  
  122.         //Send to DataBase
  123.         //Works fine - no bugs
  124.         mAuth.createUserWithEmailAndPassword(mail, pass)
  125.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  126.                     @Override
  127.                     public void onComplete(@NonNull Task<AuthResult> task) {
  128.                         if (task.isSuccessful()) {
  129.                             // Sign in success
  130.                             Log.d("success", "createUserWithEmail:success");
  131.                             FirebaseUser user = mAuth.getInstance().getCurrentUser();
  132.                             Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_SHORT).show();
  133.                             String uid = user.getUid();
  134.                             Log.d("uid",uid);
  135.  
  136.                             User user1=new User(name,uid,sex,phone,0);
  137.                             Log.d("user1.tostring()",user1.toString());
  138.                             //send info
  139.                             myRef =FirebaseDatabase.getInstance().getReference("users");
  140.                             myRef.child(uid).setValue(user1);
  141.                         } else {
  142.                             // If sign in fails, display a message to the user.
  143.                             Log.w("fail", "createUserWithEmail:failure", task.getException());
  144.                             Toast.makeText(Register.this, "Authentication failed", Toast.LENGTH_SHORT).show();
  145.                         }
  146.                     }
  147.                 });
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement