实现SignUpActivity
1 package com.example.chatapptest.activities; 2 3 import androidx.activity.result.ActivityResultLauncher; 4 import androidx.activity.result.contract.ActivityResultContracts; 5 import androidx.appcompat.app.AppCompatActivity; 6 7 import android.content.ComponentName; 8 import android.content.Intent; 9 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.provider.MediaStore; 14 import android.util.Base64; 15 import android.util.Patterns; 16 import android.view.View; 17 import android.widget.Toast; 18 19 import com.example.chatapptest.R; 20 import com.example.chatapptest.databinding.ActivitySignUpBinding; 21 import com.example.chatapptest.utilities.Constants; 22 import com.example.chatapptest.utilities.PreferenceManager; 23 import com.google.firebase.firestore.FirebaseFirestore; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.FileNotFoundException; 27 import java.io.InputStream; 28 import java.util.HashMap; 29 30 public class SignUpActivity extends AppCompatActivity { 31 32 private ActivitySignUpBinding binding; 33 private PreferenceManager preferenceManager; 34 private String encodedImage; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 binding = ActivitySignUpBinding.inflate(getLayoutInflater()); 40 setContentView(binding.getRoot()); 41 preferenceManager = new PreferenceManager(getApplicationContext()); 42 setListeners(); 43 } 44 45 private void setListeners(){ 46 binding.textSignIn.setOnClickListener(v -> onBackPressed()); 47 binding.buttonSignUp.setOnClickListener(v -> { 48 if(isValidSignUpDetails()){ 49 signUp(); 50 } 51 }); 52 binding.layoutImage.setOnClickListener(v -> { 53 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 54 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 55 pickImage.launch(intent); 56 }); 57 } 58 59 private void showToast(String message){ 60 Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 61 } 62 63 private void signUp(){ 64 loading(true); 65 FirebaseFirestore database = FirebaseFirestore.getInstance(); 66 HashMap<String,Object> user = new HashMap<>(); 67 user.put(Constants.KEY_NAME,binding.inputName.getText().toString()); 68 user.put(Constants.KEY_EMAIL,binding.inputEmail.getText().toString()); 69 user.put(Constants.KEY_PASSWORD,binding.inputPassword.getText().toString()); 70 user.put(Constants.KEY_IMAGE,encodedImage); 71 database.collection(Constants.KEY_COLLECTION_USERS) 72 .add(user) 73 .addOnSuccessListener(documentReference -> { 74 loading(false); 75 preferenceManager.putBoolean(Constants.KEY_IS_SIGNED_IN,true); 76 preferenceManager.putString(Constants.KEY_USER_ID, documentReference.getId()); 77 preferenceManager.putString(Constants.KEY_NAME,binding.inputName.getText().toString()); 78 preferenceManager.putString(Constants.KEY_IMAGE,encodedImage); 79 Intent intent = new Intent(getApplicationContext(),MainActivity.class); 80 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 81 startActivity(intent); 82 }) 83 .addOnFailureListener(exception -> { 84 loading(false); 85 showToast(exception.getMessage()); 86 }); 87 } 88 89 private String encodeImage(Bitmap bitmap){ 90 int previewWidth = 150; 91 int previewHeight = bitmap.getHeight() * previewWidth / bitmap.getWidth(); 92 Bitmap previewBitmap = Bitmap.createScaledBitmap(bitmap,previewWidth,previewHeight,false); 93 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 94 previewBitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream); 95 byte[] bytes = byteArrayOutputStream.toByteArray(); 96 return Base64.encodeToString(bytes,Base64.DEFAULT); 97 } 98 99 private final ActivityResultLauncher<Intent> pickImage = registerForActivityResult( 100 new ActivityResultContracts.StartActivityForResult(), 101 result ->{ 102 if(result.getResultCode() == RESULT_OK){ 103 if(result.getData() != null){ 104 Uri imageUri = result.getData().getData(); 105 try { 106 InputStream inputStream = getContentResolver().openInputStream(imageUri); 107 Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 108 binding.imageProfile.setImageBitmap(bitmap); 109 binding.textAddImage.setVisibility(View.GONE); 110 encodedImage = encodeImage(bitmap); 111 }catch (FileNotFoundException e){ 112 e.printStackTrace(); 113 } 114 } 115 } 116 } 117 ); 118 119 private Boolean isValidSignUpDetails(){ 120 if(encodedImage == null){ 121 showToast("Select profile image"); 122 return false; 123 }else if (binding.inputName.getText().toString().trim().isEmpty()){ 124 showToast("Enter name"); 125 return false; 126 } else if (binding.inputEmail.getText().toString().trim().isEmpty()) { 127 showToast("Enter email"); 128 return false; 129 } else if (!Patterns.EMAIL_ADDRESS.matcher(binding.inputEmail.getText().toString()).matches()) { 130 showToast("Enter valid email"); 131 return false; 132 } else if (binding.inputPassword.getText().toString().trim().isEmpty()) { 133 showToast("Enter password"); 134 return false; 135 } else if (binding.inputConfirmPassword.getText().toString().trim().isEmpty()) { 136 showToast("Confirm you password"); 137 return false; 138 } else if (!binding.inputPassword.getText().toString().equals(binding.inputConfirmPassword.getText().toString())) { 139 showToast("Password & confirm password must be same"); 140 return false; 141 }else { 142 return true; 143 } 144 } 145 146 private void loading(Boolean isLoading){ 147 if(isLoading){ 148 binding.buttonSignUp.setVisibility(View.INVISIBLE); 149 binding.progressBar.setVisibility(View.VISIBLE); 150 }else { 151 binding.progressBar.setVisibility(View.INVISIBLE); 152 binding.buttonSignUp.setVisibility(View.VISIBLE); 153 } 154 } 155 }
标签:13,binding,结对,getText,toString,冲刺,import,android,Constants From: https://www.cnblogs.com/Lyh3012648079/p/18242281