Show endless previous objects recyclerView

Viewed 208

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

2 Answers

For my understanding limitToLast(50) always takes the 50 last messages. But there might be a solution for your usecase: According to the documentation there is a method called offset(int n) which will ignore the first n results. You just need to store the amount of skipping messages in a variable.

Change your code like this and try again:

int x = //TODO for each user request of old messages increment x and store it somewhere
Query lastMessages = db.collection("Chats")
        .orderBy("SentTime", Query.Direction.ASCENDING).offset(x*50).limit(50);

Also please change this

if (loadFlag == false) {
    ...
}

if (loadFlag == true) {
    ...
}

to this code:

if (loadFlag) {
    //TODO CAREFUL, here loadFlag is TRUE
    ...
} else {
    //TODO CAREFUL, here loadFlag is FALSE
}

the problem is combination of limitToLast and lastVisible basically your saying get the last 50 to show and previous 50 items behind that for when the user scrolls but when user keeps scrolling you have no mechanism to load the other 50 before these so it get stuck on last 100, for successfully implementing this you need a paging and for paging you must have a variable to hold the current page which increment as @F_Schmidt suggested or check your lastVisible I think it is not returning the value that it should.

also you could add your current page to your QuerySnapShot model.

Related