Skip to content

Commit 701a261

Browse files
committed
feat: add function to delete database
1 parent e1eec1d commit 701a261

File tree

1 file changed

+21
-11
lines changed
  • library/src/main/java/org/xmtp/android/library

1 file changed

+21
-11
lines changed

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Client() {
8282
lateinit var apiClient: ApiClient
8383
lateinit var contacts: Contacts
8484
lateinit var conversations: Conversations
85+
var dbPath: String = ""
8586
var logger: XMTPLogger = XMTPLogger()
8687
val libXMTPVersion: String = getVersionInfo()
8788
private var libXMTPClient: FfiXmtpClient? = null
@@ -160,6 +161,7 @@ class Client() {
160161
privateKeyBundleV1: PrivateKeyBundleV1,
161162
apiClient: ApiClient,
162163
libXMTPClient: FfiXmtpClient? = null,
164+
dbPath: String = ""
163165
) : this() {
164166
this.address = address
165167
this.privateKeyBundleV1 = privateKeyBundleV1
@@ -168,6 +170,7 @@ class Client() {
168170
this.libXMTPClient = libXMTPClient
169171
this.conversations =
170172
Conversations(client = this, libXMTPConversations = libXMTPClient?.conversations())
173+
this.dbPath = dbPath
171174
}
172175

173176
fun buildFrom(
@@ -179,7 +182,7 @@ class Client() {
179182
val clientOptions = options ?: ClientOptions()
180183
val apiClient =
181184
GRPCApiClient(environment = clientOptions.api.env, secure = clientOptions.api.isSecure)
182-
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
185+
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
183186
runBlocking {
184187
ffiXmtpClient(
185188
options,
@@ -190,13 +193,14 @@ class Client() {
190193
address
191194
)
192195
}
193-
} else null
196+
} else Pair(null, " ")
194197

195198
return Client(
196199
address = address,
197200
privateKeyBundleV1 = bundle,
198201
apiClient = apiClient,
199-
libXMTPClient = v3Client
202+
libXMTPClient = v3Client,
203+
dbPath = dbPath
200204
)
201205
}
202206

@@ -226,7 +230,7 @@ class Client() {
226230
apiClient,
227231
options
228232
)
229-
val libXMTPClient: FfiXmtpClient? =
233+
val (libXMTPClient, dbPath) =
230234
ffiXmtpClient(
231235
options,
232236
account,
@@ -236,7 +240,7 @@ class Client() {
236240
account.address
237241
)
238242
val client =
239-
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient)
243+
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient, dbPath)
240244
client.ensureUserContactPublished()
241245
client
242246
} catch (e: java.lang.Exception) {
@@ -261,7 +265,7 @@ class Client() {
261265
val newOptions = options ?: ClientOptions()
262266
val apiClient =
263267
GRPCApiClient(environment = newOptions.api.env, secure = newOptions.api.isSecure)
264-
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
268+
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
265269
runBlocking {
266270
ffiXmtpClient(
267271
options,
@@ -272,13 +276,14 @@ class Client() {
272276
address
273277
)
274278
}
275-
} else null
279+
} else Pair(null, "")
276280

277281
return Client(
278282
address = address,
279283
privateKeyBundleV1 = v1Bundle,
280284
apiClient = apiClient,
281-
libXMTPClient = v3Client
285+
libXMTPClient = v3Client,
286+
dbPath = dbPath
282287
)
283288
}
284289

@@ -293,12 +298,13 @@ class Client() {
293298
privateKeyBundleV1: PrivateKeyBundleV1,
294299
legacyIdentitySource: LegacyIdentitySource,
295300
accountAddress: String,
296-
): FfiXmtpClient? {
301+
): Pair<FfiXmtpClient?, String> {
302+
var dbPath = ""
297303
val v3Client: FfiXmtpClient? =
298304
if (isAlphaMlsEnabled(options)) {
299305
val alias = "xmtp-${options!!.api.env}-${accountAddress.lowercase()}"
300306

301-
val dbPath = if (options.dbPath == null) {
307+
dbPath = if (options.dbPath == null) {
302308
val dbDir = File(appContext?.filesDir?.absolutePath, "xmtp_db")
303309
dbDir.mkdir()
304310
dbDir.absolutePath + "/$alias.db3"
@@ -364,7 +370,7 @@ class Client() {
364370
}
365371
}
366372
Log.i(TAG, "LibXMTP $libXMTPVersion")
367-
return v3Client
373+
return Pair(v3Client, dbPath)
368374
}
369375

370376
/**
@@ -586,6 +592,10 @@ class Client() {
586592
return !statuses.contains(false)
587593
}
588594

595+
fun deleteLocalDatabase() {
596+
File(dbPath).delete()
597+
}
598+
589599
val privateKeyBundle: PrivateKeyBundle
590600
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)
591601

0 commit comments

Comments
 (0)