I'm currently creating an Android application that works with Firestore, and I would like to implement a method / class to check if there is a document with a given path in one of the collections.
I tried to do this by trying to get data from one document, then if the object does not exist, I wanted to return the value of the second path, and the default would be the value of the path checked. And here's the problem, since it's an anonymous class, I can't assign anything to a variable outside of that class. Below is the code and a preview screen from my Firebase database.
public class UserValid
{
String typeOfAcc = "Podopieczni";
FirebaseFirestore fStore;
FirebaseAuth fAuth;
DocumentReference documentReference;
List itemList = new ArrayList<>();
String userID;
public UserValid()
{
Log.d("TAG", "Błąd, wybrano konstruktor bez argumentów");
}
//Metoda sprawdzająca jaki typ konta jest aktualnie obsługiwany
// Jeśli wartość true - konto podopiecznego, wartość false - konto trenera;
public UserValid(FirebaseAuth fAuth, FirebaseFirestore fStore)
{
userID = fAuth.getCurrentUser().getUid();
this.fAuth = fAuth;
this.fStore = fStore;
documentReference = fStore.collection("Podopieczni").document(userID);
readData(new FirestoreCallback()
{
@Override
public void onCallback(List<String> list)
{
if(itemList.get(0) == null)
{
typeOfAcc="Trenerzy";
Log.d("TAG", itemList.toString());
}
}
});
}
private void readData(FirestoreCallback firestoreCallback)
{
//DocumentReference docRef = db.collection("cities").document("SF");
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful())
{
//for(DocumentSnapshot document : task.getResult()) {
String itemName = task.getResult().getString("Imie");
itemList.add(itemName);
// }
firestoreCallback.onCallback(itemList);
}
}
});
}
private interface FirestoreCallback
{
void onCallback(List<String> list);
}
}