Currently my app gets all the data from the realtime database and filters through it and sends it back to android where the data is shown in a recycler view. How do I paginate my data so the user does not have to wait for the whole thing?
Firestore Function:
exports.getRecipes = functions.region('europe-west1').https.onCall((data, context) => {
const categories = data.categories;
const ingredients = data.ingredients;
recipesToReturn = [];
const eventsData = Promise.all(categories.map((category) => {
return admin.database().ref(category).once('value').then((snapshot) => {
snapshot.forEach(function (childSnapshot) {
if (canBeMade(childSnapshot.val(), ingredients)) {
recipesToReturn.push(childSnapshot.val());
}
});
return recipesToReturn;
});
}));
return eventsData;
});
Android Studio:
getRecipes().addOnCompleteListener(new OnCompleteListener<ArrayList<Recipe>>() {
@Override
public void onComplete(@NonNull Task<ArrayList<Recipe>> task) {
if (r.size() <= 0) {
progressBar.setVisibility(View.GONE);
Toast.makeText(RecipesActivity.this, "Nema recepata", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < r.size(); i++) {
recipes.add(r.get(i));
adapter.notifyItemInserted(i);
}
progressBar.setVisibility(View.GONE);
}
});
private Task<ArrayList<Recipe>> getRecipes() {
Map<String, Object> data = new HashMap<>();
data.put("categories", categories);
data.put("ingredients", ingredients);
return mFunctions
.getHttpsCallable("getRecipes")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, ArrayList<Recipe>>() {
@Override
public ArrayList<Recipe> then(@NonNull Task<HttpsCallableResult> task) throws Exception {
ArrayList<ArrayList<HashMap<String, Object>>> result = (ArrayList<ArrayList<HashMap<String, Object>>>) task.getResult().getData();
ArrayList<Recipe> recipes = new ArrayList<>();
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.get(i).size(); j++) {
recipes.add(new Recipe(result.get(i).get(j)));
}
}
return recipes;
}
});
}