Skip to content

Commit

Permalink
Fix WalletConnect no Account error
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Jun 20, 2024
1 parent 2257fc3 commit 5052bc1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.horizontalsystems.bankwallet.core

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Parcelable
import android.widget.ImageView
Expand Down Expand Up @@ -224,4 +227,10 @@ suspend fun <T> retryWhen(
delay(1000)
}
return block()
}

fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -38,6 +37,7 @@ import androidx.navigation.NavController
import androidx.navigation.navGraphViewModels
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.BaseComposeFragment
import io.horizontalsystems.bankwallet.core.findActivity
import io.horizontalsystems.bankwallet.core.managers.RateAppManager
import io.horizontalsystems.bankwallet.core.slideFromBottom
import io.horizontalsystems.bankwallet.core.slideFromRight
Expand Down Expand Up @@ -73,21 +73,17 @@ 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 = intentUri,
navController = navController,
)
}

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 @@ -103,32 +99,41 @@ class MainFragment : BaseComposeFragment() {
@Composable
private fun MainScreenWithRootedDeviceCheck(
transactionsViewModel: TransactionsViewModel,
deepLink: Uri?,
navController: NavController,
rootedDeviceViewModel: RootedDeviceViewModel = viewModel(factory = RootedDeviceModule.Factory())
) {
if (rootedDeviceViewModel.showRootedDeviceWarning) {
RootedDeviceScreen { rootedDeviceViewModel.ignoreRootedDeviceWarning() }
} else {
MainScreen(transactionsViewModel, deepLink, navController)
MainScreen(transactionsViewModel, navController)
}
}

@OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class)
@Composable
private fun MainScreen(
transactionsViewModel: TransactionsViewModel,
deepLink: Uri?,
fragmentNavController: NavController,
viewModel: MainViewModel = viewModel(factory = MainModule.Factory(deepLink))
viewModel: MainViewModel = viewModel(factory = MainModule.Factory())
) {
val uiState = viewModel.uiState
val context = LocalContext.current
val selectedPage = uiState.selectedTabIndex
val pagerState = rememberPagerState(initialPage = selectedPage) { uiState.mainNavItems.size }

val coroutineScope = rememberCoroutineScope()
val modalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)

LaunchedEffect(Unit) {
context.findActivity()?.let { activity ->
activity.intent?.data?.let { uri ->
viewModel.handleDeepLink(uri)
activity.intent?.data = null //clear intent data
}
}
}


ModalBottomSheetLayout(
sheetState = modalBottomSheetState,
sheetBackgroundColor = ComposeAppTheme.colors.transparent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.parcelize.Parcelize

object MainModule {

class Factory(private val wcDeepLink: Uri?) : ViewModelProvider.Factory {
class Factory : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MainViewModel(
Expand All @@ -28,7 +28,6 @@ object MainModule {
App.localStorage,
App.wcSessionManager,
App.wcManager,
wcDeepLink
) as T
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class MainViewModel(
private val localStorage: ILocalStorage,
wcSessionManager: WCSessionManager,
private val wcManager: WCManager,
deepLink: Uri?
) : ViewModelUiState<MainModule.UiState>() {

private var wcPendingRequestsCount = 0
Expand Down Expand Up @@ -79,7 +78,7 @@ class MainViewModel(
)
}

private var selectedTabIndex = getTabIndexToOpen(deepLink)
private var selectedTabIndex = getTabIndexToOpen()
private var deeplinkPage: DeeplinkPage? = null
private var mainNavItems = navigationItems()
private var showRateAppDialog = false
Expand Down Expand Up @@ -248,14 +247,7 @@ class MainViewModel(
}
}

private fun getTabIndexToOpen(deepLink: Uri? = null): Int {
deepLink?.let {
val (tab, deeplinkPageData) = getNavigationDataForDeeplink(it)
deeplinkPage = deeplinkPageData
currentMainTab = tab
return items.indexOf(tab)
}

private fun getTabIndexToOpen(): Int {
val tab = when {
relaunchBySettingChange -> {
relaunchBySettingChange = false
Expand Down Expand Up @@ -370,4 +362,12 @@ class MainViewModel(
emitState()
}

fun handleDeepLink(uri: Uri) {
val (tab, deeplinkPageData) = getNavigationDataForDeeplink(uri)
deeplinkPage = deeplinkPageData
currentMainTab = tab
selectedTabIndex = items.indexOf(tab)
syncNavigation()
}

}

0 comments on commit 5052bc1

Please sign in to comment.