Skip to content

Commit

Permalink
Merge pull request #1481 from WalletConnect/fix/close_siwe_fallback_w…
Browse files Browse the repository at this point in the history
…hen_connecting_MM

fix: event adapter parsing
  • Loading branch information
jakubuid authored Aug 22, 2024
2 parents f5afa31 + de8d03f commit 3389ad0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.walletconnect.web3.modal.ui.components.internal

import android.annotation.SuppressLint
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -46,6 +47,7 @@ fun Web3ModalComponent(
)
}

@SuppressLint("RestrictedApi")
@Composable
internal fun Web3ModalComponent(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ internal class SessionEventVOJsonAdapter(moshi: Moshi) : JsonAdapter<SessionEven
1 -> {
// Moshi does not handle malformed JSON where there is a missing key for an array or object
val dataAny = anyAdapter.fromJson(reader) ?: throw Util.unexpectedNull("data", "data", reader)

data = if (dataAny is List<*>) {
upsertArray(JSONArray(), dataAny).toString()
} else {
} else if (dataAny is Map<*, *>) {
val paramsMap = dataAny as Map<*, *>
upsertObject(JSONObject(), paramsMap).toString()
} else {
if (dataAny is Number) {
val castedNumber = if (dataAny.toDouble() % 1 == 0.0) {
dataAny.toLong()
} else {
dataAny.toDouble()
}
castedNumber.toString()
} else {
dataAny.toString()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import junit.framework.TestCase
import org.json.JSONArray
import org.junit.Test
import kotlin.reflect.jvm.jvmName
import kotlin.test.assertEquals

class SessionEventVOJsonAdapterTest {
private val moshi: Moshi = Moshi.Builder()
Expand Down Expand Up @@ -60,4 +61,10 @@ class SessionEventVOJsonAdapterTest {

iterateJsonArrays(expectedParamsJsonArray, actualParamsJsonArray)
}

@Test
fun testParsingNumber() {
data = """1""".trimIndent()
assertEquals(data, serializedData)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.walletconnect.sample.modal.common

import androidx.navigation.NavController

const val messageArg = "messageArg"

sealed class Route(val path: String) {
Expand All @@ -10,8 +8,4 @@ sealed class Route(val path: String) {
object Lab : Route("Lab")

object AlertDialog : Route("Alert")
}

fun NavController.openAlertDialog(message: String) {
navigate(Route.AlertDialog.path + "/$message")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.walletconnect.sample.common.getEthSignTypedData
import com.walletconnect.sample.common.getPersonalSignBody
import com.walletconnect.sample.common.ui.commons.BlueButton
import com.walletconnect.sample.modal.ModalSampleDelegate
import com.walletconnect.sample.modal.common.openAlertDialog
import com.walletconnect.web3.modal.client.Modal
import com.walletconnect.web3.modal.client.Web3Modal
import com.walletconnect.web3.modal.client.models.request.Request
Expand All @@ -44,17 +43,20 @@ fun LabScreen(

LaunchedEffect(Unit) {
ModalSampleDelegate.wcEventModels.collect { event ->
when(event) {
when (event) {
is Modal.Model.SessionRequestResponse -> {
when(event.result) {
when (event.result) {
is Modal.Model.JsonRpcResponse.JsonRpcError -> {
val error = event.result as Modal.Model.JsonRpcResponse.JsonRpcError
navController.openAlertDialog("Error Message: ${error.message}\n Error Code: ${error.code}")
Toast.makeText(context, "Error Message: ${error.message}\n Error Code: ${error.code}", Toast.LENGTH_SHORT).show()
}
is Modal.Model.JsonRpcResponse.JsonRpcResult -> navController.openAlertDialog((event.result as Modal.Model.JsonRpcResponse.JsonRpcResult).result)

is Modal.Model.JsonRpcResponse.JsonRpcResult -> Toast.makeText(context, (event.result as Modal.Model.JsonRpcResponse.JsonRpcResult).result, Toast.LENGTH_SHORT).show()
}
}
is Modal.Model.Error -> { navController.openAlertDialog(event.throwable.localizedMessage ?: "Something went wrong") }

is Modal.Model.Error -> Toast.makeText(context, event.throwable.localizedMessage ?: "Something went wrong", Toast.LENGTH_SHORT).show()

else -> Unit
}
}
Expand Down

0 comments on commit 3389ad0

Please sign in to comment.