-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Jetsurvey] Simplification of survey screens (#1058)
This eliminates the survey repository and related data models in favor of simplifying the code base. Each of the different question types has a generic composable such as MultipleChoiceQuestion, which takes all of the relevant strings/resources to generate the UI. The actual survey data is baked into a specific composable in Survey.kt, such as FreeTimeQuestion, instead of coming from a data model. The SurveyViewModel is now much more explicit with methods for each question such as onFreeTimeResponse. In a production app, you'd likely want this to be more flexible to handle server-side questions, but this code pattern makes the sample easier to skim. Every response is maintained in SurveyViewModel as a separate state. This increases verbosity in favor of better readability. Note: This does not animate between the final survey question and the survey results. When the work to move the app to Compose Navigation is done, the results will be a separate destination that is navigated to and animated that way. This fixes #1054.
- Loading branch information
Showing
20 changed files
with
943 additions
and
1,199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/QuestionWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.compose.jetsurvey.survey | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import com.example.compose.jetsurvey.theme.slightlyDeemphasizedAlpha | ||
import com.example.compose.jetsurvey.theme.stronglyDeemphasizedAlpha | ||
|
||
/** | ||
* A scrollable container with the question's title, direction, and dynamic content. | ||
* | ||
* @param titleResourceId String resource to use for the question's title | ||
* @param modifier Modifier to apply to the entire wrapper | ||
* @param directionsResourceId String resource to use for the question's directions; the direction | ||
* UI will be omitted if null is passed | ||
* @param content Composable to display after the title and option directions | ||
*/ | ||
@Composable | ||
fun QuestionWrapper( | ||
@StringRes titleResourceId: Int, | ||
modifier: Modifier = Modifier, | ||
@StringRes directionsResourceId: Int? = null, | ||
content: @Composable () -> Unit, | ||
) { | ||
Column( | ||
modifier = modifier | ||
.padding(horizontal = 16.dp) | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
Spacer(Modifier.height(32.dp)) | ||
QuestionTitle(titleResourceId) | ||
directionsResourceId?.let { | ||
Spacer(Modifier.height(18.dp)) | ||
QuestionDirections(it) | ||
} | ||
Spacer(Modifier.height(18.dp)) | ||
|
||
content() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun QuestionTitle( | ||
@StringRes title: Int, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Text( | ||
text = stringResource(id = title), | ||
style = MaterialTheme.typography.titleMedium, | ||
color = MaterialTheme.colorScheme.onSurface.copy(alpha = slightlyDeemphasizedAlpha), | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.background( | ||
color = MaterialTheme.colorScheme.inverseOnSurface, | ||
shape = MaterialTheme.shapes.small | ||
) | ||
.padding(vertical = 24.dp, horizontal = 16.dp) | ||
) | ||
} | ||
|
||
@Composable | ||
private fun QuestionDirections( | ||
@StringRes directionsResourceId: Int, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Text( | ||
text = stringResource(id = directionsResourceId), | ||
color = MaterialTheme.colorScheme.onSurface | ||
.copy(alpha = stronglyDeemphasizedAlpha), | ||
style = MaterialTheme.typography.bodySmall, | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.padding(horizontal = 8.dp) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.