@@ -3,14 +3,17 @@ package org.xmtp.android.library
3
3
import android.util.Log
4
4
import io.grpc.StatusException
5
5
import kotlinx.coroutines.CancellationException
6
+ import kotlinx.coroutines.channels.awaitClose
6
7
import kotlinx.coroutines.coroutineScope
7
8
import kotlinx.coroutines.flow.Flow
8
9
import kotlinx.coroutines.flow.MutableStateFlow
10
+ import kotlinx.coroutines.flow.callbackFlow
9
11
import kotlinx.coroutines.flow.flow
10
12
import kotlinx.coroutines.launch
11
13
import kotlinx.coroutines.runBlocking
12
14
import org.xmtp.android.library.GRPCApiClient.Companion.makeQueryRequest
13
15
import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
16
+ import org.xmtp.android.library.libxmtp.Message
14
17
import org.xmtp.android.library.messages.DecryptedMessage
15
18
import org.xmtp.android.library.messages.Envelope
16
19
import org.xmtp.android.library.messages.EnvelopeBuilder
@@ -41,6 +44,9 @@ import uniffi.xmtpv3.FfiConversationCallback
41
44
import uniffi.xmtpv3.FfiConversations
42
45
import uniffi.xmtpv3.FfiGroup
43
46
import uniffi.xmtpv3.FfiListConversationsOptions
47
+ import uniffi.xmtpv3.FfiMessage
48
+ import uniffi.xmtpv3.FfiMessageCallback
49
+ import uniffi.xmtpv3.FfiStreamCloser
44
50
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.GroupEmitter
45
51
import java.util.Date
46
52
import kotlin.time.Duration.Companion.nanoseconds
@@ -93,6 +99,7 @@ data class Conversations(
93
99
),
94
100
)
95
101
}
102
+
96
103
fun newGroup (accountAddresses : List <String >): Group {
97
104
if (accountAddresses.isEmpty()) {
98
105
throw XMTPException (" Cannot start an empty group chat." )
@@ -479,56 +486,54 @@ data class Conversations(
479
486
* of the information of those conversations according to the topics
480
487
* @return Stream of data information for the conversations
481
488
*/
482
- fun stream (includeGroups : Boolean = false): Flow <Conversation > = flow {
489
+ fun stream (includeGroups : Boolean = false): Flow <Conversation > = callbackFlow {
490
+ var stream: FfiStreamCloser ? = null
483
491
if (includeGroups) {
484
- val groupEmitter = GroupEmitter ()
485
-
486
- coroutineScope {
487
- launch {
488
- groupEmitter.groups.collect { group ->
489
- emit(Conversation .Group (Group (client, group)))
490
- }
492
+ val groupCallback = object : FfiConversationCallback {
493
+ override fun onConversation (conversation : FfiGroup ) {
494
+ Log .e(" LOPI" , " callback called" )
495
+ trySend(Conversation .Group (Group (client, conversation)))
491
496
}
492
497
}
493
498
494
- libXMTPConversations?.stream(groupEmitter.callback)
499
+ Log .e(" LOPI" , " starting stream ${libXMTPConversations.toString()} " )
500
+ stream = libXMTPConversations?.stream(groupCallback)
495
501
}
496
502
497
503
val streamedConversationTopics: MutableSet <String > = mutableSetOf ()
498
504
client.subscribeTopic(
499
505
listOf (Topic .userIntro(client.address), Topic .userInvite(client.address)),
500
506
).collect { envelope ->
501
-
502
507
if (envelope.contentTopic == Topic .userIntro(client.address).description) {
503
508
val conversationV1 = fromIntro(envelope = envelope)
504
509
if (! streamedConversationTopics.contains(conversationV1.topic)) {
505
510
streamedConversationTopics.add(conversationV1.topic)
506
- emit (conversationV1)
511
+ send (conversationV1)
507
512
}
508
513
}
509
514
510
515
if (envelope.contentTopic == Topic .userInvite(client.address).description) {
511
516
val conversationV2 = fromInvite(envelope = envelope)
512
517
if (! streamedConversationTopics.contains(conversationV2.topic)) {
513
518
streamedConversationTopics.add(conversationV2.topic)
514
- emit (conversationV2)
519
+ send (conversationV2)
515
520
}
516
521
}
517
522
}
523
+ awaitClose { stream?.end() }
518
524
}
519
525
520
- fun streamGroups (): Flow <Group > = flow {
521
- val groupEmitter = GroupEmitter ()
522
-
523
- coroutineScope {
524
- launch {
525
- groupEmitter.groups.collect { group ->
526
- emit(Group (client, group))
527
- }
526
+ fun streamGroups (): Flow <Group > = callbackFlow {
527
+ val groupCallback = object : FfiConversationCallback {
528
+ override fun onConversation (conversation : FfiGroup ) {
529
+ Log .e(" LOPI" , " callback called" )
530
+ trySend(Group (client, conversation))
528
531
}
529
532
}
530
533
531
- libXMTPConversations?.stream(groupEmitter.callback)
534
+ Log .e(" LOPI" , " starting stream ${libXMTPConversations.toString()} " )
535
+ val stream = libXMTPConversations?.stream(groupCallback)
536
+ awaitClose { stream?.end() }
532
537
}
533
538
534
539
/* *
0 commit comments