Updated on 2026-08-14
This commit is contained in:
parent
498e73d6df
commit
89fb66aa3e
8 changed files with 197 additions and 10 deletions
|
|
@ -487,6 +487,7 @@ internal class WalletModel @Inject constructor(
|
|||
is WalletsUpdateActionResolver.Action.ReloadWallets -> {
|
||||
reloadWarnings(action)
|
||||
}
|
||||
is WalletsUpdateActionResolver.Action.ReorderWallets -> reorderWallets(action)
|
||||
WalletsUpdateActionResolver.Action.EmptyWallets -> {
|
||||
Timber.w("Wallets list is empty!")
|
||||
}
|
||||
|
|
@ -496,6 +497,25 @@ internal class WalletModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun reorderWallets(action: WalletsUpdateActionResolver.Action.ReorderWallets) {
|
||||
val currentWalletId = stateHolder.getSelectedWalletId()
|
||||
val currentIndex = stateHolder.getWalletIndexByWalletId(userWalletId = currentWalletId)
|
||||
|
||||
stateHolder.update(
|
||||
transformer = ReorderWalletsTransformer(
|
||||
wallets = action.wallets,
|
||||
),
|
||||
)
|
||||
|
||||
val newIndex = stateHolder.getWalletIndexByWalletId(userWalletId = currentWalletId)
|
||||
|
||||
if (currentIndex != null && newIndex != null && currentIndex != newIndex) {
|
||||
scrollToWallet(prevIndex = currentIndex, newIndex = newIndex) {
|
||||
stateHolder.update { it.copy(selectedWalletIndex = newIndex) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun reloadWarnings(action: WalletsUpdateActionResolver.Action.ReloadWallets) {
|
||||
action.wallets.forEach {
|
||||
walletScreenContentLoader.load(
|
||||
|
|
|
|||
|
|
@ -82,6 +82,9 @@ internal class WalletsUpdateActionResolver @Inject constructor(
|
|||
isWalletsCountChanged(state, wallets) -> {
|
||||
getChangeWalletsListAction(state, wallets, selectedWallet)
|
||||
}
|
||||
isWalletsOrderChanged(state, wallets) -> {
|
||||
Action.ReorderWallets(wallets = wallets)
|
||||
}
|
||||
isAnotherWalletSelected(state, selectedWallet) -> {
|
||||
Action.ReinitializeNewWallet(
|
||||
prevWalletId = state.getPrevSelectedWallet().id,
|
||||
|
|
@ -121,6 +124,13 @@ internal class WalletsUpdateActionResolver @Inject constructor(
|
|||
return prevWalletsSize != walletsSize
|
||||
}
|
||||
|
||||
private fun isWalletsOrderChanged(state: WalletScreenState, wallets: List<UserWallet>): Boolean {
|
||||
val prevWalletIds = state.wallets.map { it.walletCardState.id }
|
||||
val newWalletIds = wallets.map { it.walletId }
|
||||
|
||||
return prevWalletIds != newWalletIds
|
||||
}
|
||||
|
||||
private fun getChangeWalletsListAction(
|
||||
state: WalletScreenState,
|
||||
wallets: List<UserWallet>,
|
||||
|
|
@ -394,6 +404,15 @@ internal class WalletsUpdateActionResolver @Inject constructor(
|
|||
val wallets: List<UserWallet>,
|
||||
) : Action()
|
||||
|
||||
data class ReorderWallets(
|
||||
val wallets: List<UserWallet>,
|
||||
) : Action() {
|
||||
|
||||
override fun toString(): String {
|
||||
return "ReorderWallets(wallets = ${wallets.joinToString { it.walletId.toString() }})"
|
||||
}
|
||||
}
|
||||
|
||||
data object EmptyWallets : Action()
|
||||
|
||||
data object Unknown : Action()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.tangem.feature.wallet.presentation.wallet.state.transformers
|
||||
|
||||
import com.tangem.domain.models.wallet.UserWallet
|
||||
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletScreenState
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
|
||||
/**
|
||||
* Transformer that reorders wallets according to the new order
|
||||
*
|
||||
* @property wallets wallets in the new order
|
||||
*/
|
||||
internal class ReorderWalletsTransformer(
|
||||
private val wallets: List<UserWallet>,
|
||||
) : WalletScreenStateTransformer {
|
||||
|
||||
override fun transform(prevState: WalletScreenState): WalletScreenState {
|
||||
val walletIdToIndex = wallets.withIndex().associate { it.value.walletId to it.index }
|
||||
|
||||
val reorderedWallets = prevState.wallets
|
||||
.sortedBy { walletIdToIndex[it.walletCardState.id] ?: Int.MAX_VALUE }
|
||||
.toImmutableList()
|
||||
|
||||
return if (reorderedWallets != prevState.wallets) {
|
||||
prevState.copy(wallets = reorderedWallets)
|
||||
} else {
|
||||
prevState
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue