Skip to content

Commit

Permalink
Request data refresh when the account screen is first opened and disp…
Browse files Browse the repository at this point in the history
…layed the first batch of data
  • Loading branch information
micbakos-rdx committed Jul 3, 2024
1 parent 152305f commit f1d071f
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.babylon.wallet.android.domain.usecases.assets.GetNextNFTsPageUseCase
import com.babylon.wallet.android.domain.usecases.assets.GetWalletAssetsUseCase
import com.babylon.wallet.android.domain.usecases.assets.UpdateLSUsInfo
import com.babylon.wallet.android.domain.usecases.transaction.SendClaimRequestUseCase
import com.babylon.wallet.android.presentation.account.AccountViewModel.State.RefreshType.*
import com.babylon.wallet.android.presentation.common.OneOffEvent
import com.babylon.wallet.android.presentation.common.OneOffEventHandler
import com.babylon.wallet.android.presentation.common.OneOffEventHandlerImpl
Expand All @@ -24,7 +25,6 @@ import com.babylon.wallet.android.presentation.common.UiMessage
import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.presentation.transfer.assets.AssetsTab
import com.babylon.wallet.android.presentation.ui.composables.assets.AssetsViewState
import com.babylon.wallet.android.presentation.wallet.WalletViewModel.RefreshType
import com.babylon.wallet.android.utils.AppEvent
import com.babylon.wallet.android.utils.AppEvent.RestoredMnemonic
import com.babylon.wallet.android.utils.AppEventBus
Expand Down Expand Up @@ -107,7 +107,13 @@ class AccountViewModel @Inject constructor(
combine(
accountFlow,
refreshFlow.onStart {
loadAccountDetails(refreshType = State.RefreshType.Manual(overrideCache = false, showRefreshIndicator = false))
loadAccountDetails(
refreshType = Manual(
overrideCache = false,
showRefreshIndicator = false,
firstRequest = true
)
)
}
) { account, refreshEvent ->
_state.update { it.onAccount(account, refreshEvent) }
Expand All @@ -120,7 +126,7 @@ class AccountViewModel @Inject constructor(
).catch { error ->
_state.update {
it.copy(
refreshType = State.RefreshType.None,
refreshType = None,
uiMessage = UiMessage.ErrorMessage(error = error)
)
}
Expand All @@ -132,7 +138,7 @@ class AccountViewModel @Inject constructor(
_state.update { state ->
state.copy(
accountWithAssets = state.accountWithAssets?.copy(assets = accountWithAssets.assets),
refreshType = State.RefreshType.None
refreshType = None
)
}

Expand All @@ -157,6 +163,12 @@ class AccountViewModel @Inject constructor(
)
}
}

if (refreshState.isFirstRefreshRequest()) {
loadAccountDetails(
refreshType = Manual(overrideCache = true, showRefreshIndicator = false, firstRequest = false)
)
}
}.flowOn(defaultDispatcher).launchIn(viewModelScope)
}

Expand All @@ -167,14 +179,15 @@ class AccountViewModel @Inject constructor(
}.collect { event ->
when (event) {
AppEvent.RefreshAssetsNeeded -> loadAccountDetails(
refreshType = State.RefreshType.Manual(
refreshType = Manual(
overrideCache = true,
showRefreshIndicator = true
showRefreshIndicator = true,
firstRequest = false
)
)

RestoredMnemonic -> loadAccountDetails(
refreshType = State.RefreshType.Manual(overrideCache = false, showRefreshIndicator = false)
refreshType = Manual(overrideCache = false, showRefreshIndicator = false, firstRequest = false)
)

else -> {}
Expand Down Expand Up @@ -215,7 +228,7 @@ class AccountViewModel @Inject constructor(
}

fun refresh() {
loadAccountDetails(refreshType = State.RefreshType.Manual(overrideCache = true, showRefreshIndicator = true))
loadAccountDetails(refreshType = Manual(overrideCache = true, showRefreshIndicator = true, firstRequest = false))
}

fun onShowHideBalanceToggle(isVisible: Boolean) {
Expand Down Expand Up @@ -368,7 +381,7 @@ class AccountViewModel @Inject constructor(
data class State(
val accountWithAssets: AccountWithAssets? = null,
private val pricesState: PricesState = PricesState.None,
val refreshType: RefreshType = RefreshType.None,
val refreshType: RefreshType = None,
val nonFungiblesWithPendingNFTs: Set<ResourceAddress> = setOf(),
val pendingStakeUnits: Boolean = false,
val securityPrompts: List<SecurityPromptType>? = null,
Expand All @@ -383,14 +396,17 @@ class AccountViewModel @Inject constructor(
val overrideCache: Boolean
val showRefreshIndicator: Boolean

fun isFirstRefreshRequest(): Boolean = if (this is Manual) this.firstRequest else false

data object None : RefreshType {
override val overrideCache: Boolean = false
override val showRefreshIndicator: Boolean = false
}

data class Manual(
override val overrideCache: Boolean,
override val showRefreshIndicator: Boolean
override val showRefreshIndicator: Boolean,
val firstRequest: Boolean
) : RefreshType

data object Automatic : RefreshType {
Expand Down Expand Up @@ -459,7 +475,7 @@ class AccountViewModel @Inject constructor(
nonFungibles = accountWithAssets.assets.nonFungibles.mapWhen(
predicate = {
it.collection.address == forResource.address &&
it.collection.items.size < forResource.items.size
it.collection.items.size < forResource.items.size
},
mutation = { NonFungibleCollection(forResource) }
)
Expand Down

0 comments on commit f1d071f

Please sign in to comment.