I have a chat app where at first I show the last 50 messages and then if the user scrolls up, it loads the previous 50 messages.
However, for some reason, I'm like stuck on a limit of 1 scroll. What I mean is if for example, I have 102 messages in my chat, when I initialize the chat it shows messages 62-102.
Then, if the user scrolls up it shows him messages 12-62.
Then, if the user scrolls up it still stuck on 12-62.
The code I use is:
public void getChats(String chatID, Boolean loadFlag , EventListener<QuerySnapshot> listener) {
Query lastMessages = db.collection("Chats")
.orderBy("SentTime", Query.Direction.ASCENDING).limitToLast( 50 );
if (loadFlag == false) {
lastMessages.addSnapshotListener( listener );
}
if (loadFlag == true) {
lastMessages.get()
.addOnSuccessListener( new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
DocumentSnapshot lastVisible = documentSnapshots.getDocuments().get( 0 );
Query previousMessages = db.collection( "Chats" )
.orderBy( "SentTime", Query.Direction.ASCENDING )
.endAt( lastVisible )
.limit( 50 );
previousMessages.addSnapshotListener( listener );
}
} );
}
}
The loadFlag is some parameter that I use in order to make sure that the listener is only at the right place.
From my understanding, it is because I always use limitToLast(50) which is always the same 50 but how can I set there the value of the new last object?
The packages im using are:
import android.util.Log;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.HashMap;
import java.util.Map;
Is there any reason why I can't go further to 1-12 in my case?
Thank you