Skip to content

Commit

Permalink
Add rank to Etf items
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Jun 13, 2024
1 parent ca4c231 commit 17f2a52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
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

0 comments on commit 17f2a52

Please sign in to comment.