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 4, 2024
1 parent 38cc5ea commit 594fbeb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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.NavController
Expand Down Expand Up @@ -72,7 +73,16 @@ import kotlinx.coroutines.launch

class MainFragment : BaseComposeFragment() {

private val transactionsViewModel by navGraphViewModels<TransactionsViewModel>(R.id.mainFragment) { TransactionsModule.Factory() }
private val transactionsViewModel: TransactionsViewModel by lazy {
try {
navGraphViewModels<TransactionsViewModel>(R.id.mainFragment) { TransactionsModule.Factory() }.value
} catch (e: IllegalStateException) {
ViewModelProvider(
this,
TransactionsModule.Factory()
).get(TransactionsViewModel::class.java)
}
}

@Composable
override fun GetContent(navController: NavController) {
Expand Down
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 594fbeb

Please sign in to comment.