Skip to content

Commit

Permalink
bugfix: track and goal visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
joaooab committed Jan 28, 2024
1 parent 462cd7e commit ad17a95
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ 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.ui.HorizontalLabeledText
import com.br.weightcontrol.util.todayAsString

@Composable
fun GoalCardSettings(
goal: Goal?,
currentTrack: Track?,
onEdit: () -> Unit,
onDelete: (Goal) -> Unit,
modifier: Modifier = Modifier
) {
GoalCard(
goal = goal,
currentTrack = null,
currentTrack = currentTrack,
navigateToGoal = onEdit,
onProgressGoal = {
GoalCardProgressSettings(
Expand Down Expand Up @@ -104,6 +106,7 @@ internal fun GoalCardFilledSettingsPreview() {
WeiTheme {
GoalCardSettings(
goal = Goal(start = 85.0, desire = 80.0, createdAt = todayAsString()),
currentTrack = Track(),
onEdit = {},
onDelete = {}
)
Expand Down
1 change: 1 addition & 0 deletions feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ android {
dependencies {
implementation(project(":feature:bmi"))
implementation(project(":feature:goal"))
implementation(project(":feature:track"))
implementation(libs.kotlinx.datetime)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.br.weightcontrol.model.Goal
import com.br.weightcontrol.model.Track
import com.br.weightcontrol.model.User
import com.br.weightcontrol.model.format
import com.br.weightcontrol.ui.CurrentTrackCard
import com.br.weightcontrol.track.component.TrackCard
import com.br.weightcontrol.ui.TrackListResourcePreviewParameterProvider
import com.br.weightcontrol.ui.chart.TrackListChart
import com.br.weightcontrol.util.todayAsString
Expand Down Expand Up @@ -79,7 +79,7 @@ internal fun HomeScreen(
Column(modifier.padding(16.dp)) {
LazyColumn {
item {
CurrentTrackCard(
TrackCard(
track = currentTrack,
onClickAdd = navigateToTrack,
modifier = Modifier.padding(top = 8.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HomeViewModel(
val currentTrack = trackRepository.getLastStream().filterNotNull().stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = Track()
initialValue = null
)

val user = userRepository.stream.stateIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import br.com.weightcontrol.component.GoalCardSettings
import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.model.Gender
import com.br.weightcontrol.model.Goal
import com.br.weightcontrol.model.Track
import com.br.weightcontrol.model.User
import com.br.weightcontrol.settings.R
import com.br.weightcontrol.ui.HorizontalLabeledText
Expand All @@ -41,6 +42,7 @@ internal fun SettingsRoute(
) {
val user by viewModel.user.collectAsStateWithLifecycle()
val goal by viewModel.goal.collectAsStateWithLifecycle()
val currentTrack by viewModel.currentTrack.collectAsStateWithLifecycle()
val deleteDialogState by viewModel.showDeleteDialog.collectAsStateWithLifecycle()

when (val state = deleteDialogState) {
Expand All @@ -57,6 +59,7 @@ internal fun SettingsRoute(
SettingsScreen(
user = user,
goal = goal,
currentTrack = currentTrack,
navigateToProfile = navigateToProfile,
navigateToGoal = navigateToGoal,
onDelete = viewModel::onDelete,
Expand All @@ -68,6 +71,7 @@ internal fun SettingsRoute(
private fun SettingsScreen(
user: User,
goal: Goal?,
currentTrack: Track?,
navigateToProfile: () -> Unit,
navigateToGoal: () -> Unit,
onDelete: (Goal) -> Unit,
Expand All @@ -86,6 +90,7 @@ private fun SettingsScreen(
Spacer(modifier = Modifier.padding(top = 16.dp))
GoalCardSettings(
goal = goal,
currentTrack = currentTrack,
onEdit = navigateToGoal,
onDelete = onDelete
)
Expand Down Expand Up @@ -155,6 +160,7 @@ fun SettingsScreenPreview() {
gender = Gender.MALE
),
goal = null,
currentTrack = Track(),
navigateToProfile = {},
navigateToGoal = {},
onDelete = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.br.weightcontrol.settings.navigation
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.br.weightcontrol.data.repository.GoalRepository
import com.br.weightcontrol.data.repository.TrackRepository
import com.br.weightcontrol.data.repository.UserRepository
import com.br.weightcontrol.model.Goal
import com.br.weightcontrol.model.User
Expand All @@ -17,6 +18,7 @@ import kotlinx.coroutines.launch

class SettingsViewModel(
userRepository: UserRepository,
trackRepository: TrackRepository,
private val goalRepository: GoalRepository
) : ViewModel() {

Expand All @@ -32,6 +34,11 @@ class SettingsViewModel(
initialValue = null,
)

val currentTrack = trackRepository.getLastStream().filterNotNull().stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = null
)

private val _showDeleteDialog = MutableStateFlow<DeleteDialogState<Goal>>(DeleteDialogState.Hide())
val showDeleteDialog: StateFlow<DeleteDialogState<Goal>> = _showDeleteDialog.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.br.weightcontrol.ui
package com.br.weightcontrol.track.component

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
Expand Down Expand Up @@ -32,16 +32,74 @@ import com.br.weightcontrol.designsystem.icon.WeiIcons
import com.br.weightcontrol.designsystem.theme.WeiTheme
import com.br.weightcontrol.model.Track
import com.br.weightcontrol.model.formatWeight
import com.br.weightcontrol.ui.text.TextWithIcon
import com.br.weightcontrol.util.format
import com.br.weightcontrol.util.today
import com.br.weightcontrol.track.R as trackR

@Composable
fun CurrentTrackCard(
fun TrackCard(
track: Track?,
onClickAdd: () -> Unit,
modifier: Modifier = Modifier
) {
track ?: return
when (track) {
null -> EmptyTrackCard(
onClickAdd = onClickAdd,
modifier = modifier
)

else -> CurrentTrackCard(
track = track,
onClickAdd = onClickAdd,
modifier = modifier
)
}
}

@Composable
private fun EmptyTrackCard(
onClickAdd: () -> Unit,
modifier: Modifier = Modifier
) {
Card(
modifier = modifier,
shape = RoundedCornerShape(16.dp)
) {
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.current_weight),
modifier = Modifier.padding(start = 8.dp),
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.weight(1f))
IconButton(onClick = onClickAdd) {
Icon(
imageVector = WeiIcons.Add,
contentDescription = null,
)
}
}
TextWithIcon(
text = trackR.string.track_empty,
icon = WeiIcons.Add,
)
}
}
}

@Composable
private fun CurrentTrackCard(
track: Track,
onClickAdd: () -> Unit,
modifier: Modifier = Modifier
) {
var progress by remember { mutableStateOf(0f) }
val progressAnimation: Float by animateFloatAsState(
targetValue = progress,
Expand Down Expand Up @@ -97,6 +155,14 @@ fun CurrentTrackCard(
}
}

@Preview
@Composable
fun EmptyTrackCardPreview() {
WeiTheme {
EmptyTrackCard(onClickAdd = { })
}
}

@Preview
@Composable
fun CurrentTrackCardPreview() {
Expand Down
1 change: 1 addition & 0 deletions feature/track/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<string name="track_which_day">Para qual dia?</string>
<string name="track_which_weight">Qual o peso?</string>
<string name="track_empty">"Pressione o botão # para registrar seu peso atual"</string>
</resources>

0 comments on commit ad17a95

Please sign in to comment.