Updated on 2026-08-14

This commit is contained in:
Tangem 2023-09-27 12:15:54 +03:00
parent 3fe6b7008a
commit 1d151c11a1
4 changed files with 16 additions and 25 deletions

View file

@ -16,15 +16,14 @@ import com.tangem.feature.wallet.presentation.organizetokens.utils.converter.err
import com.tangem.feature.wallet.presentation.organizetokens.utils.converter.items.CryptoCurrencyToDraggableItemConverter
import com.tangem.feature.wallet.presentation.organizetokens.utils.converter.items.NetworkGroupToDraggableItemsConverter
import com.tangem.feature.wallet.presentation.organizetokens.utils.converter.items.TokenListToListStateConverter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
internal class OrganizeTokensStateHolder(
private val intents: OrganizeTokensIntents,
private val dragAndDropIntents: DragAndDropIntents,
private val appCurrencyProvider: Provider<AppCurrency>,
private val onSubscription: () -> Unit,
stateFlowScope: CoroutineScope,
) {
private val stateFlowInternal: MutableStateFlow<OrganizeTokensState> = MutableStateFlow(getInitialState())
@ -52,12 +51,6 @@ internal class OrganizeTokensStateHolder(
}
val stateFlow: StateFlow<OrganizeTokensState> = stateFlowInternal
.onSubscription { onSubscription() }
.stateIn(
scope = stateFlowScope,
started = SharingStarted.WhileSubscribed(),
initialValue = getInitialState(),
)
fun updateStateWithTokenList(tokenList: TokenList) {
updateState { tokenListConverter.convert(tokenList) }

View file

@ -50,14 +50,9 @@ internal class OrganizeTokensViewModel @Inject constructor(
)
private val stateHolder = OrganizeTokensStateHolder(
stateFlowScope = viewModelScope,
intents = this,
dragAndDropIntents = dragAndDropAdapter,
appCurrencyProvider = Provider(selectedAppCurrencyFlow::value),
onSubscription = {
bootstrapTokenList()
bootstrapDragAndDropUpdates()
},
)
private val userWalletId: UserWalletId by lazy {
@ -72,6 +67,9 @@ internal class OrganizeTokensViewModel @Inject constructor(
override fun onCreate(owner: LifecycleOwner) {
analyticsEventsHandler.send(PortfolioOrganizeTokensAnalyticsEvent.ScreenOpened)
bootstrapTokenList()
bootstrapDragAndDropUpdates()
}
override fun onBackClick() {
@ -166,7 +164,7 @@ internal class OrganizeTokensViewModel @Inject constructor(
}
private fun bootstrapDragAndDropUpdates() {
dragAndDropAdapter.stateFlow
dragAndDropAdapter.dragAndDropUpdates
.distinctUntilChanged()
.onEach {
stateHolder.updateStateWithManualSorting(it)

View file

@ -9,8 +9,8 @@ import com.tangem.feature.wallet.presentation.organizetokens.utils.common.uniteI
import com.tangem.feature.wallet.presentation.organizetokens.utils.common.updateItems
import kotlinx.collections.immutable.mutate
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import org.burnoutcrew.reorderable.ItemPosition
internal class DragAndDropAdapter(
@ -22,15 +22,15 @@ internal class DragAndDropAdapter(
private val currentListState: OrganizeTokensListState
get() = listStateProvider.invoke()
private val listStateFlowInternal: MutableSharedFlow<OrganizeTokensListState> = MutableSharedFlow(
private val dragAndDropUpdatesInternal: MutableSharedFlow<OrganizeTokensListState> = MutableSharedFlow(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
private var currentDraggingItem: DraggableItem? = null
val stateFlow: Flow<OrganizeTokensListState>
get() = listStateFlowInternal
val dragAndDropUpdates: SharedFlow<OrganizeTokensListState>
get() = dragAndDropUpdatesInternal
override fun canDragItemOver(dragOver: ItemPosition, dragging: ItemPosition): Boolean {
val items = when (val listState = currentListState) {
@ -97,7 +97,7 @@ internal class DragAndDropAdapter(
private fun updateListState(block: OrganizeTokensListState.() -> List<DraggableItem>) {
val updatedState = currentListState.updateItems { block(currentListState) }
listStateFlowInternal.tryEmit(updatedState)
dragAndDropUpdatesInternal.tryEmit(updatedState)
}
private fun findItemsToMove(

View file

@ -58,10 +58,10 @@ internal class DefaultWalletRouter(private val reduxNavController: ReduxNavContr
WalletRoute.OrganizeTokens.route,
arguments = listOf(navArgument(WalletRoute.userWalletIdKey) { type = NavType.StringType }),
) {
val viewModel: OrganizeTokensViewModel = hiltViewModel<OrganizeTokensViewModel>()
.apply {
router = this@DefaultWalletRouter
}
val viewModel = hiltViewModel<OrganizeTokensViewModel>().apply {
router = this@DefaultWalletRouter
}
LocalLifecycleOwner.current.lifecycle.addObserver(viewModel)
val uiState by viewModel.uiState.collectAsStateWithLifecycle()