|
1 | 1 | package com.cherrish.android.presentation.home |
2 | 2 |
|
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Box |
3 | 6 | import androidx.compose.foundation.layout.PaddingValues |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.height |
4 | 10 | import androidx.compose.foundation.layout.padding |
5 | | -import androidx.compose.material3.Text |
| 11 | +import androidx.compose.foundation.lazy.LazyColumn |
6 | 12 | import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.getValue |
7 | 14 | import androidx.compose.ui.Modifier |
| 15 | +import androidx.compose.ui.graphics.Brush |
| 16 | +import androidx.compose.ui.tooling.preview.Preview |
| 17 | +import androidx.compose.ui.unit.dp |
| 18 | +import androidx.hilt.navigation.compose.hiltViewModel |
| 19 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 20 | +import com.cherrish.android.core.common.state.UiState |
| 21 | +import com.cherrish.android.core.designsystem.theme.CherrishTheme |
| 22 | +import com.cherrish.android.core.designsystem.theme.graEnd |
| 23 | +import com.cherrish.android.core.designsystem.theme.graStart |
| 24 | +import com.cherrish.android.presentation.home.component.ChallengeSection |
| 25 | +import com.cherrish.android.presentation.home.component.PlanBoxSection |
| 26 | +import com.cherrish.android.presentation.home.component.UpcomingPlanSection |
| 27 | +import java.time.LocalDate |
| 28 | +import kotlinx.collections.immutable.persistentListOf |
8 | 29 |
|
9 | 30 | @Composable |
10 | 31 | fun HomeRoute( |
11 | | - paddingValues: PaddingValues |
| 32 | + paddingValues: PaddingValues, |
| 33 | + viewModel: HomeViewModel = hiltViewModel() |
12 | 34 | ) { |
13 | | - HomeScreen(paddingValues = paddingValues) |
| 35 | + val uiState by viewModel.uiState.collectAsStateWithLifecycle() |
| 36 | + |
| 37 | + when (val state = uiState) { |
| 38 | + is UiState.Loading -> { |
| 39 | + } |
| 40 | + |
| 41 | + is UiState.Failure -> { |
| 42 | + } |
| 43 | + |
| 44 | + is UiState.Success -> { |
| 45 | + HomeScreen( |
| 46 | + uiState = state.data, |
| 47 | + paddingValues = paddingValues, |
| 48 | + onUpcomingPlanClick = viewModel::onUpcomingPlanClick, |
| 49 | + onChallengeStartClick = viewModel::onAddChallengeClick, |
| 50 | + onAddPlanClick = viewModel::onAddPlanClick |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + else -> {} |
| 55 | + } |
14 | 56 | } |
15 | 57 |
|
16 | 58 | @Composable |
17 | 59 | private fun HomeScreen( |
| 60 | + uiState: HomeUiState, |
18 | 61 | paddingValues: PaddingValues, |
| 62 | + onUpcomingPlanClick: (LocalDate) -> Unit, |
| 63 | + onChallengeStartClick: () -> Unit, |
| 64 | + onAddPlanClick: () -> Unit, |
19 | 65 | modifier: Modifier = Modifier |
20 | 66 | ) { |
21 | | - Text( |
22 | | - text = "Home", |
23 | | - modifier = modifier.padding(paddingValues) |
24 | | - ) |
| 67 | + Box( |
| 68 | + modifier = modifier |
| 69 | + .fillMaxSize() |
| 70 | + .background(color = CherrishTheme.colors.graEnd) |
| 71 | + ) { |
| 72 | + Box( |
| 73 | + modifier = Modifier |
| 74 | + .fillMaxWidth() |
| 75 | + .height(270.dp) |
| 76 | + .background( |
| 77 | + brush = Brush.verticalGradient( |
| 78 | + colors = persistentListOf( |
| 79 | + graStart, |
| 80 | + graEnd |
| 81 | + ) |
| 82 | + ) |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + LazyColumn( |
| 87 | + modifier = Modifier |
| 88 | + .fillMaxSize() |
| 89 | + .padding(paddingValues), |
| 90 | + contentPadding = PaddingValues( |
| 91 | + start = 17.dp, |
| 92 | + end = 17.dp, |
| 93 | + top = 30.dp, |
| 94 | + bottom = 20.dp |
| 95 | + ), |
| 96 | + verticalArrangement = Arrangement.spacedBy(12.dp) |
| 97 | + ) { |
| 98 | + item { |
| 99 | + ChallengeSection( |
| 100 | + imageRes = uiState.gauges[uiState.selectedIndex].image, |
| 101 | + currentStep = uiState.currentStep, |
| 102 | + gauges = uiState.gauges, |
| 103 | + onChallengeStartClick = onChallengeStartClick, |
| 104 | + challengeName = uiState.challengeName |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + item { |
| 109 | + PlanBoxSection( |
| 110 | + todayDate = uiState.todayDate, |
| 111 | + plans = uiState.plans |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + item { |
| 116 | + UpcomingPlanSection( |
| 117 | + onAddPlanClick = onAddPlanClick, |
| 118 | + plans = uiState.upcomingPlans, |
| 119 | + onUpcomingPlanClick = onUpcomingPlanClick |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +@Preview(showBackground = true) |
| 127 | +@Composable |
| 128 | +private fun Preview() { |
| 129 | + CherrishTheme { |
| 130 | + HomeScreen( |
| 131 | + uiState = HomeUiState.fake, |
| 132 | + paddingValues = PaddingValues(0.dp), |
| 133 | + onUpcomingPlanClick = {}, |
| 134 | + onAddPlanClick = {}, |
| 135 | + onChallengeStartClick = {} |
| 136 | + ) |
| 137 | + } |
25 | 138 | } |
0 commit comments