|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.fenix.microsurvey.ui |
| 6 | + |
| 7 | +import androidx.annotation.DrawableRes |
| 8 | +import androidx.compose.foundation.Image |
| 9 | +import androidx.compose.foundation.layout.Column |
| 10 | +import androidx.compose.foundation.layout.Row |
| 11 | +import androidx.compose.foundation.layout.Spacer |
| 12 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 13 | +import androidx.compose.foundation.layout.padding |
| 14 | +import androidx.compose.foundation.layout.size |
| 15 | +import androidx.compose.foundation.layout.width |
| 16 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 17 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 18 | +import androidx.compose.material.Card |
| 19 | +import androidx.compose.material.Text |
| 20 | +import androidx.compose.runtime.Composable |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.graphics.Color |
| 24 | +import androidx.compose.ui.res.painterResource |
| 25 | +import androidx.compose.ui.res.stringResource |
| 26 | +import androidx.compose.ui.tooling.preview.PreviewScreenSizes |
| 27 | +import androidx.compose.ui.unit.Dp |
| 28 | +import androidx.compose.ui.unit.dp |
| 29 | +import org.mozilla.fenix.R |
| 30 | +import org.mozilla.fenix.compose.annotation.LightDarkPreview |
| 31 | +import org.mozilla.fenix.compose.list.RadioButtonListItem |
| 32 | +import org.mozilla.fenix.theme.FirefoxTheme |
| 33 | + |
| 34 | +private val shape = RoundedCornerShape(16.dp) |
| 35 | +private val elevation: Dp = 5.dp |
| 36 | + |
| 37 | +/** |
| 38 | + * The micro survey content UI to hold question and answer data. |
| 39 | + * |
| 40 | + * @param question The survey question text. |
| 41 | + * @param answers The survey answer text options available for the question. |
| 42 | + * @param icon The survey icon, this will represent the feature the survey is for. |
| 43 | + * @param backgroundColor The view background color. |
| 44 | + * @param selectedAnswer The current selected answer. Will be null until user selects an option. |
| 45 | + * @param onSelectionChange An event that updates the [selectedAnswer]. |
| 46 | + */ |
| 47 | +@Composable |
| 48 | +fun MicroSurveyContent( |
| 49 | + question: String, |
| 50 | + answers: List<String>, |
| 51 | + @DrawableRes icon: Int = R.drawable.ic_print, // todo currently unknown what the default will be if any. |
| 52 | + backgroundColor: Color = FirefoxTheme.colors.layer2, |
| 53 | + selectedAnswer: String? = null, |
| 54 | + onSelectionChange: (String) -> Unit, |
| 55 | +) { |
| 56 | + Card( |
| 57 | + shape = shape, |
| 58 | + backgroundColor = backgroundColor, |
| 59 | + elevation = elevation, |
| 60 | + modifier = Modifier |
| 61 | + .wrapContentHeight() |
| 62 | + .fillMaxWidth(), |
| 63 | + ) { |
| 64 | + Column(modifier = Modifier.wrapContentHeight()) { |
| 65 | + Header(icon, question) |
| 66 | + |
| 67 | + answers.forEach { |
| 68 | + RadioButtonListItem( |
| 69 | + label = it, |
| 70 | + selected = selectedAnswer == it, |
| 71 | + onClick = { |
| 72 | + onSelectionChange.invoke(it) |
| 73 | + }, |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Composable |
| 81 | +private fun Header(icon: Int, question: String) { |
| 82 | + Row( |
| 83 | + modifier = Modifier.padding(16.dp), |
| 84 | + verticalAlignment = Alignment.CenterVertically, |
| 85 | + ) { |
| 86 | + Image( |
| 87 | + painter = painterResource(icon), |
| 88 | + contentDescription = "Survey icon", // todo update to string res once a11y strings are available. |
| 89 | + modifier = Modifier.size(24.dp), |
| 90 | + ) |
| 91 | + |
| 92 | + Spacer(modifier = Modifier.width(16.dp)) |
| 93 | + |
| 94 | + Text( |
| 95 | + text = question, |
| 96 | + color = FirefoxTheme.colors.textPrimary, |
| 97 | + style = FirefoxTheme.typography.headline7, |
| 98 | + ) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Preview for [MicroSurveyContent]. |
| 104 | + */ |
| 105 | +@PreviewScreenSizes |
| 106 | +@LightDarkPreview |
| 107 | +@Composable |
| 108 | +fun MicroSurveyContentPreview() { |
| 109 | + FirefoxTheme { |
| 110 | + MicroSurveyContent( |
| 111 | + question = "How satisfied are you with printing in Firefox?", |
| 112 | + icon = R.drawable.ic_print, |
| 113 | + answers = listOf( |
| 114 | + stringResource(id = R.string.likert_scale_option_1), |
| 115 | + stringResource(id = R.string.likert_scale_option_2), |
| 116 | + stringResource(id = R.string.likert_scale_option_3), |
| 117 | + stringResource(id = R.string.likert_scale_option_4), |
| 118 | + stringResource(id = R.string.likert_scale_option_5), |
| 119 | + ), |
| 120 | + onSelectionChange = {}, |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
0 commit comments