Skip to content

Commit

Permalink
Add new methods to query replies olderToNewer or newerToOlder
Browse files Browse the repository at this point in the history
  • Loading branch information
JcMinarro committed May 24, 2024
1 parent c250bc7 commit 57f9751
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public fun ChatClient.watchChannelAsState(
*
* @param messageId The ID of the original message the replies were made to.
* @param messageLimit The number of messages that will be initially loaded.
* @param olderToNewer The flag that determines the order of the messages.
* @param coroutineScope The [CoroutineScope] used for executing the request.
*
* @return [ThreadState]
Expand All @@ -158,10 +159,13 @@ public fun ChatClient.watchChannelAsState(
public suspend fun ChatClient.getRepliesAsState(
messageId: String,
messageLimit: Int,
olderToNewer: Boolean,
coroutineScope: CoroutineScope = CoroutineScope(DispatcherProvider.IO),
): ThreadState {
StreamLog.d(TAG) { "[getRepliesAsState] messageId: $messageId, messageLimit: $messageLimit" }
return requestsAsState(coroutineScope).getReplies(messageId, messageLimit)
StreamLog.d(TAG) {
"[getRepliesAsState] messageId: $messageId, messageLimit: $messageLimit, olderToNewer: $olderToNewer"
}
return requestsAsState(coroutineScope).getReplies(messageId, messageLimit, olderToNewer)
}

/**
Expand All @@ -172,6 +176,7 @@ public suspend fun ChatClient.getRepliesAsState(
*
* @param messageId The ID of the original message the replies were made to.
* @param messageLimit The number of messages that will be initially loaded.
* @param olderToNewer The flag that determines the order of the messages.
* @param coroutineScope The [CoroutineScope] used for executing the request.
*
* @return [ThreadState] wrapped inside a [Call].
Expand All @@ -180,10 +185,11 @@ public suspend fun ChatClient.getRepliesAsState(
public suspend fun ChatClient.awaitRepliesAsState(
messageId: String,
messageLimit: Int,
olderToNewer: Boolean,
): ThreadState {
StreamLog.d(TAG) { "[awaitRepliesAsState] messageId: $messageId, messageLimit: $messageLimit" }
return coroutineScope {
requestsAsState(scope = this).awaitReplies(messageId, messageLimit)
requestsAsState(scope = this).awaitReplies(messageId, messageLimit, olderToNewer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public interface ThreadState {
/** If we are currently loading messages. */
public val loading: StateFlow<Boolean>

/** If we are currently loading older messages. */
public val loadingOlderMessages: StateFlow<Boolean>

/** If we've reached the earliest point in this thread. */
public val endOfOlderMessages: StateFlow<Boolean>

/** The oldest message available in this thread state. It's null when we haven't loaded any messages in thread yet. */
/** If we've reached the latest point in this thread. */
public val endOfNewerMessages: StateFlow<Boolean>

/** The oldest message available in this thread state.
* It's null when we haven't loaded any messages in thread yet. */
public val oldestInThread: StateFlow<Message?>

/** The newest message available in this thread state.
* It's null when we haven't loaded any messages in thread yet. */
public val newestInThread: StateFlow<Message?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,26 @@ internal class ThreadMutableState(
) : ThreadState {
private var _messages: MutableStateFlow<Map<String, Message>>? = MutableStateFlow(emptyMap())
private var _loading: MutableStateFlow<Boolean>? = MutableStateFlow(false)
private var _loadingOlderMessages: MutableStateFlow<Boolean>? = MutableStateFlow(false)
private var _endOfOlderMessages: MutableStateFlow<Boolean>? = MutableStateFlow(false)
private var _endOfNewerMessages: MutableStateFlow<Boolean>? = MutableStateFlow(false)
private var _oldestInThread: MutableStateFlow<Message?>? = MutableStateFlow(null)
private var _newestInThread: MutableStateFlow<Message?>? = MutableStateFlow(null)

val rawMessage: StateFlow<Map<String, Message>> = _messages!!
override val messages: StateFlow<List<Message>> = rawMessage
.map { it.values }
.map { threadMessages -> threadMessages.sortedBy { m -> m.createdAt ?: m.createdLocallyAt } }
.stateIn(scope, SharingStarted.Eagerly, emptyList())
override val loading: StateFlow<Boolean> = _loading!!
override val loadingOlderMessages: StateFlow<Boolean> = _loadingOlderMessages!!
override val endOfOlderMessages: StateFlow<Boolean> = _endOfOlderMessages!!
override val endOfNewerMessages: StateFlow<Boolean> = _endOfNewerMessages!!
override val oldestInThread: StateFlow<Message?> = _oldestInThread!!
override val newestInThread: StateFlow<Message?> = _newestInThread!!

fun setLoading(isLoading: Boolean) {
_loading?.value = isLoading
}

fun setLoadingOlderMessages(isLoading: Boolean) {
_loadingOlderMessages?.value = isLoading
}

fun setEndOfOlderMessages(isEnd: Boolean) {
_endOfOlderMessages?.value = isEnd
}
Expand All @@ -61,6 +59,14 @@ internal class ThreadMutableState(
_oldestInThread?.value = message
}

fun setEndOfNewerMessages(isEnd: Boolean) {
_endOfNewerMessages?.value = isEnd
}

fun setNewestInThread(message: Message?) {
_newestInThread?.value = message
}

fun deleteMessage(message: Message) {
_messages?.apply { value -= message.id }
}
Expand All @@ -72,7 +78,6 @@ internal class ThreadMutableState(
fun destroy() {
_messages = null
_loading = null
_loadingOlderMessages = null
_endOfOlderMessages = null
_oldestInThread = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import io.getstream.chat.android.client.api.models.QueryChannelRequest
import io.getstream.chat.android.client.api.models.QueryChannelsRequest
import io.getstream.chat.android.client.channel.state.ChannelState
import io.getstream.chat.android.client.extensions.cidToTypeAndId
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.state.event.handler.chat.factory.ChatEventHandlerFactory
import io.getstream.chat.android.state.extensions.state
import io.getstream.chat.android.state.model.querychannels.pagination.internal.QueryChannelPaginationRequest
import io.getstream.chat.android.state.plugin.state.StateRegistry
import io.getstream.chat.android.state.plugin.state.channel.thread.ThreadState
import io.getstream.chat.android.state.plugin.state.querychannels.QueryChannelsState
import io.getstream.log.taggedLogger
import io.getstream.result.call.Call
import io.getstream.result.call.launch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
Expand Down Expand Up @@ -93,9 +95,13 @@ internal class ChatClientStateCalls(
}

/** Reference request of the get thread replies query. */
internal suspend fun getReplies(messageId: String, messageLimit: Int): ThreadState {
internal suspend fun getReplies(
messageId: String,
messageLimit: Int,
olderToNewer: Boolean,
): ThreadState {
logger.d { "[getReplies] messageId: $messageId, messageLimit: $messageLimit" }
chatClient.getReplies(messageId, messageLimit).launch(scope)
repliesCall(olderToNewer, messageId, messageLimit).launch(scope)
return deferredState
.await()
.thread(messageId)
Expand All @@ -111,14 +117,28 @@ internal class ChatClientStateCalls(
*
* @param messageId The id of the message we want to get replies for.
* @param messageLimit The upper limit of how many replies should be fetched.
* @param olderToNewer If true, replies will be fetched from older to newer, otherwise from newer to older.
*
* @return The replies in the form of [ThreadState].
*/
internal suspend fun awaitReplies(messageId: String, messageLimit: Int): ThreadState {
internal suspend fun awaitReplies(
messageId: String,
messageLimit: Int,
olderToNewer: Boolean,
): ThreadState {
logger.d { "[awaitReplies] messageId: $messageId, messageLimit: $messageLimit" }
chatClient.getReplies(messageId, messageLimit).await()
repliesCall(olderToNewer, messageId, messageLimit).await()
return deferredState
.await()
.thread(messageId)
}

private fun repliesCall(
olderToNewer: Boolean,
messageId: String,
messageLimit: Int,
): Call<List<Message>> = when (olderToNewer) {
true -> chatClient.getNewerReplies(messageId, messageLimit)
false -> chatClient.getReplies(messageId, messageLimit)
}
}

0 comments on commit 57f9751

Please sign in to comment.