0

I am building a chat app and displaying the text messages with the code below.I wanna know how i can paginate this live data that i am getting from cloud firestore so that a user can only look at the past say 30 messages using the paging 3.0 library.

ChatLogViewmodel

fun listenForMessages(user: User): LiveData<List<String>> {
        val toId = user.uid
        val betweenList = mutableListOf(Id(fromId), Id(toId))
        val sortedList = betweenList.sortedBy { it.Id }
        repository.getChatReference()?.whereEqualTo("between", sortedList)
            ?.addSnapshotListener { documents, e ->
                if (e != null) {
                    return@addSnapshotListener
                }
                if (documents != null) {
                    for (document in documents) {
                        docId = document.id
                        docids.clear()
                        docids.add(docId)
                    }
                }
                docList.value = docids
            }
        return docList
    }
fun listener(docId: String): LiveData<List<ChatMessage>> {
        repository.getChatReference()?.document(docId)?.collection("Messages")?.orderBy(
            "timestamp",
            Query.Direction.ASCENDING
        )?.addSnapshotListener { snap, e ->
            if (e != null) {
                return@addSnapshotListener
            }
            if (snap != null) {
                messages.clear()
                for (doc in snap.documents) {
                    val chatMessage = doc.toObject(ChatMessage::class.java)
                    if (chatMessage != null) {
                        messages.add(chatMessage)
                        isScrollable.value = true
                    }
                }
            }

            messageList.value = messages
        }
        return messageList
    }

ChatLogActivity

viewModel.listenForMessages(user).observe(this, {
                it.forEach {doc->
                    viewModel.listener(doc).observe(this, { chatMessages->
                        binding.chatLogRecycler.adapter = ChatAdapter(chatMessages)
                        if (binding.chatLogRecycler.adapter!==null)
                            binding.chatLogRecycler.scrollToPosition(((binding.chatLogRecycler.adapter)?.itemCount!!) -1)
                    })
                }
            })

Repository

class ChatRepository @Inject constructor() {

    fun getUserReference(uid:String): DocumentReference? {
        return Firebase.firestore.collection("Users").document(uid)
    }
    fun getChatReference(): CollectionReference? {
        return Firebase.firestore.collection("ChatChannels")
    }
}
saket
  • 297
  • 1
  • 3
  • 9
  • Did you see the Firebase documentation on paginating and limiting data? https://firebase.google.com/docs/firestore/query-data/query-cursors – Frank van Puffelen Sep 24 '20 at 13:49
  • @FrankvanPuffelen yes i did.I wanted to do it using the paging 3.0 library – saket Sep 24 '20 at 13:51
  • Did you try applying the explanation and code in the documentation to that library already? Nothing in the code you shared seems to do so, and that makes the question a bit broad for Stack Overflow. – Frank van Puffelen Sep 24 '20 at 14:18
  • General suggestion: For chat applications where you will be accessing live dynamic data, `Real Time Database` is the best option. It will solve man of your use cases and save lot of costs as well. https://stackoverflow.com/questions/44535831/firebase-realtime-database-structure-in-chat-app – Codex Sep 24 '20 at 14:22
  • @FrankvanPuffelen exactly. documentation only talks about retrofit network calls and local databases.Nothing about online data bases – saket Sep 24 '20 at 14:54
  • @FrankvanPuffelen Your advice helped move this along. I encourage you to post it as an answer. – MrTech Sep 25 '20 at 18:07

0 Answers0