Skip to content

Commit

Permalink
Do close dialog on connectivity exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Jul 11, 2024
1 parent 1bd55da commit 2672129
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.google.accompanist.navigation.material.BottomSheetNavigator
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.bottomSheet
import com.walletconnect.sample.wallet.domain.WCDelegate
import com.walletconnect.sample.wallet.ui.routes.Route
import com.walletconnect.sample.wallet.ui.routes.bottomsheet_routes.scan_uri.ScanUriRoute
import com.walletconnect.sample.wallet.ui.routes.bottomsheet_routes.update_subscription.UpdateSubscriptionRoute
Expand Down Expand Up @@ -60,6 +61,14 @@ fun Web3WalletNavGraph(
var scrimColor by remember { mutableStateOf(Color.Unspecified) }
val inboxViewModel: InboxViewModel = viewModel()

navController.addOnDestinationChangedListener(
listener = { _, destination, _ ->
if (destination.route == Route.Connections.path) {
WCDelegate.sessionRequestEvent = null
WCDelegate.currentId = null
}
})

ModalBottomSheetLayout(
modifier = modifier,
bottomSheetNavigator = bottomSheetNavigator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.walletconnect.android.internal.common.exception.NoConnectivityException
import com.walletconnect.sample.common.sendResponseDeepLink
import com.walletconnect.sample.common.ui.theme.mismatch_color
import com.walletconnect.sample.common.ui.themedColor
Expand Down Expand Up @@ -156,10 +157,10 @@ private fun SessionAuthenticateDialog(
},
onError = { error ->
isCancelLoading = false
showError(error, composableScope, context)
showError(navController, error, composableScope, context)
})
} catch (e: Throwable) {
showError(e.message, composableScope, context)
showError(navController, e, composableScope, context)
}
}, onConfirm = {
isConfirmLoading = true
Expand All @@ -183,10 +184,10 @@ private fun SessionAuthenticateDialog(
},
onError = { error ->
isConfirmLoading = false
showError(error, composableScope, context)
showError(navController, error, composableScope, context)
})
} catch (e: Exception) {
showError(e.message, composableScope, context)
showError(navController, e, composableScope, context)
}
},
isLoadingConfirm = isConfirmLoading,
Expand All @@ -196,9 +197,13 @@ private fun SessionAuthenticateDialog(
}
}

private fun showError(message: String?, coroutineScope: CoroutineScope, context: Context) {
private fun showError(navController: NavHostController, throwable: Throwable?, coroutineScope: CoroutineScope, context: Context) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, message ?: "Session authenticate error, please check your Internet connection", Toast.LENGTH_SHORT).show()
if (throwable !is NoConnectivityException) {
navController.popBackStack()
}

Toast.makeText(context, throwable?.message ?: "Session authenticate error, please check your Internet connection", Toast.LENGTH_SHORT).show()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import com.walletconnect.android.cacao.signature.SignatureType
import com.walletconnect.android.internal.common.exception.NoConnectivityException
import com.walletconnect.android.utils.cacao.sign
import com.walletconnect.sample.wallet.domain.ACCOUNTS_1_EIP155_ADDRESS
import com.walletconnect.sample.wallet.domain.EthAccountDelegate
Expand All @@ -18,7 +19,7 @@ import com.walletconnect.web3.wallet.utils.CacaoSigner
class SessionAuthenticateViewModel : ViewModel() {
val sessionAuthenticateUI: SessionAuthenticateUI? get() = generateAuthRequestUI()

fun approve(onSuccess: (String) -> Unit = {}, onError: (String) -> Unit = {}) {
fun approve(onSuccess: (String) -> Unit = {}, onError: (Throwable) -> Unit = {}) {
if (WCDelegate.sessionAuthenticateEvent != null) {
try {
val sessionAuthenticate = WCDelegate.sessionAuthenticateEvent!!.first
Expand All @@ -42,25 +43,29 @@ class SessionAuthenticateViewModel : ViewModel() {

val approveProposal = Wallet.Params.ApproveSessionAuthenticate(id = sessionAuthenticate.id, auths = auths)
Web3Wallet.approveSessionAuthenticate(approveProposal,
onError = { error ->
Firebase.crashlytics.recordException(error.throwable)
onError(error.throwable.message ?: "Undefined error, please check your Internet connection")
},
onSuccess = {
WCDelegate.sessionAuthenticateEvent = null
onSuccess(sessionAuthenticate.participant.metadata?.redirect ?: "")
})
},
onError = { error ->
if (error.throwable !is NoConnectivityException) {
WCDelegate.sessionAuthenticateEvent = null
}
Firebase.crashlytics.recordException(error.throwable)
onError(error.throwable)
}
)
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
WCDelegate.sessionAuthenticateEvent = null
onError(e.message ?: "Undefined error, please check your Internet connection")
onError(e)
}
} else {
onError("Authenticate request expired")
onError(Throwable("Authenticate request expired"))
}
}

fun reject(onSuccess: (String) -> Unit = {}, onError: (String) -> Unit = {}) {
fun reject(onSuccess: (String) -> Unit = {}, onError: (Throwable) -> Unit = {}) {
if (WCDelegate.sessionAuthenticateEvent != null) {
try {
val sessionAuthenticate = WCDelegate.sessionAuthenticateEvent!!.first
Expand All @@ -76,16 +81,19 @@ class SessionAuthenticateViewModel : ViewModel() {
onSuccess(sessionAuthenticate.participant.metadata?.redirect ?: "")
},
onError = { error ->
if (error.throwable !is NoConnectivityException) {
WCDelegate.sessionAuthenticateEvent = null
}
Firebase.crashlytics.recordException(error.throwable)
onError(error.throwable.message ?: "Undefined error, please check your Internet connection")
onError(error.throwable)
})
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
WCDelegate.sessionAuthenticateEvent = null
onError(e.message ?: "Undefined error, please check your Internet connection")
onError(e)
}
} else {
onError("Authenticate request expired")
onError(Throwable("Authenticate request expired"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.core.net.toUri
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.walletconnect.android.internal.common.exception.NoConnectivityException
import com.walletconnect.sample.common.Chains
import com.walletconnect.sample.common.CompletePreviews
import com.walletconnect.sample.common.sendResponseDeepLink
Expand Down Expand Up @@ -179,10 +180,10 @@ private fun SessionProposalDialog(
},
onError = { error ->
isCancelLoading = false
showError(error, coroutineScope, context)
showError(navController, error, coroutineScope, context)
})
} catch (e: Throwable) {
showError(e.message, coroutineScope, context)
showError(navController, e, coroutineScope, context)
}
},
onConfirm = {
Expand All @@ -206,10 +207,10 @@ private fun SessionProposalDialog(
},
onError = { error ->
isConfirmLoading = false
showError(error, coroutineScope, context)
showError(navController, error, coroutineScope, context)
})
} catch (e: Throwable) {
showError(e.message, coroutineScope, context)
showError(navController, e, coroutineScope, context)
}
},
isLoadingConfirm = isConfirmLoading,
Expand All @@ -219,9 +220,13 @@ private fun SessionProposalDialog(
}
}

private fun showError(message: String?, coroutineScope: CoroutineScope, context: Context) {
private fun showError(navController: NavHostController, throwable: Throwable?, coroutineScope: CoroutineScope, context: Context) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, message ?: "Session proposal error, please check your Internet connection", Toast.LENGTH_SHORT).show()
if (throwable !is NoConnectivityException) {
navController.popBackStack()
}

Toast.makeText(context, throwable?.message ?: "Session proposal error, please check your Internet connection", Toast.LENGTH_SHORT).show()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import timber.log.Timber

class SessionProposalViewModel : ViewModel() {
val sessionProposal: SessionProposalUI? = generateSessionProposalUI()
fun approve(proposalPublicKey: String, onSuccess: (String) -> Unit = {}, onError: (String) -> Unit = {}) {
fun approve(proposalPublicKey: String, onSuccess: (String) -> Unit = {}, onError: (Throwable) -> Unit = {}) {
val proposal = Web3Wallet.getSessionProposals().find { it.proposerPublicKey == proposalPublicKey }
if (proposal != null) {
try {
Expand All @@ -24,7 +24,7 @@ class SessionProposalViewModel : ViewModel() {
onError = { error ->
Firebase.crashlytics.recordException(error.throwable)
WCDelegate.sessionProposalEvent = null
onError(error.throwable.message ?: "Undefined error, please check your Internet connection")
onError(error.throwable)
},
onSuccess = {
WCDelegate.sessionProposalEvent = null
Expand All @@ -33,14 +33,14 @@ class SessionProposalViewModel : ViewModel() {
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
WCDelegate.sessionProposalEvent = null
onError(e.message ?: "Undefined error, please check your Internet connection")
onError(e)
}
} else {
onError("Cannot approve session proposal, it has expired. Please try again.")
onError(Throwable("Cannot approve session proposal, it has expired. Please try again."))
}
}

fun reject(proposalPublicKey: String, onSuccess: (String) -> Unit = {}, onError: (String) -> Unit = {}) {
fun reject(proposalPublicKey: String, onSuccess: (String) -> Unit = {}, onError: (Throwable) -> Unit = {}) {
val proposal = Web3Wallet.getSessionProposals().find { it.proposerPublicKey == proposalPublicKey }
if (proposal != null) {
try {
Expand All @@ -58,15 +58,15 @@ class SessionProposalViewModel : ViewModel() {
onError = { error ->
Firebase.crashlytics.recordException(error.throwable)
WCDelegate.sessionProposalEvent = null
onError(error.throwable.message ?: "Undefined error, please check your Internet connection")
onError(error.throwable)
})
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
WCDelegate.sessionProposalEvent = null
onError(e.message ?: "Undefined error, please check your Internet connection")
onError(e)
}
} else {
onError("Cannot reject session proposal, it has expired. Please try again.")
onError(Throwable("Cannot reject session proposal, it has expired. Please try again."))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.walletconnect.android.internal.common.exception.NoConnectivityException
import com.walletconnect.sample.common.CompletePreviews
import com.walletconnect.sample.common.sendResponseDeepLink
import com.walletconnect.sample.common.ui.theme.PreviewTheme
Expand Down Expand Up @@ -73,6 +74,7 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
is SessionRequestUI.Content -> {
val allowButtonColor = getColor(sessionRequestUI.peerContextUI)
currentId = sessionRequestUI.requestId

SemiTransparentDialog {
Spacer(modifier = Modifier.height(24.dp))
Peer(peerUI = sessionRequestUI.peerUI, "sends a request", sessionRequestUI.peerContextUI)
Expand Down Expand Up @@ -100,11 +102,11 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
},
onError = { error ->
isConfirmLoading = false
showError(error, composableScope, context)
showError(navController, error, composableScope, context)
})

} catch (e: Throwable) {
showError(e.message, composableScope, context)
showError(navController, e, composableScope, context)
}
},
onCancel = {
Expand All @@ -126,10 +128,10 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
},
onError = { error ->
isCancelLoading = false
showError(error, composableScope, context)
showError(navController, error, composableScope, context)
})
} catch (e: Throwable) {
showError(e.message, composableScope, context)
showError(navController, e, composableScope, context)
}
},
isLoadingConfirm = isConfirmLoading,
Expand Down Expand Up @@ -163,9 +165,12 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
}
}

private fun showError(message: String?, coroutineScope: CoroutineScope, context: Context) {
private fun showError(navController: NavHostController, throwable: Throwable?, coroutineScope: CoroutineScope, context: Context) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, message ?: "Session request error, please check your Internet connection", Toast.LENGTH_SHORT).show()
if (throwable !is NoConnectivityException) {
navController.popBackStack()
}
Toast.makeText(context, throwable?.message ?: "Session request error, please check your Internet connection", Toast.LENGTH_SHORT).show()
}
}

Expand Down
Loading

0 comments on commit 2672129

Please sign in to comment.