@@ -37,6 +37,7 @@ import com.ably.chat.ChatClient
3737import com.ably.chat.Message
3838import com.ably.chat.RealtimeClient
3939import com.ably.chat.SendMessageParams
40+ import com.ably.chat.SendReactionParams
4041import com.ably.chat.example.ui.theme.AblyChatExampleTheme
4142import io.ably.lib.types.ClientOptions
4243import java.util.UUID
@@ -72,17 +73,30 @@ class MainActivity : ComponentActivity() {
7273 }
7374}
7475
76+ @SuppressWarnings(" LongMethod" )
7577@Composable
7678fun Chat (chatClient : ChatClient , modifier : Modifier = Modifier ) {
7779 var messageText by remember { mutableStateOf(TextFieldValue (" " )) }
7880 var sending by remember { mutableStateOf(false ) }
7981 var messages by remember { mutableStateOf(listOf<Message >()) }
8082 val listState = rememberLazyListState()
8183 val coroutineScope = rememberCoroutineScope()
84+ var receivedReactions by remember { mutableStateOf<List <String >>(listOf ()) }
8285
8386 val roomId = " my-room"
8487 val room = chatClient.rooms.get(roomId)
8588
89+ DisposableEffect (Unit ) {
90+ coroutineScope.launch {
91+ room.attach()
92+ }
93+ onDispose {
94+ coroutineScope.launch {
95+ room.detach()
96+ }
97+ }
98+ }
99+
86100 DisposableEffect (Unit ) {
87101 val subscription = room.messages.subscribe {
88102 messages + = it.message
@@ -101,6 +115,16 @@ fun Chat(chatClient: ChatClient, modifier: Modifier = Modifier) {
101115 }
102116 }
103117
118+ DisposableEffect (Unit ) {
119+ val subscription = room.reactions.subscribe {
120+ receivedReactions + = it.type
121+ }
122+
123+ onDispose {
124+ subscription.unsubscribe()
125+ }
126+ }
127+
104128 Column (
105129 modifier = modifier.fillMaxSize(),
106130 verticalArrangement = Arrangement .SpaceBetween ,
@@ -119,17 +143,26 @@ fun Chat(chatClient: ChatClient, modifier: Modifier = Modifier) {
119143 sending = sending,
120144 messageInput = messageText,
121145 onMessageChange = { messageText = it },
122- ) {
123- sending = true
124- coroutineScope.launch {
125- room.messages.send(
126- SendMessageParams (
127- text = messageText.text,
128- ),
129- )
130- messageText = TextFieldValue (" " )
131- sending = false
132- }
146+ onSendClick = {
147+ sending = true
148+ coroutineScope.launch {
149+ room.messages.send(
150+ SendMessageParams (
151+ text = messageText.text,
152+ ),
153+ )
154+ messageText = TextFieldValue (" " )
155+ sending = false
156+ }
157+ },
158+ onReactionClick = {
159+ coroutineScope.launch {
160+ room.reactions.send(SendReactionParams (type = " \uD83D\uDC4D " ))
161+ }
162+ },
163+ )
164+ if (receivedReactions.isNotEmpty()) {
165+ Text (" Received reactions: ${receivedReactions.joinToString()} " , modifier = Modifier .padding(16 .dp))
133166 }
134167 }
135168}
@@ -164,6 +197,7 @@ fun ChatInputField(
164197 messageInput : TextFieldValue ,
165198 onMessageChange : (TextFieldValue ) -> Unit ,
166199 onSendClick : () -> Unit ,
200+ onReactionClick : () -> Unit ,
167201) {
168202 Row (
169203 modifier = Modifier
@@ -181,8 +215,14 @@ fun ChatInputField(
181215 .background(Color .White ),
182216 placeholder = { Text (" Type a message..." ) },
183217 )
184- Button (enabled = ! sending, onClick = onSendClick) {
185- Text (" Send" )
218+ if (messageInput.text.isNotEmpty()) {
219+ Button (enabled = ! sending, onClick = onSendClick) {
220+ Text (" Send" )
221+ }
222+ } else {
223+ Button (onClick = onReactionClick) {
224+ Text (" \uD83D\uDC4D " )
225+ }
186226 }
187227 }
188228}
@@ -214,6 +254,7 @@ fun ChatInputPreview() {
214254 messageInput = TextFieldValue (" " ),
215255 onMessageChange = {},
216256 onSendClick = {},
257+ onReactionClick = {},
217258 )
218259 }
219260}
0 commit comments