Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Implement support for the new item icons
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed May 1, 2022
1 parent 806578f commit f1d8b32
Showing 1 changed file with 99 additions and 13 deletions.
112 changes: 99 additions & 13 deletions app/src/main/java/com/ivy/wallet/ui/theme/components/ItemIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -23,7 +29,8 @@ fun ItemIconL(
Default: (@Composable () -> Unit)? = null
) {
ItemIcon(
modifier = modifier,
modifier = modifier
.size(64.dp),
size = "l",
iconName = iconName,
tint = tint,
Expand Down Expand Up @@ -61,7 +68,8 @@ fun ItemIconM(
Default: (@Composable () -> Unit)? = null
) {
ItemIcon(
modifier = modifier,
modifier = modifier
.size(48.dp),
size = "m",
iconName = iconName,
tint = tint,
Expand Down Expand Up @@ -99,7 +107,8 @@ fun ItemIconS(
Default: (@Composable () -> Unit)? = null
) {
ItemIcon(
modifier = modifier,
modifier = modifier
.size(32.dp),
size = "s",
iconName = iconName,
tint = tint,
Expand All @@ -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()
Expand All @@ -145,7 +175,7 @@ fun getCustomIconIdS(
context = context,
iconName = iconName,
size = "s"
) ?: defaultIcon
)?.iconId ?: defaultIcon
}

@DrawableRes
Expand All @@ -159,7 +189,7 @@ fun getCustomIconIdM(
context = context,
iconName = iconName,
size = "m"
) ?: defaultIcon
)?.iconId ?: defaultIcon
}

@DrawableRes
Expand All @@ -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 {
Expand All @@ -189,7 +275,7 @@ fun getCustomIconId(
.trim()
.toLowerCaseLocal()
context.resources.getIdentifier(
"ic_custom_${iconNameNormalized}_${size}",
iconNameNormalized,
"drawable",
context.packageName
).takeIf { it != 0 }
Expand Down

0 comments on commit f1d8b32

Please sign in to comment.