Skip to content

Commit

Permalink
improvement: change goal card empty text
Browse files Browse the repository at this point in the history
  • Loading branch information
joaooab committed Jan 28, 2024
1 parent 3f07a78 commit 462cd7e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.br.weightcontrol.ui.text

import androidx.annotation.StringRes
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.sp

typealias EntryInlineTextContent = Map<String, InlineTextContent>

private const val ICON_ID = "inlineContent"
private const val ICON_PLACE_HOLDER = "[icon]"

@Composable
fun stringResourceWithIcon(
@StringRes text: Int,
icon: ImageVector,
delimiter: String
): Pair<AnnotatedString, EntryInlineTextContent> {
val stringResources = stringResource(id = text).split(delimiter)
val inlineContent = createInlineContent(icon)
val annotatedString = buildAnnotatedString {
stringResources.getOrNull(0)?.let(::append)
appendInlineContent(ICON_ID, ICON_PLACE_HOLDER)
stringResources.getOrNull(1)?.let(::append)
}

return Pair(annotatedString, inlineContent)
}

@Composable
private fun createInlineContent(icon: ImageVector) = mapOf(
Pair(
ICON_ID,
InlineTextContent(
Placeholder(
width = 20.sp,
height = 20.sp,
placeholderVerticalAlign = PlaceholderVerticalAlign.Center
)
) {
Icon(icon, null)
}
)
)
24 changes: 24 additions & 0 deletions core/ui/src/main/java/com/br/weightcontrol/ui/text/TextWithIcon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.br.weightcontrol.ui.text

import androidx.annotation.StringRes
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp

private const val DELIMITER = "#"

/**
* Provide a text with a delimiter for replacing to an icon
*/
@Composable
fun TextWithIcon(@StringRes text: Int, icon: ImageVector, delimiter: String = DELIMITER) {
val stringResourceWithIcon = stringResourceWithIcon(text, icon, delimiter)
Text(
text = stringResourceWithIcon.first,
inlineContent = stringResourceWithIcon.second,
modifier = Modifier.padding(top = 16.dp),
)
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package br.com.weightcontrol.component

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.br.weightcontrol.designsystem.component.WeiOutlinedButton
import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.goal.R
import com.br.weightcontrol.ui.text.TextWithIcon

@Composable
internal fun GoalEmptyCard(
onAdd: () -> Unit,
onClickAdd: () -> Unit,
modifier: Modifier = Modifier
) {
Card(
Expand All @@ -43,23 +41,18 @@ internal fun GoalEmptyCard(
modifier = Modifier.padding(start = 8.dp),
fontWeight = FontWeight.Bold
)
}
Text(
text = stringResource(R.string.goal_empty),
modifier = Modifier.padding(top = 16.dp),
)
Box(
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
) {
WeiOutlinedButton(
onClick = { onAdd() },
modifier = Modifier.align(Alignment.CenterEnd)
) {
Text(text = stringResource(id = R.string.add))
Spacer(modifier = Modifier.weight(1f))
IconButton(onClick = onClickAdd) {
Icon(
imageVector = WeiIcons.Add,
contentDescription = null,
)
}
}
TextWithIcon(
text = R.string.goal_empty,
icon = WeiIcons.Add,
)
}
}
}
2 changes: 1 addition & 1 deletion feature/goal/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="goal">Meta</string>
<string name="goal_empty">"Adicione uma meta para se manter motivado"</string>
<string name="goal_empty">"Pressione o botão # para adicionar uma meta e se manter motivado"</string>
<string name="goal_current">Meta atual: %sKg</string>
<string name="goal_current_track">Peso atual: %sKg</string>
<string name="goal_recommendation">Recomendação: %sKg - %sKg</string>
Expand Down

0 comments on commit 462cd7e

Please sign in to comment.