https://github.com/princessdharmy/triviums
One of the major problems Android Developers encounter is App Crash with some Exceptions. In order to tackle this problem, here is my implementation:
FirestoreResultResponse.javato encapsulate both the data and statepublic class FirestoreResultResponse {
Map<String, Object> data;
String error;
public FirestoreResultResponse() {
}
public FirestoreResultResponse(Map<String, Object> data, String error) {
this.data = data;
this.error = error;
}
}
StageRepository.java class :private void fetchProgress(String categoryName, ProgressCallback callback) {
...
docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
}
try {
callback.onCallback(new FirestoreResultResponse(task.getResult().getData(), null));
} catch (Exception e) {
if (e instanceof FirebaseFirestoreException) {
callback.onCallback(new FirestoreResultResponse(null, e.getLocalizedMessage()));
}
e.printStackTrace();
}
})
.addOnFailureListener(error -> {
callback.onCallback(new FirestoreResultResponse(null, error.toString()));
});
}
Implementation
LeaderboardRepository.javaprivate void fetchUsers(UserCallback callback){
CollectionReference collection = firestore.collection("users");
collection.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
document = task.getResult().getDocuments();
}
try {
callback.onCallback(document);
} catch (Exception e) {
if (e instanceof FirebaseFirestoreException) {
}
e.printStackTrace();
}
})
.addOnFailureListener(error -> {
});
}
LeaderboardFragment.java class gets the user data and displaypublic void getUsers() {
viewModel.getUsers().observe(getViewLifecycleOwner(), response -> {
binding.loader.setVisibility(View.GONE);
binding.peopleRecyclerview.setVisibility(View.VISIBLE);
if (response != null) {
adapter.updateData(response);
}
});
}