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

Make fixes to Market Etf #7561

Merged
merged 2 commits into from
Jun 13, 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 @@ -8,7 +8,7 @@ import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import java.text.NumberFormat
import java.util.*
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap

class NumberFormatter(
Expand Down Expand Up @@ -115,7 +115,8 @@ class NumberFormatter(
when (value) {
is Value.Currency -> {
val currencyValue = value.currencyValue
formatFiatShort(currencyValue.value, currencyValue.currency.symbol, currencyValue.currency.decimal)
val formatted = formatFiatShort(currencyValue.value.abs(), currencyValue.currency.symbol, currencyValue.currency.decimal)
sign(value.currencyValue.value) + formatted
}
is Value.Percent -> {
format(value.percent.abs(), 0, 2, sign(value.percent), "%")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class MarketViewModel(
globalMarket.btcDominance?.let {
App.numberFormatter.format(it, 0, 2, suffix = "%")
} ?: "-",
globalMarket.btcDominance?.let { getDiff(it) } ?: "----",
globalMarket.btcDominance?.let { it > BigDecimal.ZERO } ?: false,
globalMarket.btcDominanceChange?.let { getDiff(it) } ?: "----",
globalMarket.btcDominanceChange?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.TotalMarketCap
),
MarketOverviewViewItem(
Expand All @@ -105,7 +105,7 @@ class MarketViewModel(
?: "-",
globalMarket.etfDailyInflow?.let {
val sign = if (it >= BigDecimal.ZERO) "+" else "-"
"$sign${formatFiatShortened(it, baseCurrency.symbol)}"
"$sign${formatFiatShortened(it.abs(), baseCurrency.symbol)}"
} ?: "----",
globalMarket.etfDailyInflow?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.Etf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ fun ChartEtf(loading: Boolean, etfPoints: List<EtfPoint>, currency: Currency) {
}

val dailyInflowStr = dailyInflow?.let {
App.numberFormatter.formatFiatShort(it.abs(), currency.symbol, currency.decimal)
val sign = when {
it == BigDecimal.ZERO -> ""
it < BigDecimal.ZERO -> "-"
else -> "+"
}
sign + App.numberFormatter.formatFiatShort(it.abs(), currency.symbol, currency.decimal)
}
val dailyInflowPositive = dailyInflow != null && dailyInflow > BigDecimal.ZERO

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.horizontalsystems.bankwallet.modules.market.MarketDataValue
import io.horizontalsystems.bankwallet.modules.market.TimeDuration
import io.horizontalsystems.bankwallet.ui.compose.TranslatableString
import io.horizontalsystems.bankwallet.ui.compose.WithTranslatableTitle
import io.horizontalsystems.marketkit.models.Etf
import io.horizontalsystems.marketkit.models.EtfPoint

object EtfModule {
Expand All @@ -33,6 +34,11 @@ object EtfModule {
val rank: String?,
)

data class RankedEtf(
val etf: Etf,
val rank: Int
)

@Immutable
data class UiState(
val viewItems: List<EtfViewItem>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.horizontalsystems.bankwallet.modules.market.MarketDataValue
import io.horizontalsystems.bankwallet.modules.market.TimeDuration
import io.horizontalsystems.bankwallet.modules.market.Value
import io.horizontalsystems.bankwallet.modules.market.etf.EtfModule.EtfViewItem
import io.horizontalsystems.bankwallet.modules.market.etf.EtfModule.RankedEtf
import io.horizontalsystems.marketkit.models.Etf
import io.horizontalsystems.marketkit.models.EtfPoint
import io.horizontalsystems.marketkit.models.HsTimePeriod
Expand Down Expand Up @@ -45,7 +46,7 @@ class EtfViewModel(
private var marketDataJob: Job? = null
private var sortBy: EtfModule.SortBy = sortByOptions.first()
private var timeDuration: TimeDuration = timeDurations.first()
private var cachedEtfs: List<Etf> = listOf()
private var cachedEtfs: List<RankedEtf> = listOf()
private var chartDataLoading = true
private var etfPoints = listOf<EtfPoint>()

Expand Down Expand Up @@ -88,6 +89,8 @@ class EtfViewModel(
marketDataJob = viewModelScope.launch(Dispatchers.IO) {
try {
cachedEtfs = marketKit.etfs(currencyManager.baseCurrency.code).await()
.sortedByDescending { it.totalAssets }
.mapIndexed{ index, etf -> RankedEtf(etf, index + 1) }
updateViewItems()

viewState = ViewState.Success
Expand All @@ -102,36 +105,36 @@ class EtfViewModel(

private fun updateViewItems() {
val sorted = when (sortBy) {
EtfModule.SortBy.HighestAssets -> cachedEtfs.sortedByDescending { it.totalAssets }
EtfModule.SortBy.LowestAssets -> cachedEtfs.sortedBy { it.totalAssets }
EtfModule.SortBy.HighestAssets -> cachedEtfs.sortedByDescending { it.etf.totalAssets }
EtfModule.SortBy.LowestAssets -> cachedEtfs.sortedBy { it.etf.totalAssets }
EtfModule.SortBy.Inflow -> cachedEtfs.sortedByDescending {
it.priceChangeValue(
it.etf.priceChangeValue(
timeDuration
)
}

EtfModule.SortBy.Outflow -> cachedEtfs.sortedBy { it.priceChangeValue(timeDuration) }
EtfModule.SortBy.Outflow -> cachedEtfs.sortedBy { it.etf.priceChangeValue(timeDuration) }
}
viewItems = sorted.map { etf ->
etfViewItem(etf, timeDuration)
}
}

private fun etfViewItem(etf: Etf, timeDuration: TimeDuration) = EtfViewItem(
title = etf.ticker,
iconUrl = "https://cdn.blocksdecoded.com/etf-tresuries/${etf.ticker}@3x.png",
subtitle = etf.name,
value = etf.totalAssets?.let {
private fun etfViewItem(rankedEtf: RankedEtf, timeDuration: TimeDuration) = EtfViewItem(
title = rankedEtf.etf.ticker,
iconUrl = "https://cdn.blocksdecoded.com/etf-tresuries/${rankedEtf.etf.ticker}@3x.png",
subtitle = rankedEtf.etf.name,
value = rankedEtf.etf.totalAssets?.let {
App.numberFormatter.formatFiatShort(it, currencyManager.baseCurrency.symbol, 0)
},
subvalue = etf.priceChangeValue(timeDuration)?.let {
subvalue = rankedEtf.etf.priceChangeValue(timeDuration)?.let {
MarketDataValue.DiffNew(
Value.Currency(
CurrencyValue(currencyManager.baseCurrency, it)
)
)
},
rank = null
rank = rankedEtf.rank.toString()
)

private fun refreshWithMinLoadingSpinnerPeriod() {
Expand Down
Loading