Advertisement
Guest User

asd

a guest
Apr 15th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. protected void onCreate(Bundle savedInstanceState) { bla bla balbal
  2.                     // set button in singUp mode
  3.                     mBtnSingup.setOnClickListener(new View.OnClickListener() {
  4.                         @Override
  5.                         public void onClick(View view) {
  6.                             Log.d("user", "signUp");
  7.                             if (!validateForm()) {
  8.                                 return;
  9.                             }
  10.                             //getString from editText
  11.                             String email = mEdEmail.getText().toString();
  12.                             String password = mEdPass.getText().toString();
  13.                             //Progress singUp
  14.                             mProgressDialog.show();
  15.                             mProgressDialog.setMessage("Please wait...");
  16.                             mAuth.createUserWithEmailAndPassword(email, password)
  17.                                     .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  18.                                         @Override
  19.                                         public void onComplete(@NonNull Task<AuthResult> task) {
  20.                                             Log.d("user", "createUser:onComplete:" +
  21.                                                     task.isSuccessful());
  22.  
  23.                                             mProgressDialog.dismiss();
  24.                                             if (task.isSuccessful()) {
  25.                                                 onAuthSuccess(task.getResult().getUser());
  26.                                             } else {
  27.                                                 // there was an error
  28.                                                 if (password.length() < 6) {
  29.                                                     mEdPass.setError(getString(R.string.min_pwd));
  30.                                                 } else {
  31.                                                     Toast.makeText(SingActivity.this,
  32.                                                             "Oops! ada masalah dengan email anda," +
  33.                                                                     " coba periksa kembali!!",
  34.                                                             Toast.LENGTH_LONG).show();
  35.                                                 }
  36.  
  37.                                             }
  38.                                         }
  39.                                     });
  40.                         }
  41.                     });
  42.  
  43.  
  44.  
  45.  
  46.                 }
  47.  
  48.  
  49.     @Override
  50.     public void onStart() {
  51.         super.onStart();
  52.         mAuth.addAuthStateListener(mAuthStateListener);
  53.     }
  54.  
  55.     @Override
  56.     public void onStop() {
  57.         super.onStop();
  58.         if (mAuthStateListener != null) {
  59.             mAuth.removeAuthStateListener(mAuthStateListener);
  60.         }
  61.     }
  62.  
  63.       //Get username from email kalau mau dipake
  64.     private String usernameFromEmail(String email) {
  65.         if (email.contains("@")) {
  66.             return email.split("@")[0];
  67.         } else {
  68.             return email;
  69.         }
  70.     }
  71.  
  72.  
  73.     private boolean validateForm() {
  74.         bla bla bla
  75.  
  76.         return result;
  77.     }
  78.  
  79.  
  80.  
  81.     //If singUp was success email from database converted to username from this method and save it
  82.     //on WriteNewUser task
  83.     private void onAuthSuccess(FirebaseUser user) {
  84.         String username = usernameFromEmail(user.getEmail());
  85.         String nama = mEdNama.getText.toString();
  86.         String telepon = mEdTelepon.getText.toString();
  87.         String alamat = mEdAlamat.getText.toString();
  88.         // Write new user
  89.         writeNewUser(user.getUid(), username, user.getEmail(), nama, telepon, alamat);
  90.  
  91.         // Go to MainActivity
  92.         startActivity(new Intent(SingActivity.this, MainActivity.class));
  93.         finish();
  94.     }
  95.  
  96.  
  97.     // [START basic_write on Firebase Database]
  98.     private void writeNewUser(String userId, String username, String email, String nama, String telepon, String alamat) {
  99.         User user = new User(username, email, nama, telepon, alamat);
  100.  
  101.         mDatabase.child("users").child(userId).setValue(user);
  102.     }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. ///// class lain - model
  116.  
  117.  
  118.     package id.my.asmith.myapplication;
  119.  
  120. import com.google.firebase.database.IgnoreExtraProperties;
  121.  
  122. // [START blog_user_class]
  123. @IgnoreExtraProperties
  124. public class User {
  125.  
  126.     public String username;
  127.     public String email;
  128.     public String alamat;
  129.     public String telepon;
  130.     public String nama;
  131.  
  132.  
  133.     public User() {
  134.         // Default constructor required for calls to DataSnapshot.getValue(User.class)
  135.     }
  136.  
  137.     public User(String username, String email, String nama, String alamat, String telepon) {
  138.         this.username = username;
  139.         this.email = email;
  140.         this.nama = nama;
  141.         this.alamat = alamat;
  142.         this.telepon = telepon;
  143.  
  144.     }
  145.  
  146. }
  147. // [END blog_user_class]
  148. ///// class lain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement