@@ -37,6 +37,7 @@ import com.ably.chat.ChatClient
37
37
import com.ably.chat.Message
38
38
import com.ably.chat.RealtimeClient
39
39
import com.ably.chat.SendMessageParams
40
+ import com.ably.chat.SendReactionParams
40
41
import com.ably.chat.example.ui.theme.AblyChatExampleTheme
41
42
import io.ably.lib.types.ClientOptions
42
43
import java.util.UUID
@@ -72,17 +73,30 @@ class MainActivity : ComponentActivity() {
72
73
}
73
74
}
74
75
76
+ @SuppressWarnings(" LongMethod" )
75
77
@Composable
76
78
fun Chat (chatClient : ChatClient , modifier : Modifier = Modifier ) {
77
79
var messageText by remember { mutableStateOf(TextFieldValue (" " )) }
78
80
var sending by remember { mutableStateOf(false ) }
79
81
var messages by remember { mutableStateOf(listOf<Message >()) }
80
82
val listState = rememberLazyListState()
81
83
val coroutineScope = rememberCoroutineScope()
84
+ var receivedReactions by remember { mutableStateOf<List <String >>(listOf ()) }
82
85
83
86
val roomId = " my-room"
84
87
val room = chatClient.rooms.get(roomId)
85
88
89
+ DisposableEffect (Unit ) {
90
+ coroutineScope.launch {
91
+ room.attach()
92
+ }
93
+ onDispose {
94
+ coroutineScope.launch {
95
+ room.detach()
96
+ }
97
+ }
98
+ }
99
+
86
100
DisposableEffect (Unit ) {
87
101
val subscription = room.messages.subscribe {
88
102
messages + = it.message
@@ -101,6 +115,16 @@ fun Chat(chatClient: ChatClient, modifier: Modifier = Modifier) {
101
115
}
102
116
}
103
117
118
+ DisposableEffect (Unit ) {
119
+ val subscription = room.reactions.subscribe {
120
+ receivedReactions + = it.type
121
+ }
122
+
123
+ onDispose {
124
+ subscription.unsubscribe()
125
+ }
126
+ }
127
+
104
128
Column (
105
129
modifier = modifier.fillMaxSize(),
106
130
verticalArrangement = Arrangement .SpaceBetween ,
@@ -119,17 +143,26 @@ fun Chat(chatClient: ChatClient, modifier: Modifier = Modifier) {
119
143
sending = sending,
120
144
messageInput = messageText,
121
145
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))
133
166
}
134
167
}
135
168
}
@@ -164,6 +197,7 @@ fun ChatInputField(
164
197
messageInput : TextFieldValue ,
165
198
onMessageChange : (TextFieldValue ) -> Unit ,
166
199
onSendClick : () -> Unit ,
200
+ onReactionClick : () -> Unit ,
167
201
) {
168
202
Row (
169
203
modifier = Modifier
@@ -181,8 +215,14 @@ fun ChatInputField(
181
215
.background(Color .White ),
182
216
placeholder = { Text (" Type a message..." ) },
183
217
)
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
+ }
186
226
}
187
227
}
188
228
}
@@ -214,6 +254,7 @@ fun ChatInputPreview() {
214
254
messageInput = TextFieldValue (" " ),
215
255
onMessageChange = {},
216
256
onSendClick = {},
257
+ onReactionClick = {},
217
258
)
218
259
}
219
260
}
0 commit comments