From dec2192295fc7471e852a7e9cda07ee86b7c9ca7 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 29 Jan 2024 20:45:31 -0800 Subject: [PATCH] add pagination to messages --- .../org/xmtp/android/library/Conversation.kt | 7 ++++++- .../java/org/xmtp/android/library/Group.kt | 21 +++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/org/xmtp/android/library/Conversation.kt b/library/src/main/java/org/xmtp/android/library/Conversation.kt index 2f9c901af..ce1a4037a 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversation.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversation.kt @@ -254,7 +254,12 @@ sealed class Conversation { ) is Group -> { - group.messages() + group.messages( + limit = limit, + before = before, + after = after, + direction = direction, + ) } } } diff --git a/library/src/main/java/org/xmtp/android/library/Group.kt b/library/src/main/java/org/xmtp/android/library/Group.kt index 5d269f056..e399a6d7e 100644 --- a/library/src/main/java/org/xmtp/android/library/Group.kt +++ b/library/src/main/java/org/xmtp/android/library/Group.kt @@ -79,18 +79,27 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) { libXMTPGroup.sync() } - fun messages(): List { + fun messages( + limit: Int? = null, + before: Date? = null, + after: Date? = null, + direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING, + ): List { return runBlocking { libXMTPGroup.sync() - libXMTPGroup.findMessages( + val messages = libXMTPGroup.findMessages( opts = FfiListMessagesOptions( - sentBeforeNs = null, - sentAfterNs = null, - limit = null + sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS), + sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS), + limit = limit?.toLong() ) ).map { Message(client, it).decode() - }.reversed() + } + when (direction) { + MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING -> messages + else -> messages.reversed() + } } }