Skip to content

Commit

Permalink
Implement top coins icons fetched from server
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Dec 11, 2024
1 parent 076c7ad commit efcf736
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ dependencies {
implementation 'com.github.horizontalsystems:ethereum-kit-android:0c770e3'
implementation 'com.github.horizontalsystems:blockchain-fee-rate-kit-android:1d3bd49'
implementation 'com.github.horizontalsystems:binance-chain-kit-android:c1509a2'
implementation 'com.github.horizontalsystems:market-kit-android:4201f8f'
implementation 'com.github.horizontalsystems:market-kit-android:00b9f76'
implementation 'com.github.horizontalsystems:solana-kit-android:ce738d8'
implementation 'com.github.horizontalsystems:tron-kit-android:dc3dca7'
// Zcash SDK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ package io.horizontalsystems.bankwallet.modules.market.topsectors
import io.horizontalsystems.bankwallet.core.managers.MarketKitWrapper
import io.horizontalsystems.bankwallet.entities.Currency
import io.horizontalsystems.marketkit.models.CoinCategory
import io.horizontalsystems.marketkit.models.FullCoin
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class TopSectorsRepository(
private val marketKit: MarketKitWrapper,
) {
private var itemsCache: List<CoinCategory>? = null
private var itemsCache: List<CoinCategoryWithTopCoins>? = null

suspend fun get(baseCurrency: Currency, forceRefresh: Boolean): List<CoinCategory> =
suspend fun get(baseCurrency: Currency, forceRefresh: Boolean): List<CoinCategoryWithTopCoins> =
withContext(Dispatchers.IO) {
if (forceRefresh || itemsCache == null) {
val coinCategories = marketKit.coinCategoriesSingle(baseCurrency.code).blockingGet()
itemsCache = coinCategories
itemsCache ?: emptyList()
} else {
itemsCache ?: emptyList()
itemsCache = coinCategories.map { coinCategory ->
val topCoins = marketKit
.fullCoins(coinUids = coinCategory.topCoins)
.sortedBy { fullCoin -> coinCategory.topCoins.indexOf(fullCoin.coin.uid) }
CoinCategoryWithTopCoins(coinCategory, topCoins)
}
}
itemsCache ?: emptyList()
}
}

data class CoinCategoryWithTopCoins(
val coinCategory: CoinCategory,
val topCoins: List<FullCoin>
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
Expand All @@ -22,7 +21,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
Expand All @@ -42,9 +40,9 @@ import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme
import io.horizontalsystems.bankwallet.ui.compose.HSSwipeRefresh
import io.horizontalsystems.bankwallet.ui.compose.Select
import io.horizontalsystems.bankwallet.ui.compose.components.AlertGroup
import io.horizontalsystems.bankwallet.ui.compose.components.CoinImage
import io.horizontalsystems.bankwallet.ui.compose.components.HSpacer
import io.horizontalsystems.bankwallet.ui.compose.components.HeaderSorting
import io.horizontalsystems.bankwallet.ui.compose.components.HsImage
import io.horizontalsystems.bankwallet.ui.compose.components.ListErrorView
import io.horizontalsystems.bankwallet.ui.compose.components.MarketDataValueComponent
import io.horizontalsystems.bankwallet.ui.compose.components.SectionItemBorderedRowUniversalClear
Expand All @@ -62,9 +60,10 @@ fun TopSectorsScreen(
var openPeriodSelector by rememberSaveable { mutableStateOf(false) }
var openSortingSelector by rememberSaveable { mutableStateOf(false) }

val state = rememberSaveable(uiState.sortingField, uiState.timePeriod, saver = LazyListState.Saver) {
LazyListState(0, 0)
}
val state =
rememberSaveable(uiState.sortingField, uiState.timePeriod, saver = LazyListState.Saver) {
LazyListState(0, 0)
}

Column() {
HSSwipeRefresh(
Expand Down Expand Up @@ -181,39 +180,28 @@ fun TopSectorItem(
.padding(end = 16.dp)
.width(76.dp)
) {
val leftCoinModifier = Modifier
.size(32.dp)
.background(ComposeAppTheme.colors.tyler)
.clip(CircleShape)
.align(Alignment.TopEnd)
val middleCoinModifier = Modifier
.size(32.dp)
.clip(CircleShape)
.background(ComposeAppTheme.colors.tyler)
.align(Alignment.TopCenter)
val endCoinModifier = Modifier
val iconModifier = Modifier
.size(32.dp)
.clip(CircleShape)
.background(ComposeAppTheme.colors.tyler)
.align(Alignment.TopStart)
CoinImageMock(
uid = "ethereum",
modifier = leftCoinModifier

CoinImage(
coin = viewItem.coin3.coin,
modifier = iconModifier.align(Alignment.TopEnd)
)
CoinImageMock(
uid = "bitcoin",
modifier = middleCoinModifier
CoinImage(
coin = viewItem.coin2.coin,
modifier = iconModifier.align(Alignment.TopCenter)
)
CoinImageMock(
uid = "solana",
modifier = endCoinModifier
CoinImage(
coin = viewItem.coin1.coin,
modifier = iconModifier.align(Alignment.TopStart)
)
}
Row(
body_leah(
text = viewItem.coinCategory.name,
modifier = Modifier.weight(1f)
) {
body_leah(viewItem.coinCategory.name)
}
)
Column(
horizontalAlignment = Alignment.End
) {
Expand All @@ -226,14 +214,3 @@ fun TopSectorItem(
}
}
}

@Composable
private fun CoinImageMock(
uid: String,
modifier: Modifier,
colorFilter: ColorFilter? = null
) = HsImage(
url = "https://cdn.blocksdecoded.com/coin-icons/32px/$uid@3x.png",
modifier = modifier.clip(CircleShape),
colorFilter = colorFilter
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.horizontalsystems.bankwallet.modules.market.Value
import io.horizontalsystems.bankwallet.modules.market.sortedByDescendingNullLast
import io.horizontalsystems.bankwallet.modules.market.sortedByNullLast
import io.horizontalsystems.marketkit.models.CoinCategory
import io.horizontalsystems.marketkit.models.FullCoin
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -82,7 +83,11 @@ class TopSectorsViewModel(
topSectorsRepository.get(currencyManager.baseCurrency, forceRefresh)

val topCategoryWithDiffList = topSectors.map {
TopSectorWithDiff(it, changeValue(it, timePeriod))
TopSectorWithDiff(
it.coinCategory,
changeValue(it.coinCategory, timePeriod),
it.topCoins
)
}
val sortedTopSectors = topCategoryWithDiffList.sort(sortingField)
items = sortedTopSectors.map { getViewItem(it) }
Expand All @@ -107,7 +112,11 @@ class TopSectorsViewModel(
},
changeValue = item.diff?.let {
MarketDataValue.DiffNew(Value.Percent(it))
})
},
coin1 = item.topCoins[0],
coin2 = item.topCoins[1],
coin3 = item.topCoins[2],
)

private fun changeValue(category: CoinCategory, timePeriod: TimeDuration): BigDecimal? {
return when (timePeriod) {
Expand Down Expand Up @@ -178,7 +187,8 @@ class TopSectorsViewModel(

data class TopSectorWithDiff(
val coinCategory: CoinCategory,
val diff: BigDecimal?
val diff: BigDecimal?,
val topCoins: List<FullCoin>
)

data class TopSectorsUiState(
Expand All @@ -192,5 +202,8 @@ data class TopSectorsUiState(
data class TopSectorViewItem(
val coinCategory: CoinCategory,
val marketCapValue: String?,
val changeValue: MarketDataValue?
val changeValue: MarketDataValue?,
val coin1: FullCoin,
val coin2: FullCoin,
val coin3: FullCoin,
)

0 comments on commit efcf736

Please sign in to comment.