Skip to content

Commit

Permalink
fix: Improve session management
Browse files Browse the repository at this point in the history
  • Loading branch information
elefantel committed Jan 17, 2024
1 parent d008617 commit 4135f85
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/metamask/dapp/ConnectScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.metamask.androidsdk.DappMetadata
import io.metamask.androidsdk.EthereumState

@Composable
Expand Down Expand Up @@ -82,6 +81,7 @@ fun ConnectScreen(

DappLabel(
text = ethereumState.sessionId,
color = Color.Unspecified,
modifier = Modifier.padding(bottom = bottomMargin)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ internal class CommunicationClient(context: Context, callback: EthereumEventCall
}

val submittedRequest = submittedRequests[id]?.request ?: return
val isResultMethod = EthereumMethod.isResultMethod(submittedRequest.method ?: "")
val isResultMethod = EthereumMethod.isResultMethod(submittedRequest.method)

if (!isResultMethod) {
val resultJson = data.optString("result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ class Ethereum (

private var sessionDuration: Long = DEFAULT_SESSION_DURATION

private var infuraAPIKey: String? = null

override fun updateAccount(account: String) {
Logger.log("Ethereum:: Selected account changed: $account")
_ethereumState.postValue(
currentEthereumState.copy(
selectedAddress = account
selectedAddress = account,
sessionId = communicationClient?.sessionId ?: ""
)
)
}
Expand All @@ -68,7 +67,8 @@ class Ethereum (
Logger.log("Ethereum:: ChainId changed: $newChainId")
_ethereumState.postValue(
currentEthereumState.copy(
chainId = newChainId
chainId = newChainId,
sessionId = communicationClient?.sessionId ?: ""
)
)
}
Expand All @@ -85,14 +85,12 @@ class Ethereum (
communicationClient?.clearSession {
_ethereumState.postValue(
currentEthereumState.copy(
sessionId = getSessionId()
sessionId = communicationClient?.sessionId ?: ""
)
)
}
}

private fun getSessionId(): String = communicationClient?.sessionId ?: ""

fun connect(callback: ((Result) -> Unit)? = null) {
val error = dappMetadata.validationError
if (error != null) {
Expand All @@ -118,9 +116,7 @@ class Ethereum (

fun connectWith(request: EthereumRequest, callback: ((Result) -> Unit)? = null) {
Logger.log("Ethereum:: connecting with ${request.method}...")
Logger.log("Ethereum:: connectRequestSent $connectRequestSent")
connectRequestSent = true
Logger.log("Ethereum:: connectRequestSent $connectRequestSent")
communicationClient?.dappMetadata = dappMetadata
communicationClient?.ethereumEventCallbackRef = WeakReference(this)
communicationClient?.updateSessionDuration(sessionDuration)
Expand Down

0 comments on commit 4135f85

Please sign in to comment.