From 974d1fbf8c333a8892356349a43f3a1e4f9544d6 Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 4 Nov 2024 13:02:04 +0600 Subject: [PATCH] Handle getting transactions viewmodel on restore state --- .../bankwallet/modules/main/MainFragment.kt | 33 +++++++++++++++---- .../TransactionInfoFragment.kt | 13 ++++++-- .../transactions/FilterBlockchainFragment.kt | 16 +++++++-- .../transactions/FilterCoinFragment.kt | 16 +++++++-- .../TransactionsFilterFragment.kt | 16 +++++++-- 5 files changed, 78 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/main/MainFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/main/MainFragment.kt index 51fcefd1bf2..b26ac916350 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/main/MainFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/main/MainFragment.kt @@ -31,10 +31,11 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.compose.LifecycleEventEffect import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavBackStackEntry 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 @@ -72,14 +73,24 @@ import kotlinx.coroutines.launch class MainFragment : BaseComposeFragment() { - private val transactionsViewModel by navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() } - @Composable override fun GetContent(navController: NavController) { - MainScreenWithRootedDeviceCheck( - transactionsViewModel = transactionsViewModel, - navController = navController, - ) + val backStackEntry = navController.safeGetBackStackEntry(R.id.mainFragment) + + backStackEntry?.let { + val viewModel = ViewModelProvider(backStackEntry.viewModelStore, TransactionsModule.Factory()) + .get(TransactionsViewModel::class.java) + MainScreenWithRootedDeviceCheck( + transactionsViewModel = viewModel, + navController = navController, + ) + } ?: run { + // Back stack entry doesn't exist, restart activity + val intent = requireActivity().intent + intent.data = null + requireActivity().finishAffinity() + startActivity(intent) + } } override fun onCreate(savedInstanceState: Bundle?) { @@ -357,3 +368,11 @@ private fun BadgedIcon( } } } + +fun NavController.safeGetBackStackEntry(destinationId: Int): NavBackStackEntry? { + return try { + this.getBackStackEntry(destinationId) + } catch (e: IllegalArgumentException) { + null + } +} \ No newline at end of file diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactionInfo/TransactionInfoFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactionInfo/TransactionInfoFragment.kt index 6a3681f41dd..7e2b85b0c37 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactionInfo/TransactionInfoFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactionInfo/TransactionInfoFragment.kt @@ -1,5 +1,6 @@ package io.horizontalsystems.bankwallet.modules.transactionInfo +import android.widget.Toast import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -13,6 +14,7 @@ import androidx.compose.ui.unit.dp import androidx.navigation.NavController import androidx.navigation.navGraphViewModels import io.horizontalsystems.bankwallet.R +import io.horizontalsystems.bankwallet.core.App import io.horizontalsystems.bankwallet.core.BaseComposeFragment import io.horizontalsystems.bankwallet.core.slideFromRight import io.horizontalsystems.bankwallet.core.stats.StatEntity @@ -48,11 +50,16 @@ import io.horizontalsystems.bankwallet.ui.compose.components.WarningMessageCell class TransactionInfoFragment : BaseComposeFragment() { - private val viewModelTxs by navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() } - @Composable override fun GetContent(navController: NavController) { - val viewItem = viewModelTxs.tmpItemToShow + val viewModelTxs: TransactionsViewModel? = try { + navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() }.value + } catch (e: IllegalStateException) { + Toast.makeText(App.instance, "ViewModel is Null", Toast.LENGTH_SHORT).show() + null + } + + val viewItem = viewModelTxs?.tmpItemToShow if (viewItem == null) { navController.popBackStack(R.id.transactionInfoFragment, true) return diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterBlockchainFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterBlockchainFragment.kt index 00ed7cea608..903f6de65a0 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterBlockchainFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterBlockchainFragment.kt @@ -1,5 +1,6 @@ package io.horizontalsystems.bankwallet.modules.transactions +import android.widget.Toast import androidx.compose.foundation.Image import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column @@ -26,6 +27,7 @@ import androidx.navigation.NavController import androidx.navigation.navGraphViewModels import coil.compose.rememberAsyncImagePainter import io.horizontalsystems.bankwallet.R +import io.horizontalsystems.bankwallet.core.App import io.horizontalsystems.bankwallet.core.BaseComposeFragment import io.horizontalsystems.bankwallet.core.imageUrl import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme @@ -37,10 +39,20 @@ import io.horizontalsystems.marketkit.models.Blockchain class FilterBlockchainFragment : BaseComposeFragment() { - private val viewModel by navGraphViewModels(R.id.mainFragment) - @Composable override fun GetContent(navController: NavController) { + val viewModel: TransactionsViewModel? = try { + navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() }.value + } catch (e: IllegalStateException) { + Toast.makeText(App.instance, "ViewModel is Null", Toast.LENGTH_SHORT).show() + null + } + + if (viewModel == null) { + navController.popBackStack(R.id.filterBlockchainFragment, true) + return + } + FilterBlockchainScreen(navController, viewModel) } } diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterCoinFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterCoinFragment.kt index 3763c6b2c1c..e0707446912 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterCoinFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/FilterCoinFragment.kt @@ -1,5 +1,6 @@ package io.horizontalsystems.bankwallet.modules.transactions +import android.widget.Toast import androidx.compose.foundation.Image import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column @@ -27,6 +28,7 @@ import androidx.navigation.NavController import androidx.navigation.navGraphViewModels import coil.compose.rememberAsyncImagePainter import io.horizontalsystems.bankwallet.R +import io.horizontalsystems.bankwallet.core.App import io.horizontalsystems.bankwallet.core.BaseComposeFragment import io.horizontalsystems.bankwallet.core.badge import io.horizontalsystems.bankwallet.core.iconPlaceholder @@ -41,10 +43,20 @@ import io.horizontalsystems.bankwallet.ui.compose.components.HsBackButton class FilterCoinFragment : BaseComposeFragment() { - private val viewModel by navGraphViewModels(R.id.mainFragment) - @Composable override fun GetContent(navController: NavController) { + val viewModel: TransactionsViewModel? = try { + navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() }.value + } catch (e: IllegalStateException) { + Toast.makeText(App.instance, "ViewModel is Null", Toast.LENGTH_SHORT).show() + null + } + + if (viewModel == null) { + navController.popBackStack(R.id.filterCoinFragment, true) + return + } + FilterCoinScreen(navController, viewModel) } diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/TransactionsFilterFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/TransactionsFilterFragment.kt index 1ce334bf542..8fc6cd128a6 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/TransactionsFilterFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/transactions/TransactionsFilterFragment.kt @@ -1,5 +1,6 @@ package io.horizontalsystems.bankwallet.modules.transactions +import android.widget.Toast import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -25,6 +26,7 @@ import androidx.compose.ui.unit.dp import androidx.navigation.NavController import androidx.navigation.navGraphViewModels import io.horizontalsystems.bankwallet.R +import io.horizontalsystems.bankwallet.core.App import io.horizontalsystems.bankwallet.core.BaseComposeFragment import io.horizontalsystems.bankwallet.core.badge import io.horizontalsystems.bankwallet.core.slideFromRight @@ -44,10 +46,20 @@ import io.horizontalsystems.bankwallet.ui.compose.components.body_leah class TransactionsFilterFragment : BaseComposeFragment() { - private val viewModel by navGraphViewModels(R.id.mainFragment) - @Composable override fun GetContent(navController: NavController) { + val viewModel: TransactionsViewModel? = try { + navGraphViewModels(R.id.mainFragment) { TransactionsModule.Factory() }.value + } catch (e: IllegalStateException) { + Toast.makeText(App.instance, "ViewModel is Null", Toast.LENGTH_SHORT).show() + null + } + + if (viewModel == null) { + navController.popBackStack(R.id.filterCoinFragment, true) + return + } + FilterScreen( navController, viewModel