Skip to content

Commit

Permalink
Handle getting transactions viewmodel on restore state
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Nov 6, 2024
1 parent 38cc5ea commit fcbdb1f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,14 +73,24 @@ import kotlinx.coroutines.launch

class MainFragment : BaseComposeFragment() {

private val transactionsViewModel by navGraphViewModels<TransactionsViewModel>(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?) {
Expand Down Expand Up @@ -357,3 +368,11 @@ private fun BadgedIcon(
}
}
}

fun NavController.safeGetBackStackEntry(destinationId: Int): NavBackStackEntry? {
return try {
this.getBackStackEntry(destinationId)
} catch (e: IllegalArgumentException) {
null
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -48,11 +50,16 @@ import io.horizontalsystems.bankwallet.ui.compose.components.WarningMessageCell

class TransactionInfoFragment : BaseComposeFragment() {

private val viewModelTxs by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment) { TransactionsModule.Factory() }

@Composable
override fun GetContent(navController: NavController) {
val viewItem = viewModelTxs.tmpItemToShow
val viewModelTxs: TransactionsViewModel? = try {
navGraphViewModels<TransactionsViewModel>(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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -37,10 +39,20 @@ import io.horizontalsystems.marketkit.models.Blockchain

class FilterBlockchainFragment : BaseComposeFragment() {

private val viewModel by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment)

@Composable
override fun GetContent(navController: NavController) {
val viewModel: TransactionsViewModel? = try {
navGraphViewModels<TransactionsViewModel>(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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -41,10 +43,20 @@ import io.horizontalsystems.bankwallet.ui.compose.components.HsBackButton

class FilterCoinFragment : BaseComposeFragment() {

private val viewModel by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment)

@Composable
override fun GetContent(navController: NavController) {
val viewModel: TransactionsViewModel? = try {
navGraphViewModels<TransactionsViewModel>(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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -44,10 +46,20 @@ import io.horizontalsystems.bankwallet.ui.compose.components.body_leah

class TransactionsFilterFragment : BaseComposeFragment() {

private val viewModel by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment)

@Composable
override fun GetContent(navController: NavController) {
val viewModel: TransactionsViewModel? = try {
navGraphViewModels<TransactionsViewModel>(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
Expand Down

0 comments on commit fcbdb1f

Please sign in to comment.