diff --git a/Foodify/app/build.gradle b/Foodify/app/build.gradle index c71c818..0f1bba3 100644 --- a/Foodify/app/build.gradle +++ b/Foodify/app/build.gradle @@ -43,13 +43,14 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' implementation 'androidx.navigation:navigation-fragment:2.3.5' implementation 'androidx.navigation:navigation-ui:2.3.5' + implementation 'com.google.firebase:firebase-auth:11.8.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' //Add library - implementation 'com.google.firebase:firebase-core:10.2.0' - implementation 'com.google.firebase:firebase-database:10.2.0' + implementation 'com.google.firebase:firebase-core:11.8.0' + implementation 'com.google.firebase:firebase-database:11.8.0' implementation 'com.github.jd-alexander:android-flat-button:v1.1' implementation 'com.rengwuxian.materialedittext:library:2.1.4' diff --git a/Foodify/app/src/main/java/com/waterbase/foodify/SignUp.java b/Foodify/app/src/main/java/com/waterbase/foodify/SignUp.java index b413a0c..262dafe 100644 --- a/Foodify/app/src/main/java/com/waterbase/foodify/SignUp.java +++ b/Foodify/app/src/main/java/com/waterbase/foodify/SignUp.java @@ -4,14 +4,20 @@ import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; +import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.PhoneAuthProvider; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; @@ -22,7 +28,7 @@ import com.waterbase.foodify.Model.User; public class SignUp extends AppCompatActivity { - EditText edtPhone, edtName, edtPassword; + EditText edtPhone, edtName, edtPassword, edtEmail; Button btnSignUp; TextView txtAppName; @@ -34,7 +40,7 @@ public class SignUp extends AppCompatActivity { edtName = (EditText) findViewById(R.id.txtFullName); edtPassword = (EditText) findViewById(R.id.edtPassword); edtPhone = (EditText) findViewById(R.id.edtPhone); - + edtEmail = (EditText) findViewById(R.id.edtEmail); btnSignUp = (Button) findViewById(R.id.btnSignUp); txtAppName = (TextView) findViewById(R.id.txtAppName); @@ -49,35 +55,54 @@ public class SignUp extends AppCompatActivity { btnSignUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (Common.isConnectedToInternet(getBaseContext())) { - final ProgressDialog mDialog = new ProgressDialog(SignUp.this); - mDialog.setMessage("Please waiting..."); - mDialog.show(); + if(!TextUtils.isEmpty(edtName.getText().toString()) && !TextUtils.isEmpty(edtPassword.getText().toString()) + && !TextUtils.isEmpty(edtPhone.getText().toString()) && !TextUtils.isEmpty(edtEmail.getText().toString())) { + if (Common.isConnectedToInternet(getBaseContext())) { + final ProgressDialog mDialog = new ProgressDialog(SignUp.this); + mDialog.setMessage("Vui lòng chờ..."); + mDialog.show(); - table_user.addListenerForSingleValueEvent(new ValueEventListener() { - @Override - public void onDataChange(DataSnapshot dataSnapshot) { - //Check if already user phone - if (dataSnapshot.child(edtPhone.getText().toString()).exists()) { - mDialog.dismiss(); - Toast.makeText(SignUp.this, "Phone Number is already register!", Toast.LENGTH_SHORT).show(); - finish(); - } else { - mDialog.dismiss(); - User user = new User(edtName.getText().toString(), edtPassword.getText().toString()); - table_user.child(edtPhone.getText().toString()).setValue(user); - Toast.makeText(SignUp.this, "Sign up successfully!", Toast.LENGTH_SHORT).show(); - finish(); + table_user.addListenerForSingleValueEvent(new ValueEventListener() { + @Override + public void onDataChange(DataSnapshot dataSnapshot) { + //Check if already user phone + if (dataSnapshot.child(edtPhone.getText().toString()).exists()) { + mDialog.dismiss(); + Toast.makeText(SignUp.this, "Số điện thoại đã được đăng ký!", Toast.LENGTH_SHORT).show(); + finish(); + } else { + //Firebase Auth + final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); + firebaseAuth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString()).addOnCompleteListener((task) -> { + if(task.isSuccessful()) { + firebaseAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if(task.isSuccessful()) { + mDialog.dismiss(); + User user = new User(edtName.getText().toString(), edtPassword.getText().toString()); + table_user.child(edtPhone.getText().toString()).setValue(user); + Toast.makeText(SignUp.this, "Đăng ký thành công!", Toast.LENGTH_SHORT).show(); + finish(); + } + } + }); + } + }); + } } - } - @Override - public void onCancelled(DatabaseError databaseError) { + @Override + public void onCancelled(DatabaseError databaseError) { - } - }); + } + }); + } else { + Toast.makeText(SignUp.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + return; + } } else { - Toast.makeText(SignUp.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + Toast.makeText(SignUp.this, "Vui lòng điền đầy đủ thông tin!", Toast.LENGTH_SHORT).show(); return; } } diff --git a/Foodify/app/src/main/res/drawable/ic_baseline_email_24.xml b/Foodify/app/src/main/res/drawable/ic_baseline_email_24.xml new file mode 100644 index 0000000..80111c8 --- /dev/null +++ b/Foodify/app/src/main/res/drawable/ic_baseline_email_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/Foodify/app/src/main/res/layout/activity_sign_up.xml b/Foodify/app/src/main/res/layout/activity_sign_up.xml index f7e7812..9d8b611 100644 --- a/Foodify/app/src/main/res/layout/activity_sign_up.xml +++ b/Foodify/app/src/main/res/layout/activity_sign_up.xml @@ -44,11 +44,28 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtAppName" /> + +