From f1d8b32dc6530253b6754937c0c902227244c39c Mon Sep 17 00:00:00 2001 From: Iliyan Germanov Date: Sun, 1 May 2022 19:16:01 +0300 Subject: [PATCH] Implement support for the new item icons --- .../wallet/ui/theme/components/ItemIcon.kt | 112 ++++++++++++++++-- 1 file changed, 99 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/ivy/wallet/ui/theme/components/ItemIcon.kt b/app/src/main/java/com/ivy/wallet/ui/theme/components/ItemIcon.kt index 4674998ffd..3acc499206 100644 --- a/app/src/main/java/com/ivy/wallet/ui/theme/components/ItemIcon.kt +++ b/app/src/main/java/com/ivy/wallet/ui/theme/components/ItemIcon.kt @@ -3,14 +3,20 @@ package com.ivy.wallet.ui.theme.components import android.content.Context import androidx.annotation.DrawableRes import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import com.ivy.design.l0_system.UI +import com.ivy.design.utils.thenWhen import com.ivy.wallet.ui.IvyWalletComponentPreview import com.ivy.wallet.utils.toLowerCaseLocal @@ -23,7 +29,8 @@ fun ItemIconL( Default: (@Composable () -> Unit)? = null ) { ItemIcon( - modifier = modifier, + modifier = modifier + .size(64.dp), size = "l", iconName = iconName, tint = tint, @@ -61,7 +68,8 @@ fun ItemIconM( Default: (@Composable () -> Unit)? = null ) { ItemIcon( - modifier = modifier, + modifier = modifier + .size(48.dp), size = "m", iconName = iconName, tint = tint, @@ -99,7 +107,8 @@ fun ItemIconS( Default: (@Composable () -> Unit)? = null ) { ItemIcon( - modifier = modifier, + modifier = modifier + .size(32.dp), size = "s", iconName = iconName, tint = tint, @@ -116,18 +125,39 @@ private fun ItemIcon( Default: (@Composable () -> Unit)? = null ) { val context = LocalContext.current - val iconId = getCustomIconId( + val iconInfo = getCustomIconId( context = context, iconName = iconName, size = size ) - if (iconId != null) { + if (iconInfo != null) { Image( - modifier = modifier, - painter = painterResource(id = iconId), + modifier = modifier + .thenWhen { + if (!iconInfo.newFormat) { + //do nothing for the old format of icons + return@thenWhen this + } + + when (iconInfo.style) { + IconStyle.L -> + //64.dp - 48.dp = 16.dp / 4 = 4.dp + this.padding(all = 4.dp) + IconStyle.M -> + //48.dp - 32.dp = 16.dp / 4 = 4.dp + this.padding(all = 4.dp) + IconStyle.S -> + //32.dp - 24.dp = 8.dp / 4 = 2.dp + this.padding(all = 2.dp) + IconStyle.UNKNOWN -> this + } + }, + painter = painterResource(id = iconInfo.iconId), colorFilter = ColorFilter.tint(tint), - contentDescription = "item icon" + alignment = Alignment.Center, + contentScale = ContentScale.Fit, + contentDescription = iconName ?: "item icon" ) } else { Default?.invoke() @@ -145,7 +175,7 @@ fun getCustomIconIdS( context = context, iconName = iconName, size = "s" - ) ?: defaultIcon + )?.iconId ?: defaultIcon } @DrawableRes @@ -159,7 +189,7 @@ fun getCustomIconIdM( context = context, iconName = iconName, size = "m" - ) ?: defaultIcon + )?.iconId ?: defaultIcon } @DrawableRes @@ -173,14 +203,70 @@ fun getCustomIconIdL( context = context, iconName = iconName, size = "l" - ) ?: defaultIcon + )?.iconId ?: defaultIcon } -@DrawableRes fun getCustomIconId( context: Context, iconName: String?, size: String, +): IconInfo? { + val iconStyle = when (size) { + "l" -> IconStyle.L + "m" -> IconStyle.M + "s" -> IconStyle.S + else -> IconStyle.UNKNOWN + } + + return iconName?.let { + try { + val iconNameNormalized = iconName + .replace(" ", "") + .trim() + .toLowerCaseLocal() + + val itemId = context.resources.getIdentifier( + "ic_custom_${iconNameNormalized}_${size}", + "drawable", + context.packageName + ).takeIf { it != 0 } + + itemId?.let { nonNullId -> + IconInfo( + iconId = nonNullId, + style = iconStyle, + newFormat = false + ) + } + } catch (e: Exception) { + fallbackToNewIconFormat( + context = context, iconName = iconName + )?.let { nonNullId -> + IconInfo( + iconId = nonNullId, + style = iconStyle, + newFormat = true + ) + } + } + } +} + +data class IconInfo( + @DrawableRes + val iconId: Int, + val style: IconStyle, + val newFormat: Boolean +) + +enum class IconStyle { + L, M, S, UNKNOWN +} + +@DrawableRes +fun fallbackToNewIconFormat( + context: Context, + iconName: String?, ): Int? { return iconName?.let { try { @@ -189,7 +275,7 @@ fun getCustomIconId( .trim() .toLowerCaseLocal() context.resources.getIdentifier( - "ic_custom_${iconNameNormalized}_${size}", + iconNameNormalized, "drawable", context.packageName ).takeIf { it != 0 }