Skip to content

Commit

Permalink
Navigate to connections screen only if current destination is not con…
Browse files Browse the repository at this point in the history
…nection screen
  • Loading branch information
jakubuid committed Jul 11, 2024
1 parent 2672129 commit 50b6120
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Web3WalletActivity : AppCompatActivity() {
private val connectionsViewModel = ConnectionsViewModel()

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
// FCM SDK (and your app) can post notifications.
Expand Down Expand Up @@ -93,7 +93,10 @@ class Web3WalletActivity : AppCompatActivity() {
when (event) {
is CoreEvent.Disconnect -> {
connectionsViewModel.refreshConnections()
navController.navigate(Route.Connections.path)

if (navController.currentDestination?.route != Route.Connections.path) {
navController.navigate(Route.Connections.path)
}
}

else -> Unit
Expand All @@ -115,39 +118,41 @@ class Web3WalletActivity : AppCompatActivity() {
connectionsViewModel: ConnectionsViewModel,
) {
web3walletViewModel.sessionRequestStateFlow
.onEach {
if (it.arrayOfArgs.isNotEmpty()) {
web3walletViewModel.showRequestLoader(false)
navController.navigate(Route.SessionRequest.path)
}
.onEach {
if (it.arrayOfArgs.isNotEmpty()) {
web3walletViewModel.showRequestLoader(false)
navController.navigate(Route.SessionRequest.path)
}
.launchIn(lifecycleScope)
}
.launchIn(lifecycleScope)

web3walletViewModel.walletEvents
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach { event ->
when (event) {
is SignEvent.SessionProposal -> navController.navigate(Route.SessionProposal.path)
is SignEvent.ExpiredRequest -> {
navController.popBackStack(
route = Route.Connections.path,
inclusive = false
)
if (navController.currentDestination?.route != Route.Connections.path) {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
}
Toast.makeText(baseContext, "Request expired", Toast.LENGTH_SHORT).show()
}

is SignEvent.Disconnect -> {
connectionsViewModel.refreshConnections()
is SignEvent.Disconnect -> {
connectionsViewModel.refreshConnections()

if (navController.currentDestination?.route != Route.Connections.path) {
navController.navigate(Route.Connections.path)
}
}

is AuthEvent.OnRequest -> navController.navigate(Route.AuthRequest.path)
is SignEvent.SessionAuthenticate -> navController.navigate(Route.SessionAuthenticate.path)
is AuthEvent.OnRequest -> navController.navigate(Route.AuthRequest.path)
is SignEvent.SessionAuthenticate -> navController.navigate(Route.SessionAuthenticate.path)

else -> Unit
}
else -> Unit
}
.launchIn(lifecycleScope)
}
.launchIn(lifecycleScope)
}

override fun onNewIntent(intent: Intent?) {
Expand All @@ -170,7 +175,7 @@ class Web3WalletActivity : AppCompatActivity() {
}

if (intent?.dataString?.startsWith("kotlin-web3wallet://request") == false
&& intent.dataString?.contains("requestId") == false
&& intent.dataString?.contains("requestId") == false
) {
navController.handleDeepLink(intent)
}
Expand All @@ -180,9 +185,9 @@ class Web3WalletActivity : AppCompatActivity() {
// This is only necessary for API level >= 33 (TIRAMISU)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
this,
android.Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
// FCM SDK (and your app) can post notifications.
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ fun WalletSampleHost(
web3walletViewModel.eventsSharedFlow.collect {
when (it) {
is PairingEvent.Error -> {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
if (navController.currentDestination?.route != Route.Connections.path) {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
}
Toast.makeText(navController.context, it.message, Toast.LENGTH_SHORT).show()
}

Expand Down

0 comments on commit 50b6120

Please sign in to comment.