Skip to content
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 @@ -3,65 +3,16 @@ package com.gemwallet.android.data.coordinators.asset
import com.gemwallet.android.application.assets.coordinators.GetActiveAssetsInfo
import com.gemwallet.android.data.repositories.assets.AssetsRepository
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.domains.asset.aggregates.AssetPriceDataAggregate
import com.gemwallet.android.model.AssetInfo
import com.gemwallet.android.model.CurrencyFormatter
import com.gemwallet.android.model.ValueFormatter
import com.wallet.core.primitives.Currency
import com.gemwallet.android.domains.asset.aggregates.toAssetInfoDataAggregate
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import java.math.BigDecimal

class GetActiveAssetsInfoImpl(
private val assetsRepository: AssetsRepository,
) : GetActiveAssetsInfo {
override fun getAssetsInfo(hideBalance: Boolean): Flow<List<AssetInfoDataAggregate>> =
assetsRepository.getAssetsInfo()
.map { items -> items.map { it.toAssetInfoDataAggregate(hideBalance) } }
.map { items -> items.map { it.toAssetInfoDataAggregate(hideBalance = hideBalance) } }
.distinctUntilChanged()
}

internal fun AssetInfo.toAssetInfoDataAggregate(
hideBalance: Boolean,
): AssetInfoDataAggregate {
val currency = price?.currency ?: Currency.USD
val assetPrice = price?.price
val priceValue = assetPrice?.price?.takeIf(Double::isFinite)
val changePercentage = assetPrice?.priceChangePercentage24h?.takeIf(Double::isFinite)
val assetBalance = balance
val formattedBalance = if (hideBalance) {
"*****"
} else {
ValueFormatter(style = ValueFormatter.Style.Short)
.string(BigDecimal.valueOf(assetBalance.totalAmount), asset.symbol)
}
val balanceEquivalent = if (hideBalance) {
"*****"
} else {
priceValue
?.takeUnless { it == 0.0 }
?.let { CurrencyFormatter(currency = currency).string(assetBalance.totalAmount * it) }
.orEmpty()
}
val price = assetPrice?.let {
AssetPriceDataAggregate(
currency = currency,
value = priceValue,
changePercentage = changePercentage,
)
}

return AssetInfoDataAggregate(
id = asset.id,
asset = asset,
title = asset.name,
balance = formattedBalance,
balanceEquivalent = balanceEquivalent,
isZeroBalance = assetBalance.totalAmount == 0.0,
price = price,
position = position,
pinned = metadata?.isPinned == true,
accountAddress = owner?.address.orEmpty(),
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gemwallet.android.features.asset_select.presents.views

import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate

fun getAssetBadge(item: AssetItemUIModel): String {
fun getAssetBadge(item: AssetInfoDataAggregate): String {
return if (item.asset.symbol == item.asset.name) "" else item.asset.symbol
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ import com.gemwallet.android.ui.components.filters.AssetsFilter
import com.gemwallet.android.ui.components.image.AssetIcon
import com.gemwallet.android.ui.components.list_item.AssetContextActions
import com.gemwallet.android.ui.components.list_item.AssetContextMenuRow
import com.gemwallet.android.ui.components.list_item.AssetInfoUIModel
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.ui.components.list_item.AssetListItem
import com.gemwallet.android.ui.components.list_item.PinnedAssetsHeaderItem
import com.gemwallet.android.ui.components.list_item.SubheaderItem
Expand Down Expand Up @@ -80,13 +79,13 @@ import kotlinx.coroutines.flow.drop
@Composable
fun AssetSelectScene(
title: String,
popular: ImmutableList<AssetItemUIModel>,
pinned: ImmutableList<AssetItemUIModel>,
unpinned: ImmutableList<AssetItemUIModel>,
popular: ImmutableList<AssetInfoDataAggregate>,
pinned: ImmutableList<AssetInfoDataAggregate>,
unpinned: ImmutableList<AssetInfoDataAggregate>,
recent: ImmutableList<Asset>,
state: UIState,
titleBadge: (AssetItemUIModel) -> String?,
support: ((AssetItemUIModel) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetInfoDataAggregate) -> String?,
support: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)?,
query: TextFieldState,
tags: List<AssetTag?>,
selectedTag: AssetTag?,
Expand All @@ -96,7 +95,7 @@ fun AssetSelectScene(
balanceFilter: Boolean = false,
searchable: Boolean = true,
onAction: (AssetSelectAction) -> Unit,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)? = null,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)? = null,
actions: @Composable RowScope.() -> Unit = {},
contextActions: AssetContextActions = AssetContextActions.Empty,
recentsSheetEnabled: Boolean = false,
Expand Down Expand Up @@ -138,13 +137,13 @@ fun AssetSelectScene(
@Composable
fun AssetSelectScene(
title: @Composable () -> Unit,
popular: ImmutableList<AssetItemUIModel>,
pinned: ImmutableList<AssetItemUIModel>,
unpinned: ImmutableList<AssetItemUIModel>,
popular: ImmutableList<AssetInfoDataAggregate>,
pinned: ImmutableList<AssetInfoDataAggregate>,
unpinned: ImmutableList<AssetInfoDataAggregate>,
recent: ImmutableList<Asset>,
state: UIState,
titleBadge: (AssetItemUIModel) -> String?,
support: ((AssetItemUIModel) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetInfoDataAggregate) -> String?,
support: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)?,
query: TextFieldState,
tags: List<AssetTag?>,
selectedTag: AssetTag?,
Expand All @@ -154,7 +153,7 @@ fun AssetSelectScene(
balanceFilter: Boolean = false,
searchable: Boolean = true,
onAction: (AssetSelectAction) -> Unit,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)? = null,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)? = null,
actions: @Composable RowScope.() -> Unit = {},
contextActions: AssetContextActions = AssetContextActions.Empty,
recentsSheetEnabled: Boolean = false,
Expand Down Expand Up @@ -301,12 +300,12 @@ fun AssetSelectScene(
}

private fun LazyListScope.assets(
items: List<AssetItemUIModel>,
items: List<AssetInfoDataAggregate>,
group: AssetsGroupType,
onSelect: ((AssetId) -> Unit)?,
support: ((AssetItemUIModel) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetItemUIModel) -> String?,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)?,
support: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetInfoDataAggregate) -> String?,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)?,
longPressedAsset: MutableState<AssetId?>,
contextActions: AssetContextActions,
) {
Expand All @@ -318,11 +317,11 @@ private fun LazyListScope.assets(
}

fun LazyListScope.assetRows(
items: List<AssetItemUIModel>,
items: List<AssetInfoDataAggregate>,
onSelect: ((AssetId) -> Unit)?,
support: ((AssetItemUIModel) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetItemUIModel) -> String?,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)?,
support: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetInfoDataAggregate) -> String?,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)?,
longPressedAsset: MutableState<AssetId?>,
contextActions: AssetContextActions,
indexOffset: Int = 0,
Expand All @@ -345,19 +344,19 @@ fun LazyListScope.assetRows(
@Composable
fun AssetSelectRow(
position: ListPosition,
item: AssetItemUIModel,
support: ((AssetItemUIModel) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetItemUIModel) -> String?,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)?,
item: AssetInfoDataAggregate,
support: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)?,
titleBadge: (AssetInfoDataAggregate) -> String?,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)?,
longPressedAsset: MutableState<AssetId?>,
onSelect: ((AssetId) -> Unit)?,
contextActions: AssetContextActions,
) {
AssetContextMenuRow(
assetId = item.asset.id,
address = item.owner,
isPinned = item.metadata?.isPinned == true,
isBalanceEnabled = item.metadata?.isBalanceEnabled == true,
address = item.accountAddress,
isPinned = item.pinned,
isBalanceEnabled = item.balanceEnabled,
longPressed = longPressedAsset,
actions = contextActions,
onClick = { onSelect?.invoke(item.asset.id) },
Expand Down Expand Up @@ -446,9 +445,9 @@ private fun LazyListScope.recent(
fun PreviewAssetScreenUI() {
MaterialTheme {
AssetSelectScene(
pinned = emptyList<AssetInfoUIModel>().toImmutableList(),
unpinned = emptyList<AssetInfoUIModel>().toImmutableList(),
popular = emptyList<AssetInfoUIModel>().toImmutableList(),
pinned = emptyList<AssetInfoDataAggregate>().toImmutableList(),
unpinned = emptyList<AssetInfoDataAggregate>().toImmutableList(),
popular = emptyList<AssetInfoDataAggregate>().toImmutableList(),
recent = emptyList<Asset>().toImmutableList(),
state = UIState.Idle,
title = "Send",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.gemwallet.android.ext.networkName
import com.gemwallet.android.ext.type
import com.gemwallet.android.model.RecentType
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.ui.components.list_item.ListItemSupportText
import com.gemwallet.android.features.asset_select.viewmodels.BaseAssetSelectViewModel
import com.gemwallet.android.features.asset_select.viewmodels.RecentsSheetViewModel
Expand All @@ -18,14 +18,14 @@ import kotlinx.collections.immutable.toImmutableList
@Composable
fun AssetSelectScreen(
title: String = "",
titleBadge: (AssetItemUIModel) -> String?,
titleBadge: (AssetInfoDataAggregate) -> String?,
showPopular: Boolean = false,
recentType: RecentType? = null,
onCancel: () -> Unit,
onSelect: ((AssetId) -> Unit)? = null,
onSelectRecent: ((AssetId) -> Unit)? = null,
itemTrailing: (@Composable (AssetItemUIModel) -> Unit)? = null,
itemSupport: ((AssetItemUIModel) -> (@Composable () -> Unit)?)? = null,
itemTrailing: (@Composable (AssetInfoDataAggregate) -> Unit)? = null,
itemSupport: ((AssetInfoDataAggregate) -> (@Composable () -> Unit)?)? = null,
onAddAsset: (() -> Unit)? = null,
viewModel: BaseAssetSelectViewModel,
recentsViewModel: RecentsSheetViewModel = hiltViewModel(),
Expand Down Expand Up @@ -64,7 +64,7 @@ fun AssetSelectScreen(
popular = if (showPopular) {
popular
} else {
emptyList<AssetItemUIModel>().toImmutableList()
emptyList<AssetInfoDataAggregate>().toImmutableList()
},
unpinned = unpinned,
recent = recent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.gemwallet.android.ext.networkName
import com.gemwallet.android.ext.type
import com.gemwallet.android.ui.R
import com.gemwallet.android.ui.components.list_item.AssetContextActions
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.ui.components.list_item.ListItemSupportText
import com.gemwallet.android.ui.icons.AppIcons
import com.wallet.core.primitives.Asset
Expand Down Expand Up @@ -65,7 +65,7 @@ fun AssetsManageScreen(
selectedTag = selectedTag,
tags = viewModel.getTags(),
pinned = pinned,
popular = emptyList<AssetItemUIModel>().toImmutableList(),
popular = emptyList<AssetInfoDataAggregate>().toImmutableList(),
unpinned = unpinned,
recent = emptyList<Asset>().toImmutableList(),
state = uiStates,
Expand Down Expand Up @@ -97,7 +97,7 @@ fun AssetsManageScreen(
},
itemTrailing = { asset ->
Switch(
checked = asset.metadata?.isBalanceEnabled == true,
checked = asset.balanceEnabled,
onCheckedChange = { viewModel.onChangeVisibility(asset.asset.id, it) },
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import com.gemwallet.android.ext.assetType
import com.gemwallet.android.ext.getAccount
import com.gemwallet.android.ext.runCatchingCancellable
import com.gemwallet.android.model.RecentType
import com.gemwallet.android.ui.components.list_item.AssetInfoUIModel
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.domains.asset.aggregates.AssetRowNaming
import com.gemwallet.android.domains.asset.aggregates.toAssetInfoDataAggregate
import com.gemwallet.android.ui.models.AssetToast
import com.gemwallet.android.ui.models.AssetToastEmitter
import com.gemwallet.android.ui.models.AssetToastEmitterImpl
Expand Down Expand Up @@ -107,34 +108,35 @@ open class BaseAssetSelectViewModel(
.filter { (chainFilter.isEmpty() || it.id().chain in chainFilter) && (!balanceFilter || it.balance.totalAmount > 0.0) }
.map { item ->
val owner = item.owner ?: wallet?.getAccount(item.asset.id.chain)
AssetInfoUIModel(if (item.owner == owner) item else item.copy(owner = owner))
val assetInfo = if (item.owner == owner) item else item.copy(owner = owner)
assetInfo.toAssetInfoDataAggregate(AssetRowNaming.CanonicalNative)
}
}
.flowOn(Dispatchers.IO)
.shareIn(viewModelScope, SharingStarted.Eagerly, replay = 1)

private val assets = assetsContent
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetItemUIModel>())
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetInfoDataAggregate>())

val popular = assets.map { items ->
items.filter {
it.asset.id in listOf(AssetId(Chain.Ethereum), AssetId(Chain.Bitcoin), AssetId(Chain.Solana))
}.toImmutableList()
}
.flowOn(Dispatchers.IO)
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetItemUIModel>().toImmutableList())
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetInfoDataAggregate>().toImmutableList())

val pinned = assets.map { items ->
items.filter { it.metadata?.isPinned == true && it.metadata?.isBalanceEnabled == true }.toImmutableList()
items.filter { it.pinned && it.balanceEnabled }.toImmutableList()
}
.flowOn(Dispatchers.IO)
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetItemUIModel>().toImmutableList())
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetInfoDataAggregate>().toImmutableList())

val unpinned = assets.map { items ->
items.filter { it.metadata?.isPinned != true || it.metadata?.isBalanceEnabled != true }.toImmutableList()
items.filter { !it.pinned || !it.balanceEnabled }.toImmutableList()
}
.flowOn(Dispatchers.IO)
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetItemUIModel>().toImmutableList())
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList<AssetInfoDataAggregate>().toImmutableList())

val recent = currentQuery
.flatMapLatest { query ->
Expand Down Expand Up @@ -175,7 +177,7 @@ open class BaseAssetSelectViewModel(
val session = session.value ?: return@launch
session.wallet.getAccount(assetId.chain) ?: return@launch
val item = assets.value.firstOrNull { it.asset.id == assetId }
val willPin = item?.metadata?.isPinned != true
val willPin = item?.pinned != true
toggleAssetPin(assetId)
item?.let { emitToast(AssetToast.Pin(it.asset.name, willPin)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.gemwallet.android.ui.R
import com.gemwallet.android.ui.components.empty.EmptyContentType
import com.gemwallet.android.ui.components.empty.EmptyContentView
import com.gemwallet.android.ui.components.list_item.AssetContextActions
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.ui.components.list_item.PinnedAssetsHeaderItem
import com.gemwallet.android.ui.components.list_item.SubheaderItem
import com.gemwallet.android.ui.components.list_item.assetPriceSupport
Expand Down Expand Up @@ -81,7 +81,7 @@ fun NetworkAssetsScreen(
}

private fun LazyListScope.networkAssetRows(
items: List<AssetItemUIModel>,
items: List<AssetInfoDataAggregate>,
onSelect: (AssetId) -> Unit,
longPressedAsset: MutableState<AssetId?>,
contextActions: AssetContextActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import com.gemwallet.android.ui.R
import com.gemwallet.android.ui.components.SearchBar
import com.gemwallet.android.ui.components.image.AsyncImage
import com.gemwallet.android.ui.components.list_item.AssetContextActions
import com.gemwallet.android.ui.components.list_item.AssetItemUIModel
import com.gemwallet.android.domains.asset.aggregates.AssetInfoDataAggregate
import com.gemwallet.android.ui.components.list_item.ListItem
import com.gemwallet.android.ui.components.list_item.ListItemDefaults
import com.gemwallet.android.ui.components.list_item.NftListItem
Expand Down Expand Up @@ -189,7 +189,7 @@ fun WalletSearchScreen(
selectedTag = selectedTag,
tags = viewModel.getTags(),
pinned = pinned,
popular = emptyList<AssetItemUIModel>().toImmutableList(),
popular = emptyList<AssetInfoDataAggregate>().toImmutableList(),
unpinned = previewAssets.toImmutableList(),
recent = recent,
state = state,
Expand Down
Loading
Loading