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.view.View; 8 9 import com.example.chatapptest.R; 10 import com.example.chatapptest.adapters.UsersAdapter; 11 import com.example.chatapptest.databinding.ActivityUsersBinding; 12 import com.example.chatapptest.listeners.UserListener; 13 import com.example.chatapptest.models.User; 14 import com.example.chatapptest.utilities.Constants; 15 import com.example.chatapptest.utilities.PreferenceManager; 16 import com.google.firebase.firestore.FirebaseFirestore; 17 import com.google.firebase.firestore.QueryDocumentSnapshot; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 public class UsersActivity extends AppCompatActivity implements UserListener { 23 24 private ActivityUsersBinding binding; 25 private PreferenceManager preferenceManager; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 binding = ActivityUsersBinding.inflate(getLayoutInflater()); 31 setContentView(binding.getRoot()); 32 preferenceManager = new PreferenceManager(getApplicationContext()); 33 setListeners(); 34 getUsers(); 35 } 36 37 private void setListeners(){ 38 binding.imageBack.setOnClickListener(v -> onBackPressed()); 39 } 40 41 private void getUsers(){ 42 loading(true); 43 FirebaseFirestore database = FirebaseFirestore.getInstance(); 44 database.collection(Constants.KEY_COLLECTION_USERS) 45 .get() 46 .addOnCompleteListener(task -> { 47 loading(false); 48 String currentUserId = preferenceManager.getString(Constants.KEY_USER_ID); 49 if(task.isSuccessful() && task.getResult() != null){ 50 List<User> users = new ArrayList<>(); 51 for(QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()){ 52 if(currentUserId.equals(queryDocumentSnapshot.getId())){ 53 continue; 54 } 55 User user = new User(); 56 user.name = queryDocumentSnapshot.getString(Constants.KEY_NAME); 57 user.email = queryDocumentSnapshot.getString(Constants.KEY_EMAIL); 58 user.image = queryDocumentSnapshot.getString(Constants.KEY_IMAGE); 59 user.token = queryDocumentSnapshot.getString(Constants.KEY_FCM_TOKEN); 60 user.id = queryDocumentSnapshot.getId(); 61 users.add(user); 62 } 63 if(users.size() > 0){ 64 UsersAdapter usersAdapter = new UsersAdapter(users,this); 65 binding.userRecyclerView.setAdapter(usersAdapter); 66 binding.userRecyclerView.setVisibility(View.VISIBLE); 67 }else { 68 showErrorMessage(); 69 } 70 }else { 71 showErrorMessage(); 72 } 73 }); 74 } 75 76 private void showErrorMessage(){ 77 binding.textErrorMessage.setText(String.format("%s","No user available")); 78 binding.textErrorMessage.setVisibility(View.VISIBLE); 79 } 80 81 private void loading(Boolean isLoading){ 82 if(isLoading){ 83 binding.progressBar.setVisibility(View.VISIBLE); 84 }else { 85 binding.progressBar.setVisibility(View.INVISIBLE); 86 } 87 } 88 89 @Override 90 public void onUserClicked(User user) { 91 Intent intent = new Intent(getApplicationContext(), ChatActivity.class); 92 intent.putExtra(Constants.KEY_USER,user); 93 startActivity(intent); 94 finish(); 95 } 96 }
标签:binding,冲刺,2024,chatapptest,user,import,第二阶段,com,Constants From: https://www.cnblogs.com/Lyh3012648079/p/18227796