Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting to Market Filters page #7504

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import io.horizontalsystems.marketkit.models.FullCoin
@Immutable
data class MarketViewItem(
val fullCoin: FullCoin,
val coinRate: String,
val subtitle: String,
val value: String,
val marketDataValue: MarketDataValue,
val rank: String?,
val favorited: Boolean,
Expand Down Expand Up @@ -52,6 +53,7 @@ data class MarketViewItem(
): MarketViewItem {
return MarketViewItem(
marketItem.fullCoin,
marketItem.fullCoin.coin.name,
App.numberFormatter.formatFiatFull(
marketItem.rate.value,
marketItem.rate.currency.symbol
Expand Down Expand Up @@ -93,6 +95,7 @@ data class MarketViewItem(
}
return MarketViewItem(
marketItem.fullCoin,
marketItem.fullCoin.coin.name,
App.numberFormatter.formatFiatFull(
marketItem.rate.value,
marketItem.rate.currency.symbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ fun EtfPage(
}
items(uiState.viewItems) { viewItem ->
MarketCoinClear(
subtitle = viewItem.subtitle,
title = viewItem.title,
subtitle = viewItem.subtitle,
coinIconUrl = viewItem.iconUrl,
coinIconPlaceholder = R.drawable.ic_platform_placeholder_24,
value = viewItem.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package io.horizontalsystems.bankwallet.modules.market.filtersresult

import io.horizontalsystems.bankwallet.core.managers.MarketFavoritesManager
import io.horizontalsystems.bankwallet.entities.DataState
import io.horizontalsystems.bankwallet.modules.market.MarketField
import io.horizontalsystems.bankwallet.modules.market.MarketItem
import io.horizontalsystems.bankwallet.modules.market.SortingField
import io.horizontalsystems.bankwallet.modules.market.category.MarketCategoryModule
import io.horizontalsystems.bankwallet.modules.market.category.MarketItemWrapper
import io.horizontalsystems.bankwallet.modules.market.filters.IMarketListFetcher
import io.horizontalsystems.bankwallet.modules.market.sort
import io.horizontalsystems.bankwallet.ui.compose.Select
import io.reactivex.subjects.BehaviorSubject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -28,16 +25,14 @@ class MarketFiltersResultService(

var marketItems: List<MarketItem> = listOf()

val sortingFields = SortingField.values().toList()
private val marketFields = MarketField.values().toList()
var sortingField = SortingField.HighestCap
var marketField = MarketField.PriceDiff
val sortingFields = listOf(
SortingField.HighestCap,
SortingField.LowestCap,
SortingField.TopGainers,
SortingField.TopLosers,
)

val menu: MarketCategoryModule.Menu
get() = MarketCategoryModule.Menu(
Select(sortingField, sortingFields),
Select(marketField, marketFields)
)
var sortingField = SortingField.HighestCap

private val coroutineScope = CoroutineScope(Dispatchers.Default)
private var fetchJob: Job? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
package io.horizontalsystems.bankwallet.modules.market.filtersresult

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.horizontalsystems.bankwallet.core.stats.StatEvent
import io.horizontalsystems.bankwallet.core.stats.StatPage
import io.horizontalsystems.bankwallet.core.stats.stat
import io.horizontalsystems.bankwallet.core.stats.statField
import io.horizontalsystems.bankwallet.entities.DataState
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.ViewModelUiState
import io.horizontalsystems.bankwallet.entities.ViewState
import io.horizontalsystems.bankwallet.modules.market.MarketField
import io.horizontalsystems.bankwallet.modules.market.MarketDataValue
import io.horizontalsystems.bankwallet.modules.market.MarketViewItem
import io.horizontalsystems.bankwallet.modules.market.SortingField
import io.horizontalsystems.bankwallet.modules.market.category.MarketItemWrapper
import io.horizontalsystems.bankwallet.modules.market.topcoins.SelectorDialogState
import io.horizontalsystems.bankwallet.ui.compose.Select
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.asFlow

class MarketFiltersResultViewModel(
private val service: MarketFiltersResultService,
) : ViewModel() {
) : ViewModelUiState<MarketFiltersUiState>() {

private var marketItems: List<MarketItemWrapper> = listOf()

var viewState by mutableStateOf<ViewState>(ViewState.Loading)
private set

var viewItemsState by mutableStateOf<List<MarketViewItem>>(listOf())
private set

var selectorDialogState by mutableStateOf<SelectorDialogState>(SelectorDialogState.Closed)
private set

var menuState by mutableStateOf(service.menu)
private set
private var viewState: ViewState = ViewState.Loading
private var viewItemsState: List<MarketViewItem> = listOf()

init {
syncMenu()

viewModelScope.launch {
service.stateObservable.asFlow().collect {
syncState(it)
service.stateObservable.asFlow().collect { state ->
state.viewState?.let {
viewState = it
emitState()
}

state.dataOrNull?.let {
marketItems = it
syncMarketViewItems()
emitState()
}
}
}

service.start()
}

override fun createState() = MarketFiltersUiState(
viewItems = viewItemsState,
viewState = viewState,
sortingField = service.sortingField,
selectSortingField = Select(service.sortingField, service.sortingFields)
)

override fun onCleared() {
service.stop()
}
Expand All @@ -58,28 +54,9 @@ class MarketFiltersResultViewModel(
service.refresh()
}

fun showSelectorMenu() {
selectorDialogState =
SelectorDialogState.Opened(Select(service.sortingField, service.sortingFields))
}

fun onSelectorDialogDismiss() {
selectorDialogState = SelectorDialogState.Closed
}

fun onSelectSortingField(sortingField: SortingField) {
service.updateSortingField(sortingField)
selectorDialogState = SelectorDialogState.Closed
syncMenu()
}

fun marketFieldSelected(marketField: MarketField) {
service.marketField = marketField

syncMarketViewItems()
syncMenu()

stat(page = StatPage.AdvancedSearchResults, event = StatEvent.SwitchField(marketField.statField))
emitState()
}

fun onAddFavorite(uid: String) {
Expand All @@ -90,30 +67,32 @@ class MarketFiltersResultViewModel(
service.removeFavorite(uid)
}

private fun syncState(state: DataState<List<MarketItemWrapper>>) {
viewModelScope.launch {
state.viewState?.let {
viewState = it
}

state.dataOrNull?.let {
marketItems = it

syncMarketViewItems()
}

syncMenu()
}
}

private fun syncMenu() {
menuState = service.menu
}

private fun syncMarketViewItems() {
viewItemsState = marketItems.map {
MarketViewItem.create(it.marketItem, service.marketField, it.favorited)
viewItemsState = marketItems.map { itemWrapper ->
val marketCap = App.numberFormatter.formatFiatShort(
itemWrapper.marketItem.marketCap.value,
itemWrapper.marketItem.marketCap.currency.symbol,
2
)
MarketViewItem(
fullCoin = itemWrapper.marketItem.fullCoin,
subtitle = marketCap,
value = App.numberFormatter.formatFiatFull(
itemWrapper.marketItem.rate.value,
itemWrapper.marketItem.rate.currency.symbol
),
marketDataValue = MarketDataValue.Diff(itemWrapper.marketItem.diff),
rank = itemWrapper.marketItem.rank?.toString(),
favorited = itemWrapper.favorited
)
}.toList()
}

}

data class MarketFiltersUiState(
val viewItems: List<MarketViewItem>,
val viewState: ViewState,
val sortingField: SortingField,
val selectSortingField: Select<SortingField>
)
Loading
Loading