Updated on 2026-08-14
This commit is contained in:
parent
6d95abf3b8
commit
75e2268d10
15 changed files with 522 additions and 271 deletions
|
|
@ -1,45 +1,49 @@
|
|||
package com.tangem.core.ui.extensions
|
||||
|
||||
import androidx.annotation.DrawableRes
|
||||
import com.tangem.core.ui.R
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.luminance
|
||||
import androidx.core.graphics.toColorInt
|
||||
import com.tangem.core.ui.res.TangemColorPalette
|
||||
import com.tangem.domain.tokens.models.CryptoCurrency
|
||||
|
||||
private const val LIGHT_LUMINANCE = 0.5f
|
||||
private const val COLOR_HEX_START_INDEX = 2
|
||||
private const val COLOR_HEX_END_INDEX = 7
|
||||
|
||||
/**
|
||||
* Retrieves the resource ID for the network badge of a [CryptoCurrency].
|
||||
* Retrieves the resource ID for the network of a [CryptoCurrency].
|
||||
*
|
||||
* This property provides a way to fetch the appropriate drawable resource ID
|
||||
* for the network badge of a given cryptocurrency. For coins, this will typically
|
||||
* return null as they do not have network badges, while tokens will fetch the icon
|
||||
* based on their associated network ID.
|
||||
*
|
||||
* @return Drawable resource ID for the network badge or null if the cryptocurrency is a coin.
|
||||
* @return Drawable resource ID for the network.
|
||||
*/
|
||||
@get:DrawableRes
|
||||
val CryptoCurrency.networkBadgeIconResId: Int?
|
||||
get() = when (this) {
|
||||
is CryptoCurrency.Coin -> null
|
||||
is CryptoCurrency.Token -> getActiveIconRes(network.id.value)
|
||||
val CryptoCurrency.networkIconResId: Int
|
||||
get() = getActiveIconRes(network.id.value)
|
||||
|
||||
/**
|
||||
* Tries to extract a background color from the contract address of a token.
|
||||
*
|
||||
* @param fallbackColor The color to use as a fallback.
|
||||
* @return The extracted background color or the fallback color if extraction fails or if it is a test network token.
|
||||
*/
|
||||
fun CryptoCurrency.Token.tryGetBackgroundForTokenIcon(fallbackColor: Color = TangemColorPalette.Black): Color {
|
||||
if (network.isTestnet) return fallbackColor
|
||||
|
||||
return try {
|
||||
val colorHex = "#" + contractAddress.substring(range = COLOR_HEX_START_INDEX..COLOR_HEX_END_INDEX)
|
||||
Color(colorHex.toColorInt())
|
||||
} catch (exception: Exception) {
|
||||
fallbackColor
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the resource ID for the icon of a [CryptoCurrency].
|
||||
* Determines the tint color to be used for a token icon based on its background color.
|
||||
* If the icon's background color is light, a dark tint is chosen; otherwise, a light tint is chosen.
|
||||
*
|
||||
* This property provides a way to fetch the appropriate drawable resource ID
|
||||
* for the icon of a given cryptocurrency.
|
||||
*
|
||||
* @return Drawable resource ID for the cryptocurrency icon.
|
||||
* @param iconBackground The background color of the custom token icon.
|
||||
* @return The tint color to be used for the icon.
|
||||
*/
|
||||
@get:DrawableRes
|
||||
val CryptoCurrency.iconResId: Int
|
||||
get() = when (this) {
|
||||
is CryptoCurrency.Coin -> {
|
||||
val rawCoinId = id.rawCurrencyId
|
||||
|
||||
if (rawCoinId != null) {
|
||||
getActiveIconResByCoinId(rawCoinId, network.id.value)
|
||||
} else {
|
||||
R.drawable.ic_alert_24
|
||||
}
|
||||
}
|
||||
is CryptoCurrency.Token -> R.drawable.ic_alert_24
|
||||
}
|
||||
fun getTintForTokenIcon(iconBackground: Color): Color {
|
||||
return if (iconBackground.luminance() > LIGHT_LUMINANCE) TangemColorPalette.Black else TangemColorPalette.White
|
||||
}
|
||||
|
|
@ -88,6 +88,7 @@ private fun materialThemeColors(colors: TangemColors, isDark: Boolean): Colors {
|
|||
}
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun lightThemeColors(): TangemColors {
|
||||
return TangemColors(
|
||||
text = TangemColors.Text(
|
||||
|
|
@ -135,6 +136,7 @@ private fun lightThemeColors(): TangemColors {
|
|||
}
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun darkThemeColors(): TangemColors {
|
||||
return TangemColors(
|
||||
text = TangemColors.Text(
|
||||
|
|
|
|||
10
core/ui/src/main/res/drawable/ic_custom_token_44.xml
Normal file
10
core/ui/src/main/res/drawable/ic_custom_token_44.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="44dp"
|
||||
android:height="44dp"
|
||||
android:viewportWidth="44"
|
||||
android:viewportHeight="44">
|
||||
|
||||
<path
|
||||
android:fillColor="#656565"
|
||||
android:pathData="M22,34L22.69,29.327C23,27.23 23.155,26.182 23.648,25.352C24.065,24.651 24.651,24.065 25.352,23.648C26.182,23.155 27.23,23 29.327,22.69L34,22L29.327,21.31C27.23,21 26.182,20.845 25.352,20.352C24.651,19.935 24.065,19.349 23.648,18.648C23.155,17.818 23,16.77 22.69,14.673L22,10L21.31,14.673C21,16.77 20.845,17.818 20.352,18.648C19.935,19.349 19.349,19.935 18.648,20.352C17.818,20.845 16.77,21 14.673,21.31L10,22L14.673,22.69C16.77,23 17.818,23.155 18.648,23.648C19.349,24.065 19.935,24.651 20.352,25.352C20.845,26.182 21,27.23 21.31,29.327L22,34Z" />
|
||||
</vector>
|
||||
Loading…
Add table
Add a link
Reference in a new issue