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

Commit

Permalink
Merge pull request #825 from ILIYANGERMANOV/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ILIYANGERMANOV authored May 1, 2022
2 parents cba5055 + 3c526ab commit 85cdc7a
Show file tree
Hide file tree
Showing 8 changed files with 705 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.transaction.isOverdue
import com.ivy.wallet.utils.beginningOfIvyTime
import java.time.LocalDateTime
import javax.inject.Inject

class OverdueAct @Inject constructor(
Expand All @@ -14,7 +16,10 @@ class OverdueAct @Inject constructor(

override suspend fun Input.compose(): suspend () -> Output = suspend {
DueTrnsInfoAct.Input(
range = range,
range = ClosedTimeRange(
from = beginningOfIvyTime(),
to = toRange
),
baseCurrency = baseCurrency,
dueFilter = ::isOverdue
)
Expand All @@ -26,7 +31,7 @@ class OverdueAct @Inject constructor(
}

data class Input(
val range: ClosedTimeRange,
val toRange: LocalDateTime,
val baseCurrency: String
)

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/ivy/wallet/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class HomeViewModel @Inject constructor(
updateState {
val result = overdueAct(
OverdueAct.Input(
range = timeRange,
toRange = timeRange.to,
baseCurrency = baseCurrency
)
)
Expand Down
124 changes: 110 additions & 14 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,40 @@ 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 = if (iconInfo.newFormat)
ContentScale.Fit else ContentScale.None,
contentDescription = iconName ?: "item icon"
)
} else {
Default?.invoke()
Expand All @@ -145,7 +176,7 @@ fun getCustomIconIdS(
context = context,
iconName = iconName,
size = "s"
) ?: defaultIcon
)?.iconId ?: defaultIcon
}

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

@DrawableRes
Expand All @@ -173,26 +204,91 @@ fun getCustomIconIdL(
context = context,
iconName = iconName,
size = "l"
) ?: defaultIcon
)?.iconId ?: defaultIcon
}

@DrawableRes
fun getCustomIconId(
context: Context,
iconName: String?,
size: String,
): Int? {
): 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()
context.resources.getIdentifier(

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
)
} ?: fallbackToNewIconFormat(
context = context,
iconName = iconName,
iconStyle = iconStyle
)
} catch (e: Exception) {
fallbackToNewIconFormat(
context = context,
iconName = iconName,
iconStyle = iconStyle
)
}
}
}

data class IconInfo(
@DrawableRes
val iconId: Int,
val style: IconStyle,
val newFormat: Boolean
)

enum class IconStyle {
L, M, S, UNKNOWN
}

fun fallbackToNewIconFormat(
iconStyle: IconStyle,
context: Context,
iconName: String?,
): IconInfo? {
return iconName?.let {
try {
val iconNameNormalized = iconName
.replace(" ", "")
.trim()
.toLowerCaseLocal()

val iconId = context.resources.getIdentifier(
iconNameNormalized,
"drawable",
context.packageName
).takeIf { it != 0 }

iconId?.let { nonNullId ->
IconInfo(
iconId = nonNullId,
style = iconStyle,
newFormat = true
)
}
} catch (e: Exception) {
null
}
Expand Down
Loading

0 comments on commit 85cdc7a

Please sign in to comment.