|
| 1 | +/* |
| 2 | + * Copyright 2022 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compose.jetsurvey.survey.question |
| 18 | + |
| 19 | +import android.content.res.Configuration |
| 20 | +import androidx.compose.foundation.BorderStroke |
| 21 | +import androidx.compose.foundation.Image |
| 22 | +import androidx.compose.foundation.clickable |
| 23 | +import androidx.compose.foundation.layout.Box |
| 24 | +import androidx.compose.foundation.layout.Column |
| 25 | +import androidx.compose.foundation.layout.Row |
| 26 | +import androidx.compose.foundation.layout.Spacer |
| 27 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 28 | +import androidx.compose.foundation.layout.padding |
| 29 | +import androidx.compose.foundation.layout.size |
| 30 | +import androidx.compose.foundation.layout.width |
| 31 | +import androidx.compose.foundation.selection.selectable |
| 32 | +import androidx.compose.foundation.selection.selectableGroup |
| 33 | +import androidx.compose.material3.Checkbox |
| 34 | +import androidx.compose.material3.MaterialTheme |
| 35 | +import androidx.compose.material3.RadioButton |
| 36 | +import androidx.compose.material3.Surface |
| 37 | +import androidx.compose.material3.Text |
| 38 | +import androidx.compose.runtime.Composable |
| 39 | +import androidx.compose.runtime.getValue |
| 40 | +import androidx.compose.runtime.mutableStateOf |
| 41 | +import androidx.compose.runtime.remember |
| 42 | +import androidx.compose.runtime.setValue |
| 43 | +import androidx.compose.ui.Alignment |
| 44 | +import androidx.compose.ui.Modifier |
| 45 | +import androidx.compose.ui.draw.clip |
| 46 | +import androidx.compose.ui.graphics.painter.Painter |
| 47 | +import androidx.compose.ui.res.painterResource |
| 48 | +import androidx.compose.ui.res.stringResource |
| 49 | +import androidx.compose.ui.tooling.preview.Preview |
| 50 | +import androidx.compose.ui.tooling.preview.PreviewParameter |
| 51 | +import androidx.compose.ui.tooling.preview.PreviewParameterProvider |
| 52 | +import androidx.compose.ui.unit.dp |
| 53 | +import com.example.compose.jetsurvey.R |
| 54 | +import com.example.compose.jetsurvey.survey.Answer |
| 55 | +import com.example.compose.jetsurvey.survey.AnswerOption |
| 56 | +import com.example.compose.jetsurvey.survey.PossibleAnswer |
| 57 | +import com.example.compose.jetsurvey.theme.JetsurveyTheme |
| 58 | + |
| 59 | +/** |
| 60 | + * Shows a list of possible answers with image and text and a radio button. |
| 61 | + * @param options a list of all possible answers with their icon and text value |
| 62 | + * @param answer the chosen answer (identified by text resource value), can be null if no answer is |
| 63 | + * chosen |
| 64 | + * @param onAnswerSelected callback that is called when a possible answer is clicked |
| 65 | + * @param modifier Modifier to be applied to the [SingleChoiceQuestion] |
| 66 | + */ |
| 67 | +@Composable |
| 68 | +fun SingleChoiceQuestion( |
| 69 | + options: List<AnswerOption>, |
| 70 | + answer: Answer.SingleChoice?, |
| 71 | + onAnswerSelected: (Int) -> Unit, |
| 72 | + modifier: Modifier = Modifier |
| 73 | +) { |
| 74 | + Column(modifier.selectableGroup()) { |
| 75 | + options.forEach { option -> |
| 76 | + Answer( |
| 77 | + text = stringResource(option.textRes), |
| 78 | + painter = option.iconRes?.let { painterResource(it) }, |
| 79 | + selected = option.textRes == answer?.answer, |
| 80 | + onOptionSelected = { onAnswerSelected(option.textRes) }, |
| 81 | + isSingleChoice = true, |
| 82 | + modifier = Modifier.padding(vertical = 8.dp) |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +@Composable |
| 89 | +fun MultipleChoiceQuestion( |
| 90 | + possibleAnswer: PossibleAnswer.MultipleChoice, |
| 91 | + answer: Answer.MultipleChoice?, |
| 92 | + onAnswerSelected: (Int, Boolean) -> Unit, |
| 93 | + modifier: Modifier = Modifier |
| 94 | +) { |
| 95 | + Column(modifier) { |
| 96 | + possibleAnswer.options.forEach { option -> |
| 97 | + val selected = answer?.answersStringRes?.contains(option.textRes) ?: false |
| 98 | + Answer( |
| 99 | + text = stringResource(option.textRes), |
| 100 | + painter = option.iconRes?.let { painterResource(it) }, |
| 101 | + selected = selected, |
| 102 | + onOptionSelected = { onAnswerSelected(option.textRes, !selected) }, |
| 103 | + isSingleChoice = false, |
| 104 | + modifier = Modifier.padding(vertical = 8.dp) |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@Composable |
| 111 | +private fun Answer( |
| 112 | + text: String, |
| 113 | + painter: Painter?, |
| 114 | + selected: Boolean, |
| 115 | + onOptionSelected: () -> Unit, |
| 116 | + isSingleChoice: Boolean, |
| 117 | + modifier: Modifier = Modifier |
| 118 | +) { |
| 119 | + Surface( |
| 120 | + shape = MaterialTheme.shapes.small, |
| 121 | + color = if (selected) { |
| 122 | + MaterialTheme.colorScheme.primaryContainer |
| 123 | + } else { |
| 124 | + MaterialTheme.colorScheme.surface |
| 125 | + }, |
| 126 | + border = BorderStroke( |
| 127 | + width = 1.dp, |
| 128 | + color = if (selected) { |
| 129 | + MaterialTheme.colorScheme.primary |
| 130 | + } else { |
| 131 | + MaterialTheme.colorScheme.outline |
| 132 | + } |
| 133 | + ), |
| 134 | + modifier = modifier |
| 135 | + ) { |
| 136 | + Row( |
| 137 | + modifier = Modifier |
| 138 | + .fillMaxWidth() |
| 139 | + .then( |
| 140 | + if (isSingleChoice) { |
| 141 | + Modifier.selectable(selected, onClick = onOptionSelected) |
| 142 | + } else { |
| 143 | + Modifier.clickable(onClick = onOptionSelected) |
| 144 | + } |
| 145 | + ) |
| 146 | + .padding(16.dp), |
| 147 | + verticalAlignment = Alignment.CenterVertically |
| 148 | + ) { |
| 149 | + if (painter != null) { |
| 150 | + Image( |
| 151 | + painter = painter, |
| 152 | + contentDescription = null, |
| 153 | + modifier = Modifier |
| 154 | + .size(56.dp) |
| 155 | + .clip(MaterialTheme.shapes.extraSmall) |
| 156 | + ) |
| 157 | + Spacer(Modifier.width(8.dp)) |
| 158 | + } |
| 159 | + Text(text, Modifier.weight(1f), style = MaterialTheme.typography.bodyLarge) |
| 160 | + Box(Modifier.padding(8.dp)) { |
| 161 | + if (isSingleChoice) { |
| 162 | + RadioButton(selected, onClick = null) |
| 163 | + } else { |
| 164 | + Checkbox(selected, onCheckedChange = null) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +@Preview(name = "Light", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 172 | +@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 173 | +@Composable |
| 174 | +private fun AnswerPreview( |
| 175 | + @PreviewParameter(PreviewDataProvider::class, limit = 4) previewData: PreviewData |
| 176 | +) { |
| 177 | + JetsurveyTheme { |
| 178 | + Answer( |
| 179 | + text = "Preview", |
| 180 | + painter = painterResource(id = R.drawable.frag), |
| 181 | + selected = previewData.selected, |
| 182 | + isSingleChoice = previewData.isSingleChoice, |
| 183 | + onOptionSelected = { } |
| 184 | + ) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +private data class PreviewData( |
| 189 | + val selected: Boolean, |
| 190 | + val isSingleChoice: Boolean |
| 191 | +) |
| 192 | + |
| 193 | +private class PreviewDataProvider : PreviewParameterProvider<PreviewData> { |
| 194 | + override val values = sequenceOf( |
| 195 | + PreviewData(selected = false, isSingleChoice = true), |
| 196 | + PreviewData(selected = true, isSingleChoice = true), |
| 197 | + PreviewData(selected = false, isSingleChoice = false), |
| 198 | + PreviewData(selected = true, isSingleChoice = false), |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +@Preview(name = "Light", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 203 | +@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 204 | +@Composable |
| 205 | +private fun SingleChoiceIconQuestionPreview() { |
| 206 | + var selectedAnswer: Answer.SingleChoice? by remember { |
| 207 | + mutableStateOf(Answer.SingleChoice(R.string.bugchaos)) |
| 208 | + } |
| 209 | + |
| 210 | + JetsurveyTheme { |
| 211 | + SingleChoiceQuestion( |
| 212 | + options = listOf( |
| 213 | + AnswerOption(R.string.spark, R.drawable.spark), |
| 214 | + AnswerOption(R.string.lenz, R.drawable.lenz), |
| 215 | + AnswerOption(R.string.bugchaos, R.drawable.bug_of_chaos), |
| 216 | + AnswerOption(R.string.frag, R.drawable.frag) |
| 217 | + ), |
| 218 | + answer = selectedAnswer, |
| 219 | + onAnswerSelected = { textRes -> selectedAnswer = Answer.SingleChoice(textRes) } |
| 220 | + ) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +@Preview(name = "Light", uiMode = Configuration.UI_MODE_NIGHT_NO) |
| 225 | +@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 226 | +@Composable |
| 227 | +private fun SingleChoiceQuestionPreview() { |
| 228 | + var selectedAnswer: Answer.SingleChoice? by remember { |
| 229 | + mutableStateOf(Answer.SingleChoice(R.string.bugchaos)) |
| 230 | + } |
| 231 | + |
| 232 | + JetsurveyTheme { |
| 233 | + SingleChoiceQuestion( |
| 234 | + options = listOf( |
| 235 | + AnswerOption(R.string.star_trek), |
| 236 | + AnswerOption(R.string.social_network), |
| 237 | + AnswerOption(R.string.back_to_future), |
| 238 | + AnswerOption(R.string.outbreak) |
| 239 | + ), |
| 240 | + answer = selectedAnswer, |
| 241 | + onAnswerSelected = { textRes -> selectedAnswer = Answer.SingleChoice(textRes) } |
| 242 | + ) |
| 243 | + } |
| 244 | +} |
0 commit comments