Skip to content

Commit

Permalink
Fix deeplinks handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Nov 21, 2023
1 parent bfcc8a0 commit 1609b04
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ class CoinFragment : BaseComposeFragment() {

@Composable
override fun GetContent(navController: NavController) {
val uid = try {
activity?.intent?.data?.getQueryParameter("uid")
} catch (e: UnsupportedOperationException) {
null
}

val coinUid = requireArguments().getString(COIN_UID_KEY, uid ?: "")
if (uid != null) {
activity?.intent?.data = null
}
val coinUid = requireArguments().getString(COIN_UID_KEY, "")

CoinScreen(
coinUid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class LaunchViewModel(
private val pinComponent: IPinComponent,
private val systemInfoManager: ISystemInfoManager,
private val keyStoreManager: IKeyStoreManager,
private val localStorage: ILocalStorage
localStorage: ILocalStorage
) : ViewModel() {
val torEnabled by localStorage::torEnabled

private val mainShowedOnce = localStorage.mainShowedOnce

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.horizontalsystems.bankwallet.modules.main

import android.net.Uri
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.BackHandler
Expand Down Expand Up @@ -62,24 +63,27 @@ import io.horizontalsystems.bankwallet.ui.compose.DisposableLifecycleCallbacks
import io.horizontalsystems.bankwallet.ui.compose.components.HsBottomNavigation
import io.horizontalsystems.bankwallet.ui.compose.components.HsBottomNavigationItem
import io.horizontalsystems.bankwallet.ui.extensions.WalletSwitchBottomSheet
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainFragment : BaseComposeFragment() {

private val transactionsViewModel by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment) { TransactionsModule.Factory() }
private var intentUri: Uri? = null

@Composable
override fun GetContent(navController: NavController) {
MainScreenWithRootedDeviceCheck(
transactionsViewModel = transactionsViewModel,
deepLink = activity?.intent?.data?.toString(),
deepLink = intentUri,
navController = navController,
clearActivityData = { activity?.intent?.data = null }
)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intentUri = activity?.intent?.data
activity?.intent?.data = null //clear intent data

requireActivity().onBackPressedDispatcher.addCallback(
this,
Expand All @@ -95,30 +99,28 @@ class MainFragment : BaseComposeFragment() {
@Composable
private fun MainScreenWithRootedDeviceCheck(
transactionsViewModel: TransactionsViewModel,
deepLink: String?,
deepLink: Uri?,
navController: NavController,
clearActivityData: () -> Unit,
rootedDeviceViewModel: RootedDeviceViewModel = viewModel(factory = RootedDeviceModule.Factory())
) {
if (rootedDeviceViewModel.showRootedDeviceWarning) {
RootedDeviceScreen { rootedDeviceViewModel.ignoreRootedDeviceWarning() }
} else {
MainScreen(transactionsViewModel, deepLink, navController, clearActivityData)
MainScreen(transactionsViewModel, deepLink, navController)
}
}

@OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class)
@Composable
private fun MainScreen(
transactionsViewModel: TransactionsViewModel,
deepLink: String?,
deepLink: Uri?,
fragmentNavController: NavController,
clearActivityData: () -> Unit,
viewModel: MainViewModel = viewModel(factory = MainModule.Factory(deepLink))
) {

val uiState = viewModel.uiState
val selectedPage = uiState.selectedPageIndex
val selectedPage = uiState.selectedTabIndex
val pagerState = rememberPagerState(initialPage = selectedPage) { uiState.mainNavItems.size }

val coroutineScope = rememberCoroutineScope()
Expand Down Expand Up @@ -242,17 +244,11 @@ private fun MainScreen(

if (uiState.wcSupportState != null) {
when (val wcSupportState = uiState.wcSupportState) {
SupportState.Supported -> {
fragmentNavController.slideFromRight(R.id.wallet_connect_graph)
}

SupportState.NotSupportedDueToNoActiveAccount -> {
clearActivityData.invoke()
fragmentNavController.slideFromBottom(R.id.wcErrorNoAccountFragment)
}

is SupportState.NotSupportedDueToNonBackedUpAccount -> {
clearActivityData.invoke()
val text = stringResource(R.string.WalletConnect_Error_NeedBackup)
fragmentNavController.slideFromBottom(
R.id.backupRequiredDialog,
Expand All @@ -261,18 +257,28 @@ private fun MainScreen(
}

is SupportState.NotSupported -> {
clearActivityData.invoke()
fragmentNavController.slideFromBottom(
R.id.wcAccountTypeNotSupportedDialog,
WCAccountTypeNotSupportedDialog.prepareParams(wcSupportState.accountTypeDescription)
)
}

null -> {}
else -> {}
}
viewModel.wcSupportStateHandled()
}

uiState.deeplinkPage?.let { deepLinkPage ->
LaunchedEffect(Unit) {
delay(500)
fragmentNavController.slideFromRight(
deepLinkPage.navigationId,
deepLinkPage.bundle
)
viewModel.deeplinkPageHandled()
}
}

DisposableLifecycleCallbacks(
onResume = viewModel::onResume,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand All @@ -15,7 +16,7 @@ import kotlinx.parcelize.Parcelize

object MainModule {

class Factory(private val wcDeepLink: String?) : ViewModelProvider.Factory {
class Factory(private val wcDeepLink: Uri?) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MainViewModel(
Expand Down Expand Up @@ -74,7 +75,8 @@ object MainModule {
}

data class UiState(
val selectedPageIndex: Int,
val selectedTabIndex: Int,
val deeplinkPage: DeeplinkPage?,
val mainNavItems: List<NavigationViewItem>,
val showRateAppDialog: Boolean,
val contentHidden: Boolean,
Expand All @@ -84,3 +86,8 @@ object MainModule {
val wcSupportState: WC2Manager.SupportState?
)
}

data class DeeplinkPage(
val navigationId: Int,
val bundle: Bundle? = null
)
Loading

0 comments on commit 1609b04

Please sign in to comment.