Updated on 2026-08-14
This commit is contained in:
parent
15fb5b3954
commit
ff1ac5d0e7
6 changed files with 79 additions and 37 deletions
|
|
@ -424,7 +424,7 @@ internal class AddCustomTokenViewModel @Inject constructor(
|
|||
selectedWallet: UserWallet,
|
||||
cryptoCurrency: CryptoCurrency,
|
||||
): Boolean {
|
||||
val currenciesList = getCurrenciesUseCase(selectedWallet.walletId).getOrElse { emptyList() }
|
||||
val currenciesList = getCurrenciesUseCase.getSync(selectedWallet.walletId).getOrElse { emptyList() }
|
||||
return when (cryptoCurrency) {
|
||||
is CryptoCurrency.Coin -> {
|
||||
currenciesList.any {
|
||||
|
|
|
|||
|
|
@ -33,11 +33,15 @@ import com.tangem.utils.Provider
|
|||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import com.tangem.utils.coroutines.Debouncer
|
||||
import com.tangem.utils.coroutines.Debouncer.Companion.DEFAULT_WAIT_TIME_MS
|
||||
import com.tangem.utils.coroutines.JobHolder
|
||||
import com.tangem.utils.coroutines.saveIn
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.plus
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.util.Collections
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import javax.inject.Inject
|
||||
import kotlin.collections.set
|
||||
import kotlin.properties.Delegates
|
||||
|
|
@ -62,8 +66,6 @@ internal class ManageTokensViewModel @Inject constructor(
|
|||
private val analyticsEventHandler: AnalyticsEventHandler,
|
||||
) : ViewModel(), ManageTokensClickIntents, ManageTokensUiEvents {
|
||||
|
||||
private val debouncer = Debouncer()
|
||||
|
||||
private val stateFactory = ManageTokensStateFactory(
|
||||
currentStateProvider = Provider { uiState },
|
||||
clickIntents = this,
|
||||
|
|
@ -73,11 +75,17 @@ internal class ManageTokensViewModel @Inject constructor(
|
|||
var uiState: ManageTokensState by mutableStateOf(stateFactory.getInitialState(flowOf(PagingData.from(emptyList()))))
|
||||
private set
|
||||
|
||||
private var allAddedCurrencies: MutableList<CryptoCurrency> = mutableListOf()
|
||||
private val currenciesListJobHolder: JobHolder = JobHolder()
|
||||
|
||||
private var wallets: List<UserWallet> by Delegates.notNull()
|
||||
private val debouncer = Debouncer()
|
||||
|
||||
private var addedCurrenciesByWallet: MutableMap<UserWallet, MutableList<CryptoCurrency>> = mutableMapOf()
|
||||
private var allAddedCurrencies: MutableList<CryptoCurrency> = Collections.synchronizedList(
|
||||
mutableListOf<CryptoCurrency>(),
|
||||
)
|
||||
|
||||
private var wallets: CopyOnWriteArrayList<UserWallet> by Delegates.notNull()
|
||||
|
||||
private var addedCurrenciesByWallet: MutableMap<UserWallet, MutableList<CryptoCurrency>> = ConcurrentHashMap()
|
||||
|
||||
private var selectedWallet: UserWallet? = null
|
||||
|
||||
|
|
@ -114,34 +122,53 @@ internal class ManageTokensViewModel @Inject constructor(
|
|||
getWalletsUseCase()
|
||||
.distinctUntilChanged()
|
||||
.collectLatest { userWallets ->
|
||||
wallets = userWallets.filter { it.isMultiCurrency && !it.isLocked }
|
||||
wallets.map { wallet ->
|
||||
val currencies = getCurrenciesUseCase(wallet.walletId).fold(
|
||||
ifLeft = { emptyList() },
|
||||
ifRight = { it },
|
||||
)
|
||||
allAddedCurrencies += currencies
|
||||
addedCurrenciesByWallet[wallet] = currencies.toMutableList()
|
||||
}
|
||||
withContext(dispatchers.main) {
|
||||
uiState = uiState.copy(tokens = getInitialTokensList())
|
||||
}
|
||||
selectedWallet = getSelectedWalletSyncUseCase().fold(
|
||||
ifLeft = { null },
|
||||
ifRight = { if (!it.isMultiCurrency || it.isLocked) null else it },
|
||||
)
|
||||
if (selectedWallet == null && wallets.isNotEmpty()) {
|
||||
selectWalletUseCase(wallets.first().walletId)
|
||||
selectedWallet = wallets.first()
|
||||
}
|
||||
updateDerivationNotificationState()
|
||||
withContext(dispatchers.main) {
|
||||
uiState = stateFactory.updateChooseWalletState(wallets, userWallets, selectedWallet)
|
||||
}
|
||||
launch {
|
||||
subscribeToCurrencies(userWallets)
|
||||
}.saveIn(currenciesListJobHolder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun subscribeToCurrencies(userWallets: List<UserWallet>) {
|
||||
wallets = CopyOnWriteArrayList(userWallets.filter { it.isMultiCurrency && !it.isLocked })
|
||||
|
||||
combine(wallets.map { getCurrenciesUseCase.invoke(it.walletId).distinctUntilChanged() }) {
|
||||
allAddedCurrencies.clear()
|
||||
addedCurrenciesByWallet.clear()
|
||||
|
||||
val walletsWithCurrencies = wallets.zip(
|
||||
it.map { currencyList ->
|
||||
currencyList.getOrElse {
|
||||
Timber.e("Couldn't retrieve currency list")
|
||||
emptyList()
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
allAddedCurrencies = walletsWithCurrencies.flatMap { it.second }.toMutableList()
|
||||
|
||||
walletsWithCurrencies.forEach { (wallet, currencies) ->
|
||||
addedCurrenciesByWallet[wallet] = currencies.toMutableList()
|
||||
}
|
||||
|
||||
withContext(dispatchers.main) {
|
||||
uiState = uiState.copy(tokens = getInitialTokensList())
|
||||
}
|
||||
selectedWallet = getSelectedWalletSyncUseCase().fold(
|
||||
ifLeft = { null },
|
||||
ifRight = { if (!it.isMultiCurrency || it.isLocked) null else it },
|
||||
)
|
||||
if (selectedWallet == null && wallets.isNotEmpty()) {
|
||||
selectWalletUseCase(wallets.first().walletId)
|
||||
selectedWallet = wallets.first()
|
||||
}
|
||||
updateDerivationNotificationState()
|
||||
withContext(dispatchers.main) {
|
||||
uiState = stateFactory.updateChooseWalletState(wallets, userWallets, selectedWallet)
|
||||
}
|
||||
}.collect()
|
||||
}
|
||||
|
||||
private fun getInitialTokensList(searchText: String = ""): Flow<PagingData<TokenItemState>> {
|
||||
return getGlobalTokenListUseCase(searchText = searchText).map {
|
||||
it.map { token -> tokenConverter.convert(token) }
|
||||
|
|
|
|||
|
|
@ -324,7 +324,7 @@ internal class SendViewModel @Inject constructor(
|
|||
.filterNot { it.walletId == userWalletId || it.isLocked }
|
||||
.map { wallet ->
|
||||
async(dispatchers.io) {
|
||||
getCryptoCurrenciesUseCase(wallet.walletId)
|
||||
getCryptoCurrenciesUseCase.getSync(wallet.walletId)
|
||||
.fold(
|
||||
ifRight = { currencyItem ->
|
||||
val walletCurrency = currencyItem.firstOrNull {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue