Skip to content

Commit 35660c6

Browse files
Merge pull request #513 from Abhimanyu14/abhimanyu/jetsurvey-issue-512
[Jetsurvey] fixed app crash on changing date selection
2 parents a638b28 + b79e679 commit 35660c6

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SurveyFragment : Fragment() {
4848
inflater: LayoutInflater,
4949
container: ViewGroup?,
5050
savedInstanceState: Bundle?
51-
): View? {
51+
): View {
5252
return ComposeView(requireContext()).apply {
5353
// In order for savedState to work, the same ID needs to be used for all instances.
5454
id = R.id.sign_in_fragment
@@ -108,7 +108,7 @@ class SurveyFragment : Fragment() {
108108
activity?.let {
109109
picker.show(it.supportFragmentManager, picker.toString())
110110
picker.addOnPositiveButtonClickListener {
111-
viewModel.onDatePicked(questionId, picker.headerText)
111+
viewModel.onDatePicked(questionId, picker.selection)
112112
}
113113
}
114114
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ private fun DateQuestion(
704704
val date = if (answer != null && answer.result is SurveyActionResult.Date) {
705705
answer.result.date
706706
} else {
707-
SimpleDateFormat("EEE, MMM d", Locale.getDefault()).format(Date())
707+
SimpleDateFormat(simpleDateFormatPattern, Locale.getDefault()).format(Date())
708708
}
709709
Button(
710710
onClick = { onAction(questionId, SurveyActionType.PICK_DATE) },

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import java.util.Date
3030
import java.util.Locale
3131
import kotlinx.coroutines.launch
3232

33+
const val simpleDateFormatPattern = "EEE, MMM d"
34+
3335
class SurveyViewModel(
3436
private val surveyRepository: SurveyRepository,
3537
private val photoUriManager: PhotoUriManager
@@ -74,8 +76,13 @@ class SurveyViewModel(
7476
_uiState.value = SurveyState.Result(surveyQuestions.surveyTitle, result)
7577
}
7678

77-
fun onDatePicked(questionId: Int, date: String) {
78-
updateStateWithActionResult(questionId, SurveyActionResult.Date(date))
79+
fun onDatePicked(questionId: Int, pickerSelection: Long?) {
80+
val selectedDate = Date().apply {
81+
time = pickerSelection ?: getCurrentDate(questionId)
82+
}
83+
val formattedDate =
84+
SimpleDateFormat(simpleDateFormatPattern, Locale.getDefault()).format(selectedDate)
85+
updateStateWithActionResult(questionId, SurveyActionResult.Date(formattedDate))
7986
}
8087

8188
fun getCurrentDate(questionId: Int): Long {
@@ -130,7 +137,7 @@ class SurveyViewModel(
130137
}
131138
val answer: Answer.Action? = question.answer as Answer.Action?
132139
if (answer != null && answer.result is SurveyActionResult.Date) {
133-
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH)
140+
val formatter = SimpleDateFormat(simpleDateFormatPattern, Locale.ENGLISH)
134141
val formatted = formatter.parse(answer.result.date)
135142
if (formatted is Date)
136143
ret = formatted.time

0 commit comments

Comments
 (0)