I'm developing a social app, and I'm at the Feed part, which will display recent interactions made by friends. These friends are sorted by the amount of interaction we both have in the app - I call it RankFactor. The higher this value is, the higher my friend's interaction will appear. First I read the User ID from my list of friends, ordered by The Rank Factor then I get each recent interaction.
I want the list of interactions displayed is created dynamically. It's supposed to be an infinite scroll, where per each scroll there will appear around 10 interactions.
So, ideally, I'd like that I'd keep reading friends ID's, and their interactions until I have 10 on the list. However, I'll have these interactions saved in a member List, which will be inside the ValueEventListener scope. Even if I use orderByChild("rankFactor"), and then I limitToLast(10), nothing guarantees that these highest ranked Friends would have interacted recently. Therefore I believe I would need to have some kind of loop, focusing on the size of the interactions List.
How could I achieve my goal, knowing that "logically", what I want to achieve, would be something like the following:
while(mListOfInteractions.size() <= 10){
Query friendQuery = mDatabase.getReference().child(DB_REFERENCE).child(uId).child(FRIENDS_DB)
.orderByChild("rankFactor").endAt(mLastRankFactor - 0.0000001).limitToLast(1);
friendQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
FriendRank friendRank = dataSnapshot.getValue(FriendRank.class);
mLastRankFactor = friendRank.getRankFactor();
final String userId = friendRank.getUserId();
Query interactionsQuery = mDatabase.getReference().child(DB_REFERENCE).child(userId).child(INTERACTIONS_GIVEN).orderByChild("date")
.startAt(System.currentTimeMillis() - THRESHOLD_DAYS_IN_MILLIS).limitToLast(3);
interactionsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChildren()){
for(DataSnapshot interactionData: dataSnapshot.getChildren()){
String interactionKey = interactionData.getKey();
Interaction interaction = interactionData.getValue(Interaction.class);
if(!DataUtils.doesListContainItem(mSavedInteractionsKeys, interactionsKey)){
mListOfInteractions.add(interactions);
mSavedInteractionsKeys.add(interactionKey);
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
So, as you see, I'd like that my list would keep on building up until I had gathered the amount of 10 Interactions (or a little more). The thing is, as many of you know, truly I can only access mListOfInteractions real value within the 2nd ValueEventListener. How can I make the mListOfInteractions value accessible to my While loop?
Was I able to explain my challenge and goal?
Thank you in advance!