Skip to content

Commit

Permalink
feature: add completed goal card
Browse files Browse the repository at this point in the history
  • Loading branch information
joaooab committed Dec 26, 2023
1 parent eca0c34 commit 008a776
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class Goal(
val createdAt: String,
val completedAt: String? = null,
) {
fun isFinished() = completedAt != null
fun remaining(weight: Double) = if (start < desire) desire - weight else weight - desire
}

Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package br.com.weightcontrol.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
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.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.designsystem.theme.WeiTheme
import com.br.weightcontrol.goal.R
import com.br.weightcontrol.model.Goal
import com.br.weightcontrol.model.Track
import com.br.weightcontrol.util.today
Expand All @@ -28,27 +17,19 @@ fun GoalCard(
goal: Goal?,
currentTrack: Track?,
navigateToGoal: () -> Unit,
modifier: Modifier = Modifier
modifier: Modifier = Modifier,
onEmptyGoal: @Composable () -> Unit = { GoalEmptyCard(navigateToGoal) },
onCompletedGoal: @Composable () -> Unit = { GoalCompletionCard() },
onProgressGoal: @Composable () -> Unit = { GoalProgressCard(goal, currentTrack) }
) {
Card(
shape = RoundedCornerShape(16.dp),
modifier = modifier
) {
Column(modifier = Modifier.padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = WeiIcons.Flag,
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = stringResource(R.string.goal),
modifier = Modifier.padding(start = 8.dp),
fontWeight = FontWeight.Bold
)
}
if (goal == null) GoalEmptyCard(navigateToGoal)
else GoalFilledCard(goal, currentTrack)
when {
goal == null -> onEmptyGoal()
goal.isFinished() -> onCompletedGoal()
else -> onProgressGoal()
}
}
}
Expand All @@ -66,6 +47,22 @@ fun GoalEmptyCardPreview() {
}
}

@Preview
@Composable
fun GoalCompletedCardPreview() {
WeiTheme {
GoalCard(
goal = Goal(
start = 85.0,
desire = 80.0,
createdAt = todayAsString(),
completedAt = todayAsString()
),
currentTrack = Track(weight = 84.0, createdAt = today()),
navigateToGoal = {}
)
}
}

@Preview
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package br.com.weightcontrol.component
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
Expand All @@ -32,34 +29,30 @@ fun GoalCardSettings(
onDelete: (Goal) -> Unit,
modifier: Modifier = Modifier
) {
Card(
shape = RoundedCornerShape(16.dp),
modifier = modifier
) {
Column {
if (goal == null) GoalCard(
goal = null,
currentTrack = null,
navigateToGoal = onEdit
)
else GoalCardFilledSettings(
GoalCard(
goal = goal,
currentTrack = null,
navigateToGoal = onEdit,
onProgressGoal = {
GoalCardProgressSettings(
goal = goal,
onEdit = onEdit,
onDelete = onDelete,
modifier = modifier
)
}
}
)
}

@Composable
internal fun GoalCardFilledSettings(
goal: Goal,
internal fun GoalCardProgressSettings(
goal: Goal?,
onEdit: () -> Unit,
onDelete: (Goal) -> Unit,
modifier: Modifier = Modifier
) {
Column {
goal ?: return
Column(modifier = Modifier.padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = WeiIcons.Flag,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package br.com.weightcontrol.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.br.weightcontrol.designsystem.theme.WeiTheme

@Composable
fun GoalCompletionCard(modifier: Modifier = Modifier) {
Card(
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primary),
modifier = modifier
.fillMaxWidth()
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
) {
Text(
text = "Goal Completion",
style = MaterialTheme.typography.titleMedium.copy(color = Color.White),
)
}
}
}

@Preview
@Composable
fun GoalCompletionCardPreview() {
WeiTheme {
GoalCompletionCard()
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
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.font.FontWeight
import androidx.compose.ui.unit.dp
import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.goal.R

@Composable
internal fun GoalEmptyCard(navigateToGoal: () -> Unit) {
Text(
text = stringResource(R.string.goal_empty),
modifier = Modifier.padding(top = 16.dp),
)
Box(
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
) {
Button(
onClick = { navigateToGoal() },
modifier = Modifier.align(Alignment.CenterEnd)
Column(modifier = Modifier.padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = WeiIcons.Flag,
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = stringResource(R.string.goal),
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)
) {
Text(text = stringResource(id = R.string.add))
Button(
onClick = { navigateToGoal() },
modifier = Modifier.align(Alignment.CenterEnd)
) {
Text(text = stringResource(id = R.string.add))
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package br.com.weightcontrol.component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
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.font.FontWeight
import androidx.compose.ui.unit.dp
import br.com.weightcontrol.domain.calculateGoalPercentage
import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.goal.R
import com.br.weightcontrol.model.Goal
import com.br.weightcontrol.model.Track
import com.br.weightcontrol.ui.CustomLinearProgressIndicator
import com.br.weightcontrol.ui.VerticalLabeledText

@Composable
internal fun GoalProgressCard(goal: Goal?, currentTrack: Track?) {
goal ?: return
currentTrack ?: return
Column(modifier = Modifier.padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = WeiIcons.Flag,
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = stringResource(R.string.goal),
modifier = Modifier.padding(start = 8.dp),
fontWeight = FontWeight.Bold
)
}
Row(
modifier = Modifier
.padding(top = 16.dp)
.padding(horizontal = 16.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
) {
VerticalLabeledText(
label = R.string.goal_begin,
weight = goal.start
)
VerticalLabeledText(
label = R.string.goal_remaining,
weight = goal.remaining(currentTrack.weight)
)
VerticalLabeledText(
label = R.string.goal_destination,
weight = goal.desire
)
}
CustomLinearProgressIndicator(
targetProgress = calculateGoalPercentage(goal, currentTrack),
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 56.dp)
.padding(top = 16.dp)
)
}
}

0 comments on commit 008a776

Please sign in to comment.