Skip to content

Commit 4b9d152

Browse files
authored
Remove client from serializable objects (#365)
* remove client from serializable objects * dont require context for static methods
1 parent b34ebb9 commit 4b9d152

File tree

6 files changed

+26
-49
lines changed

6 files changed

+26
-49
lines changed

library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt

-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class ClientTest {
8686
notOnNetwork.address,
8787
fixtures.bo.walletAddress
8888
),
89-
context,
9089
ClientOptions.Api(XMTPEnvironment.LOCAL, false)
9190
)
9291
}
@@ -109,7 +108,6 @@ class ClientTest {
109108
val states = runBlocking {
110109
Client.inboxStatesForInboxIds(
111110
listOf(fixtures.boClient.inboxId, fixtures.caroClient.inboxId),
112-
context,
113111
ClientOptions.Api(XMTPEnvironment.LOCAL, false)
114112
)
115113
}

library/src/main/java/org/xmtp/android/library/Client.kt

+11-24
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,16 @@ class Client() {
9191
codecRegistry.register(codec = codec)
9292
}
9393

94-
suspend fun <T> withFfiClient(
95-
appContext: Context,
94+
private suspend fun <T> withFfiClient(
9695
api: ClientOptions.Api,
9796
useClient: suspend (ffiClient: FfiXmtpClient) -> T,
9897
): T {
9998
val accountAddress = "0x0000000000000000000000000000000000000000"
10099
val inboxId = getOrCreateInboxId(api, accountAddress)
101-
val alias = "xmtp-${api.env}-$inboxId"
102-
103-
val directoryFile = File(appContext.filesDir.absolutePath, "xmtp_db")
104-
directoryFile.mkdir()
105-
val dbPath = directoryFile.absolutePath + "/$alias.db3"
106100

107101
val ffiClient = createClient(
108102
api = connectToApiBackend(api),
109-
db = dbPath,
103+
db = null,
110104
encryptionKey = null,
111105
accountAddress = accountAddress.lowercase(),
112106
inboxId = inboxId,
@@ -115,30 +109,23 @@ class Client() {
115109
historySyncUrl = null
116110
)
117111

118-
return try {
119-
useClient(ffiClient)
120-
} finally {
121-
ffiClient.releaseDbConnection()
122-
File(dbPath).delete()
123-
}
112+
return useClient(ffiClient)
124113
}
125114

126115
suspend fun inboxStatesForInboxIds(
127116
inboxIds: List<String>,
128-
appContext: Context,
129117
api: ClientOptions.Api,
130118
): List<InboxState> {
131-
return withFfiClient(appContext, api) { ffiClient ->
119+
return withFfiClient(api) { ffiClient ->
132120
ffiClient.addressesFromInboxId(true, inboxIds).map { InboxState(it) }
133121
}
134122
}
135123

136124
suspend fun canMessage(
137125
accountAddresses: List<String>,
138-
appContext: Context,
139126
api: ClientOptions.Api,
140127
): Map<String, Boolean> {
141-
return withFfiClient(appContext, api) { ffiClient ->
128+
return withFfiClient(api) { ffiClient ->
142129
ffiClient.canMessage(accountAddresses)
143130
}
144131
}
@@ -332,7 +319,7 @@ class Client() {
332319

333320
fun findGroup(groupId: String): Group? {
334321
return try {
335-
Group(this, ffiClient.conversation(groupId.hexToByteArray()))
322+
Group(this.inboxId, ffiClient.conversation(groupId.hexToByteArray()))
336323
} catch (e: Exception) {
337324
null
338325
}
@@ -342,8 +329,8 @@ class Client() {
342329
return try {
343330
val conversation = ffiClient.conversation(conversationId.hexToByteArray())
344331
when (conversation.conversationType()) {
345-
FfiConversationType.GROUP -> Conversation.Group(Group(this, conversation))
346-
FfiConversationType.DM -> Conversation.Dm(Dm(this, conversation))
332+
FfiConversationType.GROUP -> Conversation.Group(Group(this.inboxId, conversation))
333+
FfiConversationType.DM -> Conversation.Dm(Dm(this.inboxId, conversation))
347334
else -> null
348335
}
349336
} catch (e: Exception) {
@@ -358,8 +345,8 @@ class Client() {
358345
return try {
359346
val conversation = ffiClient.conversation(conversationId.hexToByteArray())
360347
when (conversation.conversationType()) {
361-
FfiConversationType.GROUP -> Conversation.Group(Group(this, conversation))
362-
FfiConversationType.DM -> Conversation.Dm(Dm(this, conversation))
348+
FfiConversationType.GROUP -> Conversation.Group(Group(this.inboxId, conversation))
349+
FfiConversationType.DM -> Conversation.Dm(Dm(this.inboxId, conversation))
363350
else -> null
364351
}
365352
} catch (e: Exception) {
@@ -369,7 +356,7 @@ class Client() {
369356

370357
fun findDmByInboxId(inboxId: String): Dm? {
371358
return try {
372-
Dm(this, ffiClient.dmConversation(inboxId))
359+
Dm(this.inboxId, ffiClient.dmConversation(inboxId))
373360
} catch (e: Exception) {
374361
null
375362
}

library/src/main/java/org/xmtp/android/library/Conversation.kt

-8
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ sealed class Conversation {
141141
}
142142
}
143143

144-
val client: Client
145-
get() {
146-
return when (this) {
147-
is Group -> group.client
148-
is Dm -> dm.client
149-
}
150-
}
151-
152144
fun streamMessages(): Flow<Message> {
153145
return when (this) {
154146
is Group -> group.streamMessages()

library/src/main/java/org/xmtp/android/library/Conversations.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ data class Conversations(
4242
suspend fun fromWelcome(envelopeBytes: ByteArray): Conversation {
4343
val conversation = ffiConversations.processStreamedWelcomeMessage(envelopeBytes)
4444
return when (conversation.conversationType()) {
45-
FfiConversationType.DM -> Conversation.Dm(Dm(client, conversation))
46-
else -> Conversation.Group(Group(client, conversation))
45+
FfiConversationType.DM -> Conversation.Dm(Dm(client.inboxId, conversation))
46+
else -> Conversation.Group(Group(client.inboxId, conversation))
4747
}
4848
}
4949

@@ -118,7 +118,7 @@ data class Conversations(
118118
customPermissionPolicySet = permissionsPolicySet
119119
)
120120
)
121-
return Group(client, group)
121+
return Group(client.inboxId, group)
122122
}
123123

124124
// Sync from the network the latest list of conversations
@@ -152,7 +152,7 @@ data class Conversations(
152152
var dm = client.findDmByAddress(peerAddress)
153153
if (dm == null) {
154154
val dmConversation = ffiConversations.createDm(peerAddress.lowercase())
155-
dm = Dm(client, dmConversation)
155+
dm = Dm(client.inboxId, dmConversation)
156156
}
157157
return dm
158158
}
@@ -174,7 +174,7 @@ data class Conversations(
174174
)
175175

176176
return ffiGroups.map {
177-
Group(client, it.conversation(), it.lastMessage())
177+
Group(client.inboxId, it.conversation(), it.lastMessage())
178178
}
179179
}
180180

@@ -195,7 +195,7 @@ data class Conversations(
195195
)
196196

197197
return ffiDms.map {
198-
Dm(client, it.conversation(), it.lastMessage())
198+
Dm(client.inboxId, it.conversation(), it.lastMessage())
199199
}
200200
}
201201

@@ -220,8 +220,8 @@ data class Conversations(
220220

221221
private suspend fun FfiConversationListItem.toConversation(): Conversation {
222222
return when (conversation().conversationType()) {
223-
FfiConversationType.DM -> Conversation.Dm(Dm(client, conversation(), lastMessage()))
224-
else -> Conversation.Group(Group(client, conversation(), lastMessage()))
223+
FfiConversationType.DM -> Conversation.Dm(Dm(client.inboxId, conversation(), lastMessage()))
224+
else -> Conversation.Group(Group(client.inboxId, conversation(), lastMessage()))
225225
}
226226
}
227227

@@ -234,13 +234,13 @@ data class Conversations(
234234
FfiConversationType.DM -> trySend(
235235
Conversation.Dm(
236236
Dm(
237-
client,
237+
client.inboxId,
238238
conversation
239239
)
240240
)
241241
)
242242

243-
else -> trySend(Conversation.Group(Group(client, conversation)))
243+
else -> trySend(Conversation.Group(Group(client.inboxId, conversation)))
244244
}
245245
}
246246
}

library/src/main/java/org/xmtp/android/library/Dm.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import uniffi.xmtpv3.FfiMessageCallback
2222
import uniffi.xmtpv3.FfiSubscribeException
2323
import java.util.Date
2424

25-
class Dm(val client: Client, private val libXMTPGroup: FfiConversation, private val ffiLastMessage: FfiMessage? = null) {
25+
class Dm(private val clientInboxId: String, private val libXMTPGroup: FfiConversation, private val ffiLastMessage: FfiMessage? = null) {
2626
val id: String
2727
get() = libXMTPGroup.id().toHex()
2828

@@ -150,7 +150,7 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation, private
150150
}
151151

152152
suspend fun isCreator(): Boolean {
153-
return metadata().creatorInboxId() == client.inboxId
153+
return metadata().creatorInboxId() == clientInboxId
154154
}
155155

156156
suspend fun members(): List<Member> {

library/src/main/java/org/xmtp/android/library/Group.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import uniffi.xmtpv3.FfiSubscribeException
2929
import java.util.Date
3030

3131
class Group(
32-
val client: Client,
32+
private val clientInboxId: String,
3333
private val libXMTPGroup: FfiConversation,
3434
private val ffiLastMessage: FfiMessage? = null,
3535
) {
@@ -193,7 +193,7 @@ class Group(
193193
}
194194

195195
suspend fun isCreator(): Boolean {
196-
return metadata().creatorInboxId() == client.inboxId
196+
return metadata().creatorInboxId() == clientInboxId
197197
}
198198

199199
suspend fun addMembers(addresses: List<String>) {
@@ -234,7 +234,7 @@ class Group(
234234

235235
suspend fun peerInboxIds(): List<String> {
236236
val ids = members().map { it.inboxId }.toMutableList()
237-
ids.remove(client.inboxId)
237+
ids.remove(clientInboxId)
238238
return ids
239239
}
240240

0 commit comments

Comments
 (0)