Skip to content

Commit 6d24e39

Browse files
committed
add preference streaming
1 parent 7316bd3 commit 6d24e39

File tree

1 file changed

+47
-1
lines changed
  • android/src/main/java/expo/modules/xmtpreactnativesdk

1 file changed

+47
-1
lines changed

android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.xmtp.android.library.Conversation
4242
import org.xmtp.android.library.Conversations.*
4343
import org.xmtp.android.library.EntryType
4444
import org.xmtp.android.library.PreEventCallback
45+
import org.xmtp.android.library.PreferenceType
4546
import org.xmtp.android.library.SendOptions
4647
import org.xmtp.android.library.SigningKey
4748
import org.xmtp.android.library.WalletType
@@ -208,6 +209,7 @@ class XMTPModule : Module() {
208209
"message",
209210
"conversationMessage",
210211
"consent",
212+
"preferences",
211213
)
212214

213215
Function("address") { installationId: String ->
@@ -1262,6 +1264,12 @@ class XMTPModule : Module() {
12621264
}
12631265
}
12641266

1267+
Function("subscribeToPreferenceUpdates") { installationId: String ->
1268+
logV("subscribeToPreferenceUpdates")
1269+
1270+
subscribeToPreferenceUpdates(installationId = installationId)
1271+
}
1272+
12651273
Function("subscribeToConsent") { installationId: String ->
12661274
logV("subscribeToConsent")
12671275

@@ -1289,6 +1297,11 @@ class XMTPModule : Module() {
12891297
}
12901298
}
12911299

1300+
Function("unsubscribeFromPreferenceUpdates") { installationId: String ->
1301+
logV("unsubscribeFromPreferenceUpdates")
1302+
subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1303+
}
1304+
12921305
Function("unsubscribeFromConsent") { installationId: String ->
12931306
logV("unsubscribeFromConsent")
12941307
subscriptions[getConsentKey(installationId)]?.cancel()
@@ -1417,6 +1430,35 @@ class XMTPModule : Module() {
14171430
}
14181431
}
14191432

1433+
private fun preferenceTypeToString(type: PreferenceType): String {
1434+
return when (type) {
1435+
PreferenceType.HMAC_KEYS -> "hmac_keys"
1436+
}
1437+
}
1438+
1439+
private fun subscribeToPreferenceUpdates(installationId: String) {
1440+
val client = clients[installationId] ?: throw XMTPException("No client")
1441+
1442+
subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1443+
subscriptions[getPreferenceUpdatesKey(installationId)] =
1444+
CoroutineScope(Dispatchers.IO).launch {
1445+
try {
1446+
client.preferences.streamPreferenceUpdates().collect { type ->
1447+
sendEvent(
1448+
"preferences",
1449+
mapOf(
1450+
"installationId" to installationId,
1451+
"preferenceType" to preferenceTypeToString(type)
1452+
)
1453+
)
1454+
}
1455+
} catch (e: Exception) {
1456+
Log.e("XMTPModule", "Error in preference subscription: $e")
1457+
subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1458+
}
1459+
}
1460+
}
1461+
14201462
private fun subscribeToConsent(installationId: String) {
14211463
val client = clients[installationId] ?: throw XMTPException("No client")
14221464

@@ -1434,7 +1476,7 @@ class XMTPModule : Module() {
14341476
)
14351477
}
14361478
} catch (e: Exception) {
1437-
Log.e("XMTPModule", "Error in group subscription: $e")
1479+
Log.e("XMTPModule", "Error in consent subscription: $e")
14381480
subscriptions[getConsentKey(installationId)]?.cancel()
14391481
}
14401482
}
@@ -1513,6 +1555,10 @@ class XMTPModule : Module() {
15131555
}
15141556
}
15151557

1558+
private fun getPreferenceUpdatesKey(installationId: String): String {
1559+
return "preferences:$installationId"
1560+
}
1561+
15161562
private fun getConsentKey(installationId: String): String {
15171563
return "consent:$installationId"
15181564
}

0 commit comments

Comments
 (0)