Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function to delete database #187

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Client() {
lateinit var apiClient: ApiClient
lateinit var contacts: Contacts
lateinit var conversations: Conversations
var dbPath: String = ""
var logger: XMTPLogger = XMTPLogger()
val libXMTPVersion: String = getVersionInfo()
private var libXMTPClient: FfiXmtpClient? = null
Expand Down Expand Up @@ -160,6 +161,7 @@ class Client() {
privateKeyBundleV1: PrivateKeyBundleV1,
apiClient: ApiClient,
libXMTPClient: FfiXmtpClient? = null,
dbPath: String = ""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to store the dbPath like this is it something the FfiXmtpClient could expose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep track of that and expose it. We have the info

) : this() {
this.address = address
this.privateKeyBundleV1 = privateKeyBundleV1
Expand All @@ -168,6 +170,7 @@ class Client() {
this.libXMTPClient = libXMTPClient
this.conversations =
Conversations(client = this, libXMTPConversations = libXMTPClient?.conversations())
this.dbPath = dbPath
}

fun buildFrom(
Expand All @@ -179,7 +182,7 @@ class Client() {
val clientOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(environment = clientOptions.api.env, secure = clientOptions.api.isSecure)
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
Expand All @@ -190,13 +193,14 @@ class Client() {
address
)
}
} else null
} else Pair(null, " ")

return Client(
address = address,
privateKeyBundleV1 = bundle,
apiClient = apiClient,
libXMTPClient = v3Client
libXMTPClient = v3Client,
dbPath = dbPath
)
}

Expand Down Expand Up @@ -226,7 +230,7 @@ class Client() {
apiClient,
options
)
val libXMTPClient: FfiXmtpClient? =
val (libXMTPClient, dbPath) =
ffiXmtpClient(
options,
account,
Expand All @@ -236,7 +240,7 @@ class Client() {
account.address
)
val client =
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient)
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient, dbPath)
client.ensureUserContactPublished()
client
} catch (e: java.lang.Exception) {
Expand All @@ -261,7 +265,7 @@ class Client() {
val newOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(environment = newOptions.api.env, secure = newOptions.api.isSecure)
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
Expand All @@ -272,13 +276,14 @@ class Client() {
address
)
}
} else null
} else Pair(null, "")

return Client(
address = address,
privateKeyBundleV1 = v1Bundle,
apiClient = apiClient,
libXMTPClient = v3Client
libXMTPClient = v3Client,
dbPath = dbPath
)
}

Expand All @@ -293,12 +298,13 @@ class Client() {
privateKeyBundleV1: PrivateKeyBundleV1,
legacyIdentitySource: LegacyIdentitySource,
accountAddress: String,
): FfiXmtpClient? {
): Pair<FfiXmtpClient?, String> {
var dbPath = ""
val v3Client: FfiXmtpClient? =
if (isAlphaMlsEnabled(options)) {
val alias = "xmtp-${options!!.api.env}-${accountAddress.lowercase()}"

val dbPath = if (options.dbPath == null) {
dbPath = if (options.dbPath == null) {
val dbDir = File(appContext?.filesDir?.absolutePath, "xmtp_db")
dbDir.mkdir()
dbDir.absolutePath + "/$alias.db3"
Expand Down Expand Up @@ -364,7 +370,7 @@ class Client() {
}
}
Log.i(TAG, "LibXMTP $libXMTPVersion")
return v3Client
return Pair(v3Client, dbPath)
}

/**
Expand Down Expand Up @@ -586,6 +592,10 @@ class Client() {
return !statuses.contains(false)
}

fun deleteLocalDatabase() {
File(dbPath).delete()
}

val privateKeyBundle: PrivateKeyBundle
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)

Expand Down
Loading