Skip to content

Commit e830e63

Browse files
Merge pull request #476 from angeles-bilbao6/issue460/date-slider-photo
[Jetsurvey] Issue460/date slider photo
2 parents c5d572e + 90434ac commit e830e63

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ sealed class PossibleAnswer {
6161
val steps: Int,
6262
@StringRes val startText: Int,
6363
@StringRes val endText: Int,
64-
val defaultValue: Float = range.start
64+
@StringRes val neutralText: Int,
65+
val defaultValue: Float = 5.5f
6566
) : PossibleAnswer()
6667
}
6768

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ class SurveyFragment : Fragment() {
8888
}
8989

9090
private fun showDatePicker(questionId: Int) {
91-
val picker = MaterialDatePicker.Builder.datePicker().build()
91+
val date = viewModel.getCurrentDate(questionId = questionId)
92+
val picker = MaterialDatePicker.Builder.datePicker()
93+
.setSelection(date)
94+
.build()
9295
activity?.let {
9396
picker.show(it.supportFragmentManager, picker.toString())
9497
picker.addOnPositiveButtonClickListener {

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

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.example.compose.jetsurvey.survey
1818

19-
import androidx.annotation.StringRes
2019
import androidx.compose.foundation.BorderStroke
2120
import androidx.compose.foundation.Image
2221
import androidx.compose.foundation.background
22+
import androidx.compose.foundation.border
2323
import androidx.compose.foundation.clickable
2424
import androidx.compose.foundation.layout.Arrangement
2525
import androidx.compose.foundation.layout.Column
@@ -36,6 +36,7 @@ import androidx.compose.foundation.layout.wrapContentSize
3636
import androidx.compose.foundation.lazy.LazyColumn
3737
import androidx.compose.foundation.selection.selectable
3838
import androidx.compose.material.Button
39+
import androidx.compose.material.ButtonDefaults
3940
import androidx.compose.material.Checkbox
4041
import androidx.compose.material.CheckboxDefaults
4142
import androidx.compose.material.ContentAlpha
@@ -50,6 +51,7 @@ import androidx.compose.material.Surface
5051
import androidx.compose.material.Text
5152
import androidx.compose.material.icons.Icons
5253
import androidx.compose.material.icons.filled.AddAPhoto
54+
import androidx.compose.material.icons.filled.ArrowDropDown
5355
import androidx.compose.material.icons.filled.SwapHoriz
5456
import androidx.compose.runtime.Composable
5557
import androidx.compose.runtime.CompositionLocalProvider
@@ -61,11 +63,15 @@ import androidx.compose.ui.Alignment
6163
import androidx.compose.ui.Modifier
6264
import androidx.compose.ui.res.painterResource
6365
import androidx.compose.ui.res.stringResource
66+
import androidx.compose.ui.text.style.TextAlign
6467
import androidx.compose.ui.tooling.preview.Preview
6568
import androidx.compose.ui.unit.dp
6669
import com.example.compose.jetsurvey.R
6770
import com.example.compose.jetsurvey.theme.JetsurveyTheme
6871
import com.google.accompanist.coil.rememberCoilPainter
72+
import java.text.SimpleDateFormat
73+
import java.util.Date
74+
import java.util.Locale
6975

7076
@Composable
7177
fun Question(
@@ -305,7 +311,6 @@ private fun ActionQuestion(
305311
SurveyActionType.PICK_DATE -> {
306312
DateQuestion(
307313
questionId = questionId,
308-
answerLabel = possibleAnswer.label,
309314
answer = answer,
310315
onAction = onAction,
311316
modifier = modifier
@@ -379,25 +384,41 @@ private fun PhotoQuestion(
379384
@Composable
380385
private fun DateQuestion(
381386
questionId: Int,
382-
@StringRes answerLabel: Int,
383387
answer: Answer.Action?,
384388
onAction: (Int, SurveyActionType) -> Unit,
385389
modifier: Modifier = Modifier
386390
) {
387-
// val date = Date()
388-
// val formatter = SimpleDateFormat("dd/MM/yyyy")
391+
val date = if (answer != null && answer.result is SurveyActionResult.Date) {
392+
answer.result.date
393+
} else {
394+
SimpleDateFormat("EEE, MMM d", Locale.getDefault()).format(Date())
395+
}
389396
Button(
390397
onClick = { onAction(questionId, SurveyActionType.PICK_DATE) },
391-
modifier = modifier.padding(vertical = 20.dp)
392-
) {
393-
Text(text = stringResource(id = answerLabel))
394-
}
398+
colors = ButtonDefaults.buttonColors(
399+
backgroundColor = MaterialTheme.colors.onPrimary,
400+
contentColor = MaterialTheme.colors.onSecondary
401+
),
402+
shape = MaterialTheme.shapes.small,
403+
modifier = modifier
404+
.padding(vertical = 20.dp)
405+
.height(54.dp),
406+
elevation = ButtonDefaults.elevation(0.dp),
407+
border = BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.12f))
395408

396-
if (answer != null && answer.result is SurveyActionResult.Date) {
409+
) {
397410
Text(
398-
text = stringResource(R.string.selected_date, answer.result.date),
399-
style = MaterialTheme.typography.h4,
400-
modifier = Modifier.padding(vertical = 20.dp)
411+
text = date,
412+
modifier = Modifier
413+
.fillMaxWidth()
414+
.weight(1.8f)
415+
)
416+
Icon(
417+
imageVector = Icons.Filled.ArrowDropDown,
418+
contentDescription = null,
419+
modifier = Modifier
420+
.fillMaxWidth()
421+
.weight(0.2f)
401422
)
402423
}
403424
}
@@ -430,10 +451,7 @@ private fun SliderQuestion(
430451
mutableStateOf(answer?.answerValue ?: possibleAnswer.defaultValue)
431452
}
432453
Row(modifier = modifier) {
433-
Text(
434-
text = stringResource(id = possibleAnswer.startText),
435-
modifier = Modifier.align(Alignment.CenterVertically)
436-
)
454+
437455
Slider(
438456
value = sliderPosition,
439457
onValueChange = {
@@ -446,9 +464,31 @@ private fun SliderQuestion(
446464
.weight(1f)
447465
.padding(horizontal = 16.dp)
448466
)
467+
}
468+
Row {
469+
Text(
470+
text = stringResource(id = possibleAnswer.startText),
471+
style = MaterialTheme.typography.caption,
472+
textAlign = TextAlign.Start,
473+
modifier = Modifier
474+
.fillMaxWidth()
475+
.weight(1.8f)
476+
)
477+
Text(
478+
text = stringResource(id = possibleAnswer.neutralText),
479+
style = MaterialTheme.typography.caption,
480+
textAlign = TextAlign.Center,
481+
modifier = Modifier
482+
.fillMaxWidth()
483+
.weight(1.8f)
484+
)
449485
Text(
450486
text = stringResource(id = possibleAnswer.endText),
451-
modifier = Modifier.align(Alignment.CenterVertically)
487+
style = MaterialTheme.typography.caption,
488+
textAlign = TextAlign.End,
489+
modifier = Modifier
490+
.fillMaxWidth()
491+
.weight(1.8f)
452492
)
453493
}
454494
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ private val jetpackQuestions = mutableListOf(
7979
answer = PossibleAnswer.Slider(
8080
range = 1f..10f,
8181
steps = 3,
82-
startText = R.string.selfie_min,
83-
endText = R.string.selfie_max
82+
startText = R.string.strongly_dislike,
83+
endText = R.string.strongly_like,
84+
neutralText = R.string.neutral
8485
)
8586
)
8687
).apply {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.compose.jetsurvey.survey
1818

19+
import androidx.compose.animation.core.animateFloatAsState
1920
import androidx.compose.foundation.layout.Box
2021
import androidx.compose.foundation.layout.Column
2122
import androidx.compose.foundation.layout.Row
@@ -34,13 +35,15 @@ import androidx.compose.material.LinearProgressIndicator
3435
import androidx.compose.material.LocalContentAlpha
3536
import androidx.compose.material.MaterialTheme
3637
import androidx.compose.material.OutlinedButton
38+
import androidx.compose.material.ProgressIndicatorDefaults
3739
import androidx.compose.material.Scaffold
3840
import androidx.compose.material.Surface
3941
import androidx.compose.material.Text
4042
import androidx.compose.material.icons.Icons
4143
import androidx.compose.material.icons.filled.Close
4244
import androidx.compose.runtime.Composable
4345
import androidx.compose.runtime.CompositionLocalProvider
46+
import androidx.compose.runtime.getValue
4447
import androidx.compose.runtime.remember
4548
import androidx.compose.ui.Alignment
4649
import androidx.compose.ui.Modifier
@@ -206,8 +209,12 @@ private fun SurveyTopAppBar(
206209
}
207210
}
208211
}
212+
val animatedProgress by animateFloatAsState(
213+
targetValue = (questionIndex + 1) / totalQuestionsCount.toFloat(),
214+
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
215+
)
209216
LinearProgressIndicator(
210-
progress = (questionIndex + 1) / totalQuestionsCount.toFloat(),
217+
progress = animatedProgress,
211218
modifier = Modifier
212219
.fillMaxWidth()
213220
.padding(horizontal = 20.dp),

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import androidx.lifecycle.MutableLiveData
2222
import androidx.lifecycle.ViewModel
2323
import androidx.lifecycle.ViewModelProvider
2424
import androidx.lifecycle.viewModelScope
25+
import java.text.SimpleDateFormat
26+
import java.util.Date
27+
import java.util.Locale
2528
import kotlinx.coroutines.launch
2629

2730
class SurveyViewModel(
@@ -69,6 +72,10 @@ class SurveyViewModel(
6972
updateStateWithActionResult(questionId, SurveyActionResult.Date(date))
7073
}
7174

75+
fun getCurrentDate(questionId: Int): Long {
76+
return getSelectedDate(questionId)
77+
}
78+
7279
fun getUriToSaveImage(): Uri? {
7380
uri = photoUriManager.buildNewUri()
7481
return uri
@@ -101,6 +108,25 @@ class SurveyViewModel(
101108
}
102109
return null
103110
}
111+
112+
private fun getSelectedDate(questionId: Int): Long {
113+
val latestState = _uiState.value
114+
var ret = Date().time
115+
if (latestState != null && latestState is SurveyState.Questions) {
116+
val question =
117+
latestState.questionsState.first { questionState ->
118+
questionState.question.id == questionId
119+
}
120+
val answer: Answer.Action? = question.answer as Answer.Action?
121+
if (answer != null && answer.result is SurveyActionResult.Date) {
122+
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH)
123+
val formatted = formatter.parse(answer.result.date)
124+
if (formatted is Date)
125+
ret = formatted.time
126+
}
127+
}
128+
return ret
129+
}
104130
}
105131

106132
class SurveyViewModelFactory(

Jetsurvey/app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@
9292
<string name="survey_result">Congratulations, you are %s</string>
9393
<string name="survey_result_description">You are a curious developer, always willing to try
9494
something new. You want to stay up to date with the trends to Compose is your middle name</string>
95+
<string name="strongly_dislike">Strongly\nDislike</string>
96+
<string name="strongly_like">Strongly\nLike</string>
97+
<string name="neutral">Neutral</string>
9598
</resources>

0 commit comments

Comments
 (0)