Skip to content

Commit 4861812

Browse files
committed
[ECO-4944] feat: update example app
1 parent f519ca9 commit 4861812

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

chat-android/src/main/java/com/ably/chat/RoomReactions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ internal class DefaultRoomReactions(
117117
// (CHA-ER3a) Reactions are sent on the channel using a message in a particular format - see spec for format.
118118
override suspend fun send(params: SendReactionParams) {
119119
val pubSubMessage = PubSubMessage().apply {
120+
name = RoomReactionEventType.Reaction.eventName
120121
data = JsonObject().apply {
121122
addProperty("type", params.type)
122123
params.metadata?.let { add("metadata", it.toJson()) }
@@ -145,7 +146,7 @@ internal class DefaultRoomReactions(
145146
createdAt = pubSubMessage.timestamp,
146147
clientId = pubSubMessage.clientId,
147148
metadata = data.get("metadata")?.toMap() ?: mapOf(),
148-
headers = pubSubMessage.extras.asJsonObject().get("headers")?.toMap() ?: mapOf(),
149+
headers = pubSubMessage.extras?.asJsonObject()?.get("headers")?.toMap() ?: mapOf(),
149150
isSelf = pubSubMessage.clientId == clientId,
150151
)
151152
listener.onReaction(reaction)

example/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dependencies {
6767
implementation(libs.androidx.ui.graphics)
6868
implementation(libs.androidx.ui.tooling.preview)
6969
implementation(libs.androidx.material3)
70+
implementation(libs.konfetti.compose)
7071
testImplementation(libs.junit)
7172
androidTestImplementation(libs.androidx.junit)
7273
androidTestImplementation(libs.androidx.espresso.core)

example/src/main/java/com/ably/chat/example/MainActivity.kt

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.ably.chat.ChatClient
3737
import com.ably.chat.Message
3838
import com.ably.chat.RealtimeClient
3939
import com.ably.chat.SendMessageParams
40+
import com.ably.chat.SendReactionParams
4041
import com.ably.chat.example.ui.theme.AblyChatExampleTheme
4142
import io.ably.lib.types.ClientOptions
4243
import java.util.UUID
@@ -72,17 +73,30 @@ class MainActivity : ComponentActivity() {
7273
}
7374
}
7475

76+
@SuppressWarnings("LongMethod")
7577
@Composable
7678
fun 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
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ably = "1.2.43"
77
junit = "4.13.2"
88
agp = "8.5.2"
99
detekt = "1.23.6"
10+
konfetti-compose = "2.0.4"
1011
kotlin = "2.0.10"
1112
androidx-test = "1.6.1"
1213
androidx-junit = "1.2.1"
@@ -43,6 +44,7 @@ androidx-material3 = { group = "androidx.compose.material3", name = "material3"
4344

4445
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
4546

47+
konfetti-compose = { module = "nl.dionsegijn:konfetti-compose", version.ref = "konfetti-compose" }
4648
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
4749

4850
coroutine-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutine" }

0 commit comments

Comments
 (0)