Skip to content

Commit

Permalink
Merge pull request #135 from MetaMask/sync-dapp-state
Browse files Browse the repository at this point in the history
chore: sync dapp state after async fetching cached state
  • Loading branch information
elefantel authored Jul 29, 2024
2 parents d80e6fa + 3c9c970 commit 895604d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions metamask-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
targetSdk 33

ext.versionCode = 1
ext.versionName = "0.6.0"
ext.versionName = "0.6.1"

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -65,7 +65,7 @@ dependencies {

ext {
PUBLISH_GROUP_ID = 'io.metamask.androidsdk'
PUBLISH_VERSION = '0.6.0'
PUBLISH_VERSION = '0.6.1'
PUBLISH_ARTIFACT_ID = 'metamask-android-sdk'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference

private const val METAMASK_DEEPLINK = "https://metamask.app.link"
Expand Down Expand Up @@ -46,9 +48,12 @@ class Ethereum (

// Expose plain variables for developers who prefer not using observing live data via ethereumState
val chainId: String
get() = currentEthereumState.chainId
get() = if (currentEthereumState.chainId.isEmpty()) { currentEthereumState.chainId } else { cachedChainId }
val selectedAddress: String
get() = currentEthereumState.selectedAddress
get() = if (currentEthereumState.selectedAddress.isEmpty()) { currentEthereumState.selectedAddress } else { cachedAccount }

private var cachedChainId = ""
private var cachedAccount = ""

// Toggle SDK tracking
var enableDebug: Boolean = true
Expand All @@ -58,23 +63,30 @@ class Ethereum (
}

init {
updateSessionDuration()
initializeEthereumState()
runBlocking { fetchCachedSession() }
}

private fun initializeEthereumState() {
coroutineScope.launch(Dispatchers.IO) {
private suspend fun fetchCachedSession() {
withContext(Dispatchers.IO) {
try {
val account = storage.getValue(key = SessionManager.SESSION_ACCOUNT_KEY, file = SessionManager.SESSION_CONFIG_FILE)
val chainId = storage.getValue(key = SessionManager.SESSION_CHAIN_ID_KEY, file = SessionManager.SESSION_CONFIG_FILE)
_ethereumState.postValue(
currentEthereumState.copy(
selectedAddress = account ?: "",
chainId = chainId ?: ""
if (account != null && chainId != null) {
cachedChainId = chainId
cachedAccount = account

_ethereumState.postValue(
currentEthereumState.copy(
selectedAddress = account,
chainId = chainId,
sessionId = communicationClient?.sessionId ?: ""
)
)
)
} else {

}
} catch (e: Exception) {
logger.error(e.localizedMessage)
e.localizedMessage?.let { logger.error(it) }
}
}
}
Expand Down Expand Up @@ -125,6 +137,7 @@ class Ethereum (

fun connect(callback: ((Result) -> Unit)? = null) {
connectRequestSent = true

val error = dappMetadata.validationError
if (error != null) {
callback?.invoke((Result.Error(error)))
Expand Down Expand Up @@ -207,11 +220,19 @@ class Ethereum (
}

fun getChainId(callback: ((Result) -> Unit)?) {
ethereumRequest(method = EthereumMethod.ETH_CHAIN_ID, params = null, callback)
if(connectRequestSent) {
ethereumRequest(method = EthereumMethod.ETH_CHAIN_ID, params = null, callback)
} else {
callback?.invoke((Result.Success.Item(cachedChainId)))
}
}

fun getEthAccounts(callback: ((Result) -> Unit)?) {
ethereumRequest(method = EthereumMethod.ETH_ACCOUNTS, params = null, callback)
if (connectRequestSent) {
ethereumRequest(method = EthereumMethod.ETH_ACCOUNTS, params = null, callback)
} else {
callback?.invoke((Result.Success.Item(cachedAccount)))
}
}

fun getEthBalance(address: String, block: String, callback: ((Result) -> Unit)? = null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.metamask.androidsdk

object SDKInfo {
const val VERSION = "0.6.0"
const val VERSION = "0.6.1"
const val PLATFORM = "android"
}

0 comments on commit 895604d

Please sign in to comment.