实现SignActivity
1 package com.example.chatapptest.activities; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.util.Patterns; 8 import android.view.View; 9 import android.widget.Toast; 10 11 import com.example.chatapptest.R; 12 import com.example.chatapptest.databinding.ActivitySignInBinding; 13 import com.example.chatapptest.utilities.Constants; 14 import com.example.chatapptest.utilities.PreferenceManager; 15 import com.google.firebase.firestore.DocumentSnapshot; 16 import com.google.firebase.firestore.FirebaseFirestore; 17 18 import java.util.HashMap; 19 20 public class SignInActivity extends AppCompatActivity { 21 private ActivitySignInBinding binding; 22 private PreferenceManager preferenceManager; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 preferenceManager = new PreferenceManager(getApplicationContext()); 28 if(preferenceManager.getBoolean(Constants.KEY_IS_SIGNED_IN)){ 29 Intent intent = new Intent(getApplicationContext(), MainActivity.class); 30 startActivity(intent); 31 finish(); 32 } 33 binding = ActivitySignInBinding.inflate(getLayoutInflater()); 34 setContentView(binding.getRoot()); 35 setListeners(); 36 } 37 38 private void setListeners(){ 39 binding.textCreateNewAccount.setOnClickListener(v -> 40 startActivity(new Intent(getApplicationContext(), SignUpActivity.class))); 41 binding.buttonSignIn.setOnClickListener(v -> { 42 if(isValidSignInDetails()){ 43 signIn(); 44 } 45 }); 46 } 47 48 private void showToast(String message){ 49 Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 50 } 51 52 private void signIn(){ 53 loading(true); 54 FirebaseFirestore database = FirebaseFirestore.getInstance(); 55 database.collection(Constants.KEY_COLLECTION_USERS) 56 .whereEqualTo(Constants.KEY_EMAIL,binding.inputEmail.getText().toString()) 57 .whereEqualTo(Constants.KEY_PASSWORD,binding.inputPassword.getText().toString()) 58 .get() 59 .addOnCompleteListener(task -> { 60 if(task.isSuccessful() && task.getResult() != null 61 && task.getResult().getDocuments().size()>0){ 62 DocumentSnapshot documentSnapshot = task.getResult().getDocuments().get(0); 63 preferenceManager.putBoolean(Constants.KEY_IS_SIGNED_IN,true); 64 preferenceManager.putString(Constants.KEY_USER_ID, documentSnapshot.getId()); 65 preferenceManager.putString(Constants.KEY_NAME,documentSnapshot.getString(Constants.KEY_NAME)); 66 preferenceManager.putString(Constants.KEY_IMAGE,documentSnapshot.getString(Constants.KEY_IMAGE)); 67 Intent intent = new Intent(getApplicationContext(), MainActivity.class); 68 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 69 startActivity(intent); 70 }else { 71 loading(false); 72 showToast("Unable to sign in"); 73 } 74 }); 75 } 76 77 private void loading(Boolean isLoading){ 78 if(isLoading){ 79 binding.buttonSignIn.setVisibility(View.INVISIBLE); 80 binding.progressBar.setVisibility(View.VISIBLE); 81 }else { 82 binding.progressBar.setVisibility(View.INVISIBLE); 83 binding.buttonSignIn.setVisibility(View.VISIBLE); 84 } 85 } 86 87 private Boolean isValidSignInDetails(){ 88 if(binding.inputEmail.getText().toString().trim().isEmpty()){ 89 showToast("Enter email"); 90 return false; 91 } else if (!Patterns.EMAIL_ADDRESS.matcher(binding.inputEmail.getText().toString()).matches()) { 92 showToast("Enter valid email"); 93 return false; 94 } else if (binding.inputPassword.getText().toString().trim().isEmpty()) { 95 showToast("Enter password"); 96 return false; 97 }else { 98 return true; 99 } 100 } 101 }
标签:结对,12,binding,冲刺,Intent,KEY,import,com,Constants From: https://www.cnblogs.com/Lyh3012648079/p/18242278