|
| 1 | +package org.simple.clinic.summary.bloodsugar.ui |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.Spacer |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.foundation.layout.requiredHeight |
| 11 | +import androidx.compose.material.Divider |
| 12 | +import androidx.compose.material.Icon |
| 13 | +import androidx.compose.material.MaterialTheme |
| 14 | +import androidx.compose.material.Text |
| 15 | +import androidx.compose.runtime.Composable |
| 16 | +import androidx.compose.ui.Modifier |
| 17 | +import androidx.compose.ui.res.dimensionResource |
| 18 | +import androidx.compose.ui.res.painterResource |
| 19 | +import androidx.compose.ui.res.stringResource |
| 20 | +import androidx.compose.ui.text.style.TextAlign |
| 21 | +import androidx.compose.ui.tooling.preview.Preview |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import org.simple.clinic.R |
| 24 | +import org.simple.clinic.bloodsugar.BloodSugarMeasurementType |
| 25 | +import org.simple.clinic.bloodsugar.BloodSugarReading |
| 26 | +import org.simple.clinic.bloodsugar.BloodSugarUnitPreference |
| 27 | +import org.simple.clinic.bloodsugar.Fasting |
| 28 | +import org.simple.clinic.bloodsugar.HbA1c |
| 29 | +import org.simple.clinic.bloodsugar.PostPrandial |
| 30 | +import org.simple.clinic.common.ui.components.ButtonSize |
| 31 | +import org.simple.clinic.common.ui.components.TextButton |
| 32 | +import org.simple.clinic.common.ui.theme.SimpleTheme |
| 33 | +import java.util.UUID |
| 34 | + |
| 35 | +@Composable |
| 36 | +fun BloodSugarSummary( |
| 37 | + summaryItems: List<BloodSugarSummaryItem>, |
| 38 | + canShowSeeAllButton: Boolean, |
| 39 | + onSeeAllClick: () -> Unit, |
| 40 | + onAddBPClick: () -> Unit, |
| 41 | + onEditBPClick: (UUID, BloodSugarMeasurementType) -> Unit, |
| 42 | + modifier: Modifier = Modifier |
| 43 | +) { |
| 44 | + Column( |
| 45 | + modifier = Modifier |
| 46 | + .fillMaxWidth() |
| 47 | + .background(MaterialTheme.colors.surface) |
| 48 | + .then(modifier) |
| 49 | + ) { |
| 50 | + Row( |
| 51 | + modifier = Modifier.fillMaxWidth(), |
| 52 | + horizontalArrangement = Arrangement.SpaceBetween, |
| 53 | + ) { |
| 54 | + TextButton( |
| 55 | + leadingIcon = { |
| 56 | + Icon( |
| 57 | + painter = painterResource(id = R.drawable.ic_add_circle_blue1_24dp), |
| 58 | + contentDescription = null |
| 59 | + ) |
| 60 | + }, |
| 61 | + onClick = onAddBPClick, |
| 62 | + buttonSize = ButtonSize.Small, |
| 63 | + ) { |
| 64 | + val label = stringResource(id = R.string.bloodsugarsummaryview_add_blood_sugar_button).uppercase() |
| 65 | + Text(text = label) |
| 66 | + } |
| 67 | + |
| 68 | + if (canShowSeeAllButton) { |
| 69 | + TextButton( |
| 70 | + trailingIcon = { |
| 71 | + Icon( |
| 72 | + painter = painterResource(id = R.drawable.ic_chevron_right_blue1_24dp), |
| 73 | + contentDescription = null |
| 74 | + ) |
| 75 | + }, |
| 76 | + onClick = onSeeAllClick, |
| 77 | + buttonSize = ButtonSize.Small, |
| 78 | + ) { |
| 79 | + val label = stringResource(id = R.string.bloodpressuresummaryview_see_all_button).uppercase() |
| 80 | + Text(text = label) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Divider( |
| 86 | + modifier = Modifier |
| 87 | + .fillMaxWidth() |
| 88 | + .padding(horizontal = dimensionResource(id = R.dimen.spacing_8)), |
| 89 | + color = SimpleTheme.colors.onSurface11, |
| 90 | + ) |
| 91 | + |
| 92 | + if (summaryItems.isNotEmpty()) { |
| 93 | + Column( |
| 94 | + modifier = Modifier |
| 95 | + .fillMaxWidth() |
| 96 | + .padding(top = dimensionResource(id = R.dimen.spacing_4)) |
| 97 | + ) { |
| 98 | + summaryItems.forEach { summaryItem -> |
| 99 | + BloodSugarSummaryItem( |
| 100 | + item = summaryItem, |
| 101 | + onEdit = { |
| 102 | + onEditBPClick(summaryItem.id, summaryItem.reading.type) |
| 103 | + }, |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + } else { |
| 108 | + Text( |
| 109 | + text = stringResource(id = R.string.bloodsugarsummaryview_no_blood_sugars), |
| 110 | + modifier = Modifier |
| 111 | + .fillMaxWidth() |
| 112 | + .padding(vertical = dimensionResource(id = R.dimen.spacing_16)), |
| 113 | + textAlign = TextAlign.Center, |
| 114 | + style = MaterialTheme.typography.body1, |
| 115 | + color = SimpleTheme.colors.onSurface67 |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + when { |
| 120 | + summaryItems.size > 1 -> { |
| 121 | + Spacer(Modifier.requiredHeight(dimensionResource(R.dimen.spacing_8))) |
| 122 | + } |
| 123 | + |
| 124 | + summaryItems.isNotEmpty() -> { |
| 125 | + Spacer(Modifier.requiredHeight(dimensionResource(R.dimen.spacing_24))) |
| 126 | + } |
| 127 | + |
| 128 | + else -> { |
| 129 | + // no-op |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +@Preview |
| 136 | +@Composable |
| 137 | +private fun BloodSugarSummaryPreview() { |
| 138 | + SimpleTheme { |
| 139 | + BloodSugarSummary( |
| 140 | + summaryItems = listOf( |
| 141 | + BloodSugarSummaryItem( |
| 142 | + id = UUID.fromString("dbf8d3f5-87e2-4f90-ab53-1c250737af95"), |
| 143 | + reading = BloodSugarReading( |
| 144 | + value = "80", |
| 145 | + type = PostPrandial |
| 146 | + ), |
| 147 | + measurementUnit = BloodSugarUnitPreference.Mg, |
| 148 | + date = "12-Jun-2025", |
| 149 | + time = "08:10 am", |
| 150 | + canEdit = false, |
| 151 | + ), |
| 152 | + BloodSugarSummaryItem( |
| 153 | + id = UUID.fromString("dbf8d3f5-87e2-4f90-ab53-1c250737af95"), |
| 154 | + reading = BloodSugarReading( |
| 155 | + value = "75", |
| 156 | + type = PostPrandial |
| 157 | + ), |
| 158 | + measurementUnit = BloodSugarUnitPreference.Mg, |
| 159 | + date = "12-Jun-2025", |
| 160 | + time = "08:00 am", |
| 161 | + canEdit = false, |
| 162 | + ), |
| 163 | + BloodSugarSummaryItem( |
| 164 | + id = UUID.fromString("0057f476-dd45-47c0-9797-21427aa86766"), |
| 165 | + reading = BloodSugarReading( |
| 166 | + value = "7.5", |
| 167 | + type = HbA1c |
| 168 | + ), |
| 169 | + measurementUnit = BloodSugarUnitPreference.Mg, |
| 170 | + date = "12-Jun-2025", |
| 171 | + time = null, |
| 172 | + canEdit = true, |
| 173 | + ), |
| 174 | + BloodSugarSummaryItem( |
| 175 | + id = UUID.fromString("b309c90f-1391-4ab4-9991-09a4eae21631"), |
| 176 | + reading = BloodSugarReading( |
| 177 | + value = "250", |
| 178 | + type = Fasting |
| 179 | + ), |
| 180 | + measurementUnit = BloodSugarUnitPreference.Mg, |
| 181 | + date = "12-Jun-2025", |
| 182 | + time = null, |
| 183 | + canEdit = true, |
| 184 | + ), |
| 185 | + ), |
| 186 | + canShowSeeAllButton = true, |
| 187 | + onSeeAllClick = { |
| 188 | + // no-op |
| 189 | + }, |
| 190 | + onAddBPClick = { |
| 191 | + // no-op |
| 192 | + }, |
| 193 | + onEditBPClick = { _, _ -> |
| 194 | + // no-op |
| 195 | + } |
| 196 | + ) |
| 197 | + } |
| 198 | +} |
0 commit comments