Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SurveyFragment : Fragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
return ComposeView(requireContext()).apply {
// In order for savedState to work, the same ID needs to be used for all instances.
id = R.id.sign_in_fragment
Expand Down Expand Up @@ -95,7 +95,7 @@ class SurveyFragment : Fragment() {
activity?.let {
picker.show(it.supportFragmentManager, picker.toString())
picker.addOnPositiveButtonClickListener {
viewModel.onDatePicked(questionId, picker.headerText)
viewModel.onDatePicked(questionId, picker.selection)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ private fun DateQuestion(
val date = if (answer != null && answer.result is SurveyActionResult.Date) {
answer.result.date
} else {
SimpleDateFormat("EEE, MMM d", Locale.getDefault()).format(Date())
SimpleDateFormat(simpleDateFormatPattern, Locale.getDefault()).format(Date())
}
Button(
onClick = { onAction(questionId, SurveyActionType.PICK_DATE) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import java.util.Date
import java.util.Locale
import kotlinx.coroutines.launch

const val simpleDateFormatPattern = "EEE, MMM d"

class SurveyViewModel(
private val surveyRepository: SurveyRepository,
private val photoUriManager: PhotoUriManager
Expand Down Expand Up @@ -68,8 +70,13 @@ class SurveyViewModel(
_uiState.value = SurveyState.Result(surveyQuestions.surveyTitle, result)
}

fun onDatePicked(questionId: Int, date: String) {
updateStateWithActionResult(questionId, SurveyActionResult.Date(date))
fun onDatePicked(questionId: Int, pickerSelection: Long?) {
val selectedDate = Date().apply {
time = pickerSelection ?: getCurrentDate(questionId)
}
val formattedDate =
SimpleDateFormat(simpleDateFormatPattern, Locale.getDefault()).format(selectedDate)
updateStateWithActionResult(questionId, SurveyActionResult.Date(formattedDate))
}

fun getCurrentDate(questionId: Int): Long {
Expand Down Expand Up @@ -119,7 +126,7 @@ class SurveyViewModel(
}
val answer: Answer.Action? = question.answer as Answer.Action?
if (answer != null && answer.result is SurveyActionResult.Date) {
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH)
val formatter = SimpleDateFormat(simpleDateFormatPattern, Locale.ENGLISH)
val formatted = formatter.parse(answer.result.date)
if (formatted is Date)
ret = formatted.time
Expand Down