Skip to content

Instantly share code, notes, and snippets.

@waseefakhtar
Last active September 2, 2023 22:16
Show Gist options
  • Save waseefakhtar/621fccd890c57eca0079b7cc3021a10c to your computer and use it in GitHub Desktop.
Save waseefakhtar/621fccd890c57eca0079b7cc3021a10c to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddMedicationScreen() {
// Previously implemented code.
var isMorningSelected by rememberSaveable { mutableStateOf(false) }
var isAfternoonSelected by rememberSaveable { mutableStateOf(false) }
var isEveningSelected by rememberSaveable { mutableStateOf(false) }
var isNightSelected by rememberSaveable { mutableStateOf(false) }
Column(
modifier = Modifier
.padding(16.dp, 16.dp)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// Previously implemented code.
Spacer(modifier = Modifier.padding(4.dp))
Text(
text = stringResource(id = R.string.times_of_day),
style = MaterialTheme.typography.bodyLarge
)
var selectionCount by rememberSaveable { mutableStateOf(0) }
val context = LocalContext.current
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
FilterChip(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
selected = isMorningSelected,
onClick = {
handleSelection(
isSelected = isMorningSelected,
selectionCount = selectionCount,
canSelectMoreTimesOfDay = canSelectMoreTimesOfDay(
selectionCount,
numberOfDosage.toIntOrNull() ?: 0
),
onStateChange = { count, selected ->
isMorningSelected = selected
selectionCount = count
},
onShowMaxSelectionError = {
showMaxSelectionToast(numberOfDosage, context)
}
)
},
label = { Text(text = TimesOfDay.Morning.name) },
leadingIcon = {
Icon(
imageVector = Icons.Default.Done,
contentDescription = "Selected"
)
}
)
FilterChip(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
selected = isAfternoonSelected,
onClick = {
handleSelection(
isSelected = isAfternoonSelected,
selectionCount = selectionCount,
canSelectMoreTimesOfDay = canSelectMoreTimesOfDay(
selectionCount,
numberOfDosage.toIntOrNull() ?: 0
),
onStateChange = { count, selected ->
isAfternoonSelected = selected
selectionCount = count
},
onShowMaxSelectionError = {
showMaxSelectionToast(numberOfDosage, context)
}
)
},
label = { Text(text = TimesOfDay.Afternoon.name) },
leadingIcon = {
Icon(
imageVector = Icons.Default.Done,
contentDescription = "Selected"
)
}
)
}
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
FilterChip(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
selected = isEveningSelected,
onClick = {
handleSelection(
isSelected = isEveningSelected,
selectionCount = selectionCount,
canSelectMoreTimesOfDay = canSelectMoreTimesOfDay(
selectionCount,
numberOfDosage.toIntOrNull() ?: 0
),
onStateChange = { count, selected ->
isEveningSelected = selected
selectionCount = count
},
onShowMaxSelectionError = {
showMaxSelectionToast(numberOfDosage, context)
}
)
},
label = { Text(text = TimesOfDay.Evening.name) },
leadingIcon = {
Icon(
imageVector = Icons.Default.Done,
contentDescription = "Selected"
)
}
)
FilterChip(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
selected = isNightSelected,
onClick = {
handleSelection(
isSelected = isNightSelected,
selectionCount = selectionCount,
canSelectMoreTimesOfDay = canSelectMoreTimesOfDay(
selectionCount,
numberOfDosage.toIntOrNull() ?: 0
),
onStateChange = { count, selected ->
isNightSelected = selected
selectionCount = count
},
onShowMaxSelectionError = {
showMaxSelectionToast(numberOfDosage, context)
}
)
},
label = { Text(text = TimesOfDay.Night.name) },
leadingIcon = {
Icon(
imageVector = Icons.Default.Done,
contentDescription = "Selected"
)
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment