Search code examples
androidandroid-architecture-componentsandroid-pagingandroid-paging-library

Android PagingLibrary network + database


I'm working on Messaging app and I am implementing database + network to save chat messages from api and show them from database. I'm using BoundaryCallback to fetch message when database has no more data. My api works like this:

getlist(       @Query("msgid") long msgid,
               @Query("loadolder") boolean olderOrNewer,
               @Query("showcurrentMessage") boolean showcurrentMessage,
               @Query("MsgCountToLoad") int MsgCountToLoad);
  • msgid : last message id of that chat . if db is empty I request with the chat.getlastmessageid if the db has data but there was no more data I will send last message id in db to load more and if first time opening chat, the last message id in db was not equal to chat.lastmessageid it's mean there is new message to load.
  • loadolder : this flag false to tell api load new messages from this message id I sent to you and on and if flag set to true means load older messages from this message id I sent to you
  • showcurrentMessage: if true it will give me the current message (msgid) too
  • MsgCountToLoad : how many messages to take from api

question is how to handle this stuff in Pagginglibrary? How to tell it to load older or newer message which is based on scrolling position. First time to load data is easy, it will returns null object so I will use the chat.lastmessageid next time opening chat where I could check if chat.lastmessageid is equal to db.lastmessageid and tell it to load more new messages.


Solution

  • PagedList.BoundaryCallback has two separate APIs for prepending and appending.

    You should look to implement these methods:

    onItemAtEndLoaded
    onItemAtFrontLoaded
    

    Assuming your initial load loads the most recent messages, and scrolling up loads older messages, you can just pass true for loadolder in onItemAtFrontLoaded and false in onItemAtEndLoaded.