Skip to content

Commit

Permalink
Clean up kotlin code
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Dec 22, 2023
1 parent 3650b8d commit 08649d7
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import expo.modules.xmtpreactnativesdk.wrappers.DecodedMessageWrapper
import expo.modules.xmtpreactnativesdk.wrappers.DecryptedLocalAttachment
import expo.modules.xmtpreactnativesdk.wrappers.EncryptedLocalAttachment
import expo.modules.xmtpreactnativesdk.wrappers.PreparedLocalMessage
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -127,8 +127,9 @@ class XMTPModule : Module() {
private val isDebugEnabled = BuildConfig.DEBUG // TODO: consider making this configurable
private val conversations: MutableMap<String, Conversation> = mutableMapOf()
private val subscriptions: MutableMap<String, Job> = mutableMapOf()
var waitForPreEnableIdentityCallback: Boolean = false
var waitForPreCreateIdentityCallback: Boolean = false
private var preEnableIdentityCallbackDeferred: CompletableDeferred<Unit>? = null
private var preCreateIdentityCallbackDeferred: CompletableDeferred<Unit>? = null


override fun definition() = ModuleDefinition {
Name("XMTP")
Expand All @@ -155,8 +156,10 @@ class XMTPModule : Module() {
val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address)
signer = reactSigner

waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true
waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true
if (hasCreateIdentityCallback == true)
preCreateIdentityCallbackDeferred = CompletableDeferred()
if (hasEnableIdentityCallback == true)
preEnableIdentityCallbackDeferred = CompletableDeferred()
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
Expand All @@ -182,8 +185,10 @@ class XMTPModule : Module() {
logV("createRandom")
val privateKey = PrivateKeyBuilder()

waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true
waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true
if (hasCreateIdentityCallback == true)
preCreateIdentityCallbackDeferred = CompletableDeferred()
if (hasEnableIdentityCallback == true)
preEnableIdentityCallbackDeferred = CompletableDeferred()
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
Expand Down Expand Up @@ -600,16 +605,14 @@ class XMTPModule : Module() {
client.contacts.consentList.entries.map { ConsentWrapper.encode(it.value) }
}

Function("preEnableIdentityCallbackCompleted") {
logV("preEnableIdentityCallbackCompleted")
waitForPreEnableIdentityCallback = false
true
}

Function("preCreateIdentityCallbackCompleted") {
logV("preCreateIdentityCallbackCompleted")
waitForPreCreateIdentityCallback = false
true
preCreateIdentityCallbackDeferred?.complete(Unit)
}

Function("preEnableIdentityCallbackCompleted") {
logV("preEnableIdentityCallbackCompleted")
preEnableIdentityCallbackDeferred?.complete(Unit)
}
}

Expand Down Expand Up @@ -736,19 +739,14 @@ class XMTPModule : Module() {

private val preEnableIdentityCallback: suspend () -> Unit = {
sendEvent("preEnableIdentityCallback")
waitForCallback { waitForPreEnableIdentityCallback }
preEnableIdentityCallbackDeferred?.await()
preCreateIdentityCallbackDeferred == null
}

private val preCreateIdentityCallback: suspend () -> Unit = {
sendEvent("preCreateIdentityCallback")
waitForCallback { waitForPreCreateIdentityCallback }
}

// Helper function to wait for a callback
private suspend fun waitForCallback(check: () -> Boolean) {
while (check()) {
delay(100) // Wait for 100ms before checking again
}
preCreateIdentityCallbackDeferred?.await()
preCreateIdentityCallbackDeferred = null
}
}

Expand Down

0 comments on commit 08649d7

Please sign in to comment.