Add identified code in sign up user, improve layout

This commit is contained in:
Nezumi
2023-03-21 15:19:54 +07:00
parent a4b5472fc8
commit a645e680d8
12 changed files with 432 additions and 215 deletions
@@ -32,17 +32,24 @@ import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;
import com.thecode.aestheticdialogs.OnDialogClickListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import io.paperdb.Paper;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@@ -60,6 +67,8 @@ public class AccountAndProfileActivity extends AppCompatActivity {
private Uri imageUri;
final private StorageReference storageReference = FirebaseStorage.getInstance().getReference();
ConstraintLayout progressLayout;
FirebaseAuth mAuth;
FirebaseUser firebaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -72,6 +81,15 @@ public class AccountAndProfileActivity extends AppCompatActivity {
//Init data
initData();
//Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
//Paper Init
Paper.init(this);
//Show notification about email verify for user
showNotificationEmailVerify();
chooseDateOfBirth();
change_password.setOnClickListener(new View.OnClickListener() {
@@ -199,66 +217,42 @@ public class AccountAndProfileActivity extends AppCompatActivity {
}
}
private void initComponent() {
//Init Component
textInput_email= findViewById(R.id.textInput_email);
textInput_phone = findViewById(R.id.textInput_phone);
textInput_fullName = findViewById(R.id.textInput_fullName);
textInput_birthDay = findViewById(R.id.textInput_birthDay);
change_password = findViewById(R.id.change_password);
private void showNotificationEmailVerify(){
//Get firebase user from Paper
firebaseUser = Paper.book().read("user");
update_button = findViewById(R.id.updated_button);
update_image_button = findViewById(R.id.change_image_button);
// Check if user is signed in (non-null) and update UI accordingly.
firebaseUser = mAuth.getCurrentUser();
if(firebaseUser != null){
back_image = findViewById(R.id.back_image);
edt_birthday = findViewById(R.id.edt_birthDay);
edt_email = findViewById(R.id.edt_email);
edt_phone = findViewById(R.id.edt_phone);
edt_fullName = findViewById(R.id.edt_fullName);
profile_avatar = findViewById(R.id.profile_avatar);
progressLayout = findViewById(R.id.progress_layout);
}
private void setFontUI() {
textInput_email.setTypeface(Common.setFontBebas(getAssets()));
textInput_phone.setTypeface(Common.setFontBebas(getAssets()));
textInput_fullName.setTypeface(Common.setFontBebas(getAssets()));
textInput_birthDay.setTypeface(Common.setFontBebas(getAssets()));
edt_birthday.setTypeface(Common.setFontBebas(getAssets()));
}
private void chooseDateOfBirth() {
//Get date, month, year from user
String [] dateParts = edt_birthday.getText().toString().split("-");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
//Calendar date of birth
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH,month);
myCalendar.set(Calendar.DAY_OF_MONTH,day);
updateLabel();
if(!firebaseUser.isEmailVerified()){
showDialogCaution();
textInput_email.setError("Nhấp vào đây để xác thực!");
textInput_email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//When user click on the text
firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(AccountAndProfileActivity.this, "Email đã được gửi đi, vui lòng kiểm tra hộp thư của bạn!", Toast.LENGTH_SHORT).show();
}
});
}
});
} else {
textInput_email.setErrorEnabled(false);
}
}, Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
edt_birthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerDialog.show();
}
});
}
}
private void updateLabel(){
SimpleDateFormat dateFormat=new SimpleDateFormat(Common.FORMAT_DATE, Locale.US);
edt_birthday.setText(dateFormat.format(myCalendar.getTime()));
private void showDialogCaution() {
new AestheticDialog.Builder(this, DialogStyle.RAINBOW, DialogType.WARNING)
.setTitle("Email chưa được xác thực!")
.setMessage("Vui lòng xác thực email để bạn có thể thực hiện việc lấy lại mật khẩu nếu quên!")
.setCancelable(true)
.show();
}
private void updateUser(String imageUrl) {
@@ -286,4 +280,62 @@ public class AccountAndProfileActivity extends AppCompatActivity {
}
});
}
private void initComponent() {
//Init Component
textInput_email= findViewById(R.id.textInput_email);
textInput_phone = findViewById(R.id.textInput_phone);
textInput_fullName = findViewById(R.id.textInput_fullName);
textInput_birthDay = findViewById(R.id.textInput_birthDay);
change_password = findViewById(R.id.change_password);
update_button = findViewById(R.id.updated_button);
update_image_button = findViewById(R.id.change_image_button);
back_image = findViewById(R.id.back_image);
edt_birthday = findViewById(R.id.edt_birthDay);
edt_email = findViewById(R.id.edt_email);
edt_phone = findViewById(R.id.edt_phone);
edt_fullName = findViewById(R.id.edt_fullName);
profile_avatar = findViewById(R.id.profile_avatar);
progressLayout = findViewById(R.id.progress_layout);
}
private void setFontUI() {
textInput_email.setTypeface(Common.setFontBebas(getAssets()));
textInput_phone.setTypeface(Common.setFontBebas(getAssets()));
textInput_fullName.setTypeface(Common.setFontBebas(getAssets()));
textInput_birthDay.setTypeface(Common.setFontBebas(getAssets()));
edt_birthday.setTypeface(Common.setFontBebas(getAssets()));
}
private void chooseDateOfBirth() {
//Get date, month, year from user
String [] dateParts = edt_birthday.getText().toString().split("-");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
//Calendar date of birth
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH,month);
myCalendar.set(Calendar.DAY_OF_MONTH,day);
updateLabel();
}
}, Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
edt_birthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerDialog.show();
}
});
}
private void updateLabel(){
SimpleDateFormat dateFormat=new SimpleDateFormat(Common.FORMAT_DATE, Locale.US);
edt_birthday.setText(dateFormat.format(myCalendar.getTime()));
}
}
@@ -1,22 +1,40 @@
package com.capstone.foodify.Activity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.capstone.foodify.API.FoodApi;
import com.capstone.foodify.Common;
import com.capstone.foodify.Model.Response.CustomResponse;
import com.capstone.foodify.Model.User;
import com.capstone.foodify.R;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ForgotPasswordActivity extends AppCompatActivity {
TextInputLayout textInput_account;
TextInputLayout textInput_phone;
EditText edt_phone;
MaterialButton send_code_button;
ConstraintLayout progress_layout;
ImageView back_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -36,21 +54,87 @@ public class ForgotPasswordActivity extends AppCompatActivity {
send_code_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ForgotPasswordActivity.this, VerifyAccountActivity.class));
findUserByPhone(edt_phone.getText().toString());
}
});
setFontUI();
}
private void findUserByPhone(String phone){
User user = new User();
user.setPhoneNumber(phone);
FoodApi.apiService.checkEmailOrPhoneExist(user).enqueue(new Callback<CustomResponse>() {
@Override
public void onResponse(Call<CustomResponse> call, Response<CustomResponse> response) {
CustomResponse responseData = response.body();
if(responseData != null){
if(responseData.isTrue()){
//If this phone number has already register in server, send the code
} else {
//If this phone number
Toast.makeText(ForgotPasswordActivity.this, "Hệ thống hiện chưa tìm thấy số điện thoại này, vui lòng kiểm tra lại!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<CustomResponse> call, Throwable t) {
}
});
}
private void sendCode(){
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.setLanguageCode("vi");
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(Common.PHONE_CODE + edt_phone.getText().toString())
.setTimeout(0L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
Intent intent = new Intent(ForgotPasswordActivity.this, VerifyAccountActivity.class);
intent.putExtra("phone", edt_phone.getText().toString());
intent.putExtra("verificationId", verificationId);
intent.putExtra(Common.IS_FORGOT_PASSWORD, Common.IS_FORGOT_PASSWORD);
startActivity(intent);
finish();
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
// ...
}
})
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
private void setFontUI() {
textInput_account = (TextInputLayout) findViewById(R.id.textInput_account);
textInput_account.setTypeface(Common.setFontBebas(getAssets()));
textInput_phone = (TextInputLayout) findViewById(R.id.textInput_phone);
textInput_phone.setTypeface(Common.setFontBebas(getAssets()));
}
private void initComponent() {
back_image = (ImageView) findViewById(R.id.back_image);
send_code_button = (MaterialButton) findViewById(R.id.send_code_button);
edt_phone = findViewById(R.id.edt_phone);
progress_layout = findViewById(R.id.progress_layout);
}
}
@@ -52,16 +52,16 @@ import retrofit2.Callback;
import retrofit2.Response;
public class SignUpActivity extends AppCompatActivity {
TextInputLayout textInput_password, textInput_phone, textInput_email,
TextInputLayout textInput_password, textInput_phone, textInput_email, textInput_id_card,
textInput_address, textInput_confirmPassword, textInput_fullName, textInput_birthDay;
final Calendar myCalendar= Calendar.getInstance();
private EditText edt_birthday, edt_phone, edt_passWord, edt_confirmPassword, edt_fullName, edt_address, edt_email;
private EditText edt_birthday, edt_phone, edt_passWord, edt_confirmPassword, edt_fullName, edt_address, edt_email, edt_id_card;
private TextView signIn_textView, errorText, errorTextDistrictWard;
private Spinner wardSpinner, districtSpinner;
private ImageView back_image;
private MaterialButton signUpButton;
private ConstraintLayout progressLayout;
private String email, phone, name, dateOfBirth, address, district, ward, password = null;
private String email, phone, name, dateOfBirth, address, district, ward, password = null, identifiedCode;
private static final ArrayList<String> wardList = new ArrayList<>();
private static final ArrayList<String> districtList = new ArrayList<>();
@@ -74,6 +74,7 @@ public class SignUpActivity extends AppCompatActivity {
textInput_fullName = (TextInputLayout) findViewById(R.id.textInput_fullName);
textInput_birthDay = (TextInputLayout) findViewById(R.id.textInput_birthDay);
textInput_email = findViewById(R.id.textInput_email);
textInput_id_card = findViewById(R.id.textInput_id_card);
signUpButton = findViewById(R.id.sign_up_button);
@@ -84,6 +85,7 @@ public class SignUpActivity extends AppCompatActivity {
edt_fullName = findViewById(R.id.edt_fullName);
edt_address = findViewById(R.id.edt_address);
edt_email = findViewById(R.id.edt_email);
edt_id_card = findViewById(R.id.edt_id_card);
districtSpinner = (Spinner) findViewById(R.id.district);
wardSpinner = (Spinner) findViewById(R.id.ward);
@@ -166,7 +168,7 @@ public class SignUpActivity extends AppCompatActivity {
user.setDateOfBirth(dateOfBirth);
user.setEmail(email);
user.setFullName(name);
user.setIdentifiedCode("");
user.setIdentifiedCode(identifiedCode);
user.setImageUrl("");
user.setLocked(false);
user.setPhoneNumber(phone);
@@ -239,6 +241,14 @@ public class SignUpActivity extends AppCompatActivity {
textInput_fullName.setErrorEnabled(false);
}
//Check identified Code
if(identifiedCode.length() < 9 && identifiedCode.length() > 10){
textInput_id_card.setError("CCCD / CMND không hợp lệ!");
dataHasValidate = false;
} else {
textInput_id_card.setErrorEnabled(false);
}
//Check birthday
if(dateOfBirth.isEmpty()){
textInput_birthDay.setError("Ngày tháng năm sinh của bạn còn trống!");
@@ -306,6 +316,7 @@ public class SignUpActivity extends AppCompatActivity {
district = districtSpinner.getSelectedItem().toString();
ward = wardSpinner.getSelectedItem().toString();
password = edt_confirmPassword.getText().toString();
identifiedCode = edt_id_card.getText().toString();
}
@@ -503,7 +514,6 @@ public class SignUpActivity extends AppCompatActivity {
textInput_fullName.setTypeface(Common.setFontBebas(getAssets()));
textInput_birthDay.setTypeface(Common.setFontBebas(getAssets()));
textInput_email.setTypeface(Common.setFontBebas(getAssets()));
edt_birthday.setTypeface(Common.setFontBebas(getAssets()));
textInput_id_card.setTypeface(Common.setFontBebas(getAssets()));
}
}
@@ -244,13 +244,15 @@ public class VerifyAccountActivity extends AppCompatActivity {
@Override
public void onResponse(Call<User> call, Response<User> response) {
progressLayout.setVisibility(View.GONE);
//Create user success;
Toast.makeText(VerifyAccountActivity.this, "Tạo tài khoản thành công!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(VerifyAccountActivity.this, SignInActivity.class));
finish();
if(response.code() == 200){
progressLayout.setVisibility(View.GONE);
//Create user success;
Toast.makeText(VerifyAccountActivity.this, "Tạo tài khoản thành công!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(VerifyAccountActivity.this, SignInActivity.class));
finish();
} else {
Toast.makeText(VerifyAccountActivity.this, "Lỗi hệ thống! Code: " + response.code(), Toast.LENGTH_SHORT).show();
}
}
@Override
@@ -16,6 +16,7 @@ import com.capstone.foodify.API.FoodApiToken;
import com.capstone.foodify.Fragment.HomeFragment;
import com.capstone.foodify.Model.Basket;
import com.capstone.foodify.Model.User;
import com.google.firebase.auth.FirebaseUser;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;
@@ -31,6 +32,7 @@ import retrofit2.Callback;
import retrofit2.Response;
public class Common {
public static final String IS_FORGOT_PASSWORD = "isForgotPassword";
public static final String FORMAT_DATE="dd-MM-yyyy";
public static final String VALID_EMAIL_ADDRESS_REGEX = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$";
public static final String PHONE_CODE = "+84";
@@ -49,9 +49,6 @@ public class ProfileFragment extends Fragment {
popupDialog = PopupDialog.getInstance(getContext());
//Set user name on textview
user_name.setText(Common.CURRENT_USER.getFullName());
//Set image for user
if(Common.CURRENT_USER.getImageUrl().isEmpty()){
profile_avatar.setImageResource(R.drawable.profile_avatar);