Skip to content

Commit 44b1708

Browse files
[Jetsurvey] Fixing the way the state is updated when taking a photo
1 parent ba9954e commit 44b1708

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyQuestions.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ private fun PhotoQuestion(
311311
}
312312
OutlinedButton(
313313
onClick = { onAction(questionId, SurveyActionType.TAKE_PHOTO) },
314-
modifier = modifier
314+
modifier = modifier,
315+
contentPadding = PaddingValues()
315316
) {
316317
Column {
317318
if (answer != null && answer.result is SurveyActionResult.Photo) {
@@ -326,7 +327,8 @@ private fun PhotoQuestion(
326327
modifier = Modifier
327328
.fillMaxWidth()
328329
.wrapContentSize(Alignment.BottomCenter)
329-
.padding(vertical = 26.dp)
330+
.padding(vertical = 26.dp),
331+
verticalAlignment = Alignment.Bottom
330332
) {
331333
Icon(resource)
332334
Spacer(modifier = Modifier.width(8.dp))

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyRepository.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ import com.example.compose.jetsurvey.survey.SurveyActionType.TAKE_PHOTO
2525

2626
// Static data of questions
2727
private val jetpackQuestions = listOf(
28-
Question(
29-
id = 975,
30-
questionText = R.string.selfie_skills,
31-
answer = Action(label = R.string.add_photo, actionType = TAKE_PHOTO)
32-
),
3328
Question(
3429
id = 1,
3530
questionText = R.string.in_my_free_time,
@@ -86,6 +81,11 @@ private val jetpackQuestions = listOf(
8681
startText = R.string.selfie_min,
8782
endText = R.string.selfie_max
8883
)
84+
),
85+
Question(
86+
id = 975,
87+
questionText = R.string.selfie_skills,
88+
answer = Action(label = R.string.add_photo, actionType = TAKE_PHOTO)
8989
)
9090
)
9191
private val jetpackSurvey = Survey(

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyScreen.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ import androidx.compose.material.icons.Icons
4141
import androidx.compose.material.icons.filled.Close
4242
import androidx.compose.runtime.Composable
4343
import androidx.compose.runtime.Providers
44-
import androidx.compose.runtime.getValue
4544
import androidx.compose.runtime.remember
46-
import androidx.compose.runtime.savedinstancestate.savedInstanceState
47-
import androidx.compose.runtime.setValue
4845
import androidx.compose.ui.Modifier
4946
import androidx.compose.ui.res.stringResource
5047
import androidx.compose.ui.text.annotatedString
@@ -61,9 +58,9 @@ fun SurveyQuestionsScreen(
6158
onDonePressed: () -> Unit,
6259
onBackPressed: () -> Unit
6360
) {
64-
var currentQuestionIndex by savedInstanceState { 0 }
65-
val questionState =
66-
remember(currentQuestionIndex) { questions.questionsState[currentQuestionIndex] }
61+
val questionState = remember(questions.currentQuestionIndex) {
62+
questions.questionsState[questions.currentQuestionIndex]
63+
}
6764

6865
Surface(modifier = Modifier.fillMaxSize()) {
6966
Scaffold(
@@ -91,8 +88,8 @@ fun SurveyQuestionsScreen(
9188
bottomBar = {
9289
SurveyBottomBar(
9390
questionState = questionState,
94-
onPreviousPressed = { currentQuestionIndex-- },
95-
onNextPressed = { currentQuestionIndex++ },
91+
onPreviousPressed = { questions.currentQuestionIndex-- },
92+
onNextPressed = { questions.currentQuestionIndex++ },
9693
onDonePressed = onDonePressed
9794
)
9895
}
@@ -158,7 +155,7 @@ private fun TopAppBarTitle(
158155
fontWeight = FontWeight.Bold
159156
)
160157
val totalStyle = MaterialTheme.typography.caption.toSpanStyle().copy(
161-
fontWeight = FontWeight.SemiBold
158+
fontWeight = FontWeight.Normal
162159
)
163160
val text = annotatedString {
164161
withStyle(style = indexStyle) {

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyState.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ sealed class SurveyState {
3838
data class Questions(
3939
@StringRes val surveyTitle: Int,
4040
val questionsState: List<QuestionState>
41-
) : SurveyState()
41+
) : SurveyState() {
42+
var currentQuestionIndex by mutableStateOf(0)
43+
}
4244

4345
data class Result(
4446
@StringRes val surveyTitle: Int,

Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class SurveyViewModel(private val surveyRepository: SurveyRepository) : ViewMode
8484
private fun getLatestQuestionId(): Int? {
8585
val latestState = _uiState.value
8686
if (latestState != null && latestState is SurveyState.Questions) {
87-
return latestState.questionsState[0].question.id
87+
return latestState.questionsState[latestState.currentQuestionIndex].question.id
8888
}
8989
return null
9090
}

0 commit comments

Comments
 (0)