I am trying to query if the current User has sent a friend request to a specific person. I have a button btnFriendRequest with the following code:
binding.btnFriendRequest.setOnClickListener(view ->{
final Map<String, Object> addUsertoArrayMap = new HashMap<>();
addUsertoArrayMap.put("requests", FieldValue.arrayUnion(curUser));
curPerson.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
db.collection("people").document(document.getId()).update(addUsertoArrayMap);
binding.btnFriendRequest.setEnabled(false);
}
});
});
Basically, when I press btnFriendRequest, the email of the current user gets passed off to a Firestore Field "requests" under a specific "Person" document.I am trying to conditionally render my button (disable it with different text "Friend request already sent!"). Since I know the person for sure has sent a friend request, I set the button to disable within my onClickListener. However when I go out of the fragment(this whole thing is in a fragment). I find that it does not stick. Here is my current attempt to check:
I have a function that checks whether the current User is already in the "requests" array, i.e. the current User has already sent a request.
public void userApplied(){
curJob.whereArrayContains("requests",curUser).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
if (task.getResult().getDocuments().isEmpty()){
sent = false;
Log.d("bid", "user has not sent friend request");
Log.d("fact", "sent value is: "+ sent);
}
else{
sent = true;
Log.d("bid", "user has sent friend request");
Log.d("fact", "sent value is: "+ sent);
}
};
}
});
}
I have verified that logcat does indeed display the correct value. Here is my code to conditionally render my button, it is placed under OnCreateView:
if (sent) {
binding.btnAcceptJob.setEnabled(false);
Log.d("check", "onCreateView: button should be disabled now");
}else{
Log.d("check", "onCreateView: button should not be disabled now");
}
What I suspect is OnCreateView does not properly wait for my query to finish despite me calling for my function earlier. I am also trying to render a set of ChipViews depending on some other categories, but never got to it because I got stuck on the button. What can I do to resolve this? Also is my process correct or is there an easier way to do what I want to do?