-
Notifications
You must be signed in to change notification settings - Fork 46
chat: add Jetpack Compose support to documentation #2991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3d7feab
e6875ba
0293510
37360d5
1abfd27
f363830
838a06c
4a5066e
4e71d12
d086414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,10 @@ Use the <If lang="javascript">[`status`](https://sdk.ably.com/builds/ably/ably-c | |
| Use the [`currentStatus`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-react.UseChatConnectionResponse.html#currentStatus) property returned in the response of the [`useChatConnection`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat-react.useChatConnection.html) hook to check which status a connection is currently in: | ||
| </If> | ||
|
|
||
| <If lang="jetpack"> | ||
| Use the [`collectAsStatus()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/jetpack/chat-extensions-compose/com.ably.chat.extensions.compose/collect-as-status.html) composable function to observe the connection status as a State: | ||
| </If> | ||
|
|
||
| <Code> | ||
| ```javascript | ||
| const connectionStatus = chatClient.connection.status; | ||
|
|
@@ -56,6 +60,17 @@ let status = chatClient.connection.status | |
| ```kotlin | ||
| val connectionStatus = chatClient.connection.status | ||
| ``` | ||
|
|
||
| ```jetpack | ||
| import com.ably.chat.extensions.compose.collectAsStatus | ||
|
|
||
| @Composable | ||
| fun MyComponent(chatClient: ChatClient) { | ||
| val connectionStatus by chatClient.connection.collectAsStatus() | ||
|
|
||
| Text("Connection status: $connectionStatus") | ||
| } | ||
| ``` | ||
|
Comment on lines
+64
to
+73
|
||
| </Code> | ||
|
|
||
| <If lang="react"> | ||
|
|
@@ -84,6 +99,10 @@ Listeners can also be registered to monitor the changes in connection status. An | |
| Use the <If lang="javascript">[`connection.onStatusChange()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Connection.html#onStatusChange)</If><If lang="swift">[`connection.onStatusChange()`](https://sdk.ably.com/builds/ably/ably-chat-swift/main/AblyChat/documentation/ablychat/connection/onstatuschange%28%29-76t7)</If><If lang="kotlin">[`connection.status.onStatusChange()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-connection/on-status-change.html)</If> method to register a listener for status change updates: | ||
| </If> | ||
|
|
||
| <If lang="jetpack"> | ||
| In Jetpack Compose, you can use [`collectAsStatus()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/jetpack/chat-extensions-compose/com.ably.chat.extensions.compose/collect-as-status.html) to observe status changes reactively: | ||
| </If> | ||
|
|
||
| <Code> | ||
| ```javascript | ||
| const { off } = chatClient.connection.onStatusChange((change) => console.log(change)); | ||
|
|
@@ -114,6 +133,24 @@ val (off) = chatClient.connection.onStatusChange { statusChange: ConnectionStatu | |
| println(statusChange.toString()) | ||
| } | ||
| ``` | ||
|
|
||
| ```jetpack | ||
| import androidx.compose.material.* | ||
| import androidx.compose.runtime.* | ||
| import com.ably.chat.ChatClient | ||
| import com.ably.chat.extensions.compose.collectAsStatus | ||
|
|
||
| @Composable | ||
| fun MyComponent(chatClient: ChatClient) { | ||
| val connectionStatus by chatClient.connection.collectAsStatus() | ||
|
|
||
| LaunchedEffect(connectionStatus) { | ||
| println("Connection status changed to: $connectionStatus") | ||
| } | ||
|
|
||
| Text("Connection status: $connectionStatus") | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| <If lang="javascript,kotlin"> | ||
|
|
@@ -148,6 +185,10 @@ The Chat SDK provides an `onDiscontinuity()` handler exposed via the Room object | |
| Any hooks that take an optional listener to monitor their events, such as typing indicator events in the `useTyping` hook, can also register a listener to be notified of, and handle, periods of discontinuity. | ||
| </If> | ||
|
|
||
| <If lang="jetpack"> | ||
| Use the [`discontinuityAsFlow()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/discontinuity-as-flow.html) extension function to observe discontinuity events as a Flow in Jetpack Compose: | ||
| </If> | ||
|
|
||
| For example, for messages: | ||
|
|
||
| <Code> | ||
|
|
@@ -184,6 +225,22 @@ val (off) = room.onDiscontinuity { reason: ErrorInfo -> | |
| // Recover from the discontinuity | ||
| } | ||
| ``` | ||
|
|
||
| ```jetpack | ||
| import androidx.compose.runtime.* | ||
| import com.ably.chat.Room | ||
| import com.ably.chat.discontinuityAsFlow | ||
|
|
||
| @Composable | ||
| fun MyComponent(room: Room) { | ||
| LaunchedEffect(room) { | ||
| room.discontinuityAsFlow().collect { error -> | ||
| // Recover from the discontinuity | ||
| println("Discontinuity detected: $error") | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| <If lang="javascript,kotlin"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ The history feature enables users to retrieve messages that have been previously | |
| Use the <If lang="javascript">[`messages.history()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Messages.html#history)</If><If lang="swift">[`messages.history()`](https://sdk.ably.com/builds/ably/ably-chat-swift/main/AblyChat/documentation/ablychat/messages/history(withparams:))</If><If lang="kotlin">[`messages.history()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-messages/history.html)</If> method to retrieve messages that have been previously sent to a room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| </If> | ||
|
|
||
| <If lang="jetpack"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, we have only kept the lower one |
||
| Use the [`collectAsPagingMessagesState()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/jetpack/chat-extensions-compose/com.ably.chat.extensions.compose/collect-as-paging-messages-state.html) method to retrieve messages that have been previously sent to a room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| </If> | ||
|
|
||
| <If lang="react"> | ||
| Use the [`history()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-react.UseMessagesResponse.html#history) method available from the response of the `useMessages` hook to retrieve messages that have been previously sent to a room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| </If> | ||
|
|
@@ -71,6 +75,29 @@ while (historicalMessages.hasNext()) { | |
|
|
||
| println("End of messages") | ||
| ``` | ||
|
|
||
| ```jetpack | ||
| import androidx.compose.foundation.lazy.* | ||
| import androidx.compose.material.* | ||
| import androidx.compose.runtime.* | ||
| import com.ably.chat.OrderBy | ||
| import com.ably.chat.Room | ||
| import com.ably.chat.extensions.compose.collectAsPagingMessagesState | ||
|
|
||
| @Composable | ||
| fun HistoryComponent(room: Room) { | ||
| val pagingMessagesState by room.messages.collectAsPagingMessagesState( | ||
| orderBy = OrderBy.NewestFirst | ||
| ) | ||
|
|
||
| LazyColumn { | ||
| items(pagingMessagesState.messages.size) { index -> | ||
| val message = pagingMessagesState.messages[index] | ||
| Text("Message: ${message.text}") | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| The following optional parameters can be passed when retrieving previously sent messages: | ||
|
|
@@ -87,7 +114,11 @@ The following optional parameters can be passed when retrieving previously sent | |
| Users can also retrieve historical messages that were sent to a room before the point that they registered a listener by [subscribing](/docs/chat/rooms/messages#subscribe). The order of messages returned is from most recent, to oldest. This is useful for providing conversational context when a user first joins a room, or when they subsequently rejoin it later on. It also ensures that the message history they see is continuous, without any overlap of messages being returned between their subscription and their history call. | ||
|
|
||
| <If lang="javascript,swift,kotlin"> | ||
| Use the <If lang="javascript">[`historyBeforeSubscribe()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageSubscriptionResponse.html#historyBeforeSubscribe)</If><If lang="swift">[`historyBeforeSubscribe(withParams:)`](https://sdk.ably.com/builds/ably/ably-chat-swift/main/AblyChat/documentation/ablychat/messagesubscriptionresponse/historybeforesubscribe%28withparams%3A%29))</If><If lang="kotlin">[`getPreviousMessages()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-messages-subscription/get-previous-messages.html)</If> function returned as part of a [message subscription](/docs/chat/rooms/messages#subscribe) response to only retrieve messages that were received before the listener was subscribed to the room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| Use the <If lang="javascript">[`historyBeforeSubscribe()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageSubscriptionResponse.html#historyBeforeSubscribe)</If><If lang="swift">[`historyBeforeSubscribe(withParams:)`](https://sdk.ably.com/builds/ably/ably-chat-swift/main/AblyChat/documentation/ablychat/messagesubscriptionresponse/historybeforesubscribe%28withparams%3A%29))</If><If lang="kotlin">[`historyBeforeSubscribe()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-messages-subscription/history-before-subscribe.html)</If> function returned as part of a [message subscription](/docs/chat/rooms/messages#subscribe) response to only retrieve messages that were received before the listener was subscribed to the room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| </If> | ||
|
|
||
| <If lang="jetpack"> | ||
| Use the [`historyBeforeSubscribe()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-messages-subscription/history-before-subscribe.html) function returned as part of a [message subscription](/docs/chat/rooms/messages#subscribe) response to only retrieve messages that were received before the listener was subscribed to the room. This returns a paginated response, which can be queried further to retrieve the next set of messages. | ||
| </If> | ||
|
|
||
| <If lang="react"> | ||
|
|
@@ -167,6 +198,53 @@ while (historicalMessages.hasNext()) { | |
|
|
||
| println("End of messages") | ||
| ``` | ||
|
|
||
| ```jetpack | ||
| import androidx.compose.foundation.layout.* | ||
| import androidx.compose.material.* | ||
| import androidx.compose.runtime.* | ||
| import com.ably.chat.MessagesSubscription | ||
| import com.ably.chat.Room | ||
|
|
||
| @Composable | ||
| fun HistoryBeforeSubscribeComponent(room: Room) { | ||
| var messages by remember { mutableStateOf<List<String>>(emptyList()) } | ||
| var subscription by remember { mutableStateOf<MessagesSubscription?>(null) } | ||
|
|
||
| DisposableEffect(room) { | ||
| subscription = room.messages.subscribe { | ||
| println("New message received") | ||
| } | ||
|
|
||
| onDispose { | ||
| subscription?.unsubscribe() | ||
| } | ||
| } | ||
|
|
||
| LaunchedEffect(subscription) { | ||
| subscription?.let { sub -> | ||
| var historicalMessages = sub.historyBeforeSubscribe(limit = 50) | ||
| println(historicalMessages.items.toString()) | ||
| messages = historicalMessages.items.map { it.text } | ||
|
|
||
| while (historicalMessages.hasNext()) { | ||
| historicalMessages = historicalMessages.next() | ||
| println(historicalMessages.items.toString()) | ||
| messages = messages + historicalMessages.items.map { it.text } | ||
| } | ||
|
|
||
| println("End of messages") | ||
| } | ||
| } | ||
|
|
||
| // Display messages in UI | ||
| Column { | ||
| messages.forEach { message -> | ||
| Text(message) | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| </Code> | ||
|
|
||
| The following parameters can be passed when retrieving previously sent messages: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added these as a fixup so that it shows up in docs - could you source an icon for Jetpack so it shows in the language selector?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added alias to kotlin, so it will use kotlin icon for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created PR to add icon -> ably/ably-ui#1023