|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.fhir.datacapture.views.factories |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.content.Context |
| 21 | +import android.text.InputType |
| 22 | +import android.text.format.DateFormat |
| 23 | +import android.view.View |
| 24 | +import androidx.appcompat.app.AppCompatActivity |
| 25 | +import androidx.lifecycle.lifecycleScope |
| 26 | +import com.google.android.fhir.datacapture.R |
| 27 | +import com.google.android.fhir.datacapture.extensions.getRequiredOrOptionalText |
| 28 | +import com.google.android.fhir.datacapture.extensions.toLocalizedString |
| 29 | +import com.google.android.fhir.datacapture.extensions.tryUnwrapContext |
| 30 | +import com.google.android.fhir.datacapture.views.HeaderView |
| 31 | +import com.google.android.fhir.datacapture.views.QuestionnaireViewItem |
| 32 | +import com.google.android.material.textfield.TextInputEditText |
| 33 | +import com.google.android.material.textfield.TextInputLayout |
| 34 | +import com.google.android.material.timepicker.MaterialTimePicker |
| 35 | +import com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_CLOCK |
| 36 | +import com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_KEYBOARD |
| 37 | +import com.google.android.material.timepicker.TimeFormat |
| 38 | +import java.time.LocalTime |
| 39 | +import java.time.format.DateTimeFormatter |
| 40 | +import kotlinx.coroutines.launch |
| 41 | +import org.hl7.fhir.r4.model.QuestionnaireResponse |
| 42 | +import org.hl7.fhir.r4.model.TimeType |
| 43 | + |
| 44 | +object TimePickerViewHolderFactory : QuestionnaireItemViewHolderFactory(R.layout.time_picker_view) { |
| 45 | + |
| 46 | + override fun getQuestionnaireItemViewHolderDelegate() = |
| 47 | + object : QuestionnaireItemViewHolderDelegate { |
| 48 | + private val TAG = "time-picker" |
| 49 | + private lateinit var context: AppCompatActivity |
| 50 | + private lateinit var header: HeaderView |
| 51 | + private lateinit var timeInputLayout: TextInputLayout |
| 52 | + private lateinit var timeInputEditText: TextInputEditText |
| 53 | + override lateinit var questionnaireViewItem: QuestionnaireViewItem |
| 54 | + |
| 55 | + override fun init(itemView: View) { |
| 56 | + context = itemView.context.tryUnwrapContext()!! |
| 57 | + header = itemView.findViewById(R.id.header) |
| 58 | + timeInputLayout = itemView.findViewById(R.id.text_input_layout) |
| 59 | + timeInputEditText = itemView.findViewById(R.id.text_input_edit_text) |
| 60 | + timeInputEditText.inputType = InputType.TYPE_NULL |
| 61 | + timeInputEditText.hint = itemView.context.getString(R.string.time) |
| 62 | + |
| 63 | + timeInputLayout.setEndIconOnClickListener { |
| 64 | + // The application is wrapped in a ContextThemeWrapper in QuestionnaireFragment |
| 65 | + // and again in TextInputEditText during layout inflation. As a result, it is |
| 66 | + // necessary to access the base context twice to retrieve the application object |
| 67 | + // from the view's context. |
| 68 | + val context = itemView.context.tryUnwrapContext()!! |
| 69 | + buildMaterialTimePicker(context, INPUT_MODE_CLOCK) |
| 70 | + } |
| 71 | + timeInputEditText.setOnClickListener { |
| 72 | + buildMaterialTimePicker(itemView.context, INPUT_MODE_KEYBOARD) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @SuppressLint("NewApi") // java.time APIs can be used due to desugaring |
| 77 | + override fun bind(questionnaireViewItem: QuestionnaireViewItem) { |
| 78 | + clearPreviousState() |
| 79 | + header.bind(questionnaireViewItem) |
| 80 | + timeInputLayout.helperText = getRequiredOrOptionalText(questionnaireViewItem, context) |
| 81 | + |
| 82 | + val questionnaireItemViewItemDateTimeAnswer = |
| 83 | + questionnaireViewItem.answers.singleOrNull()?.valueTimeType?.localTime |
| 84 | + |
| 85 | + // If there is no set answer in the QuestionnaireItemViewItem, make the time field empty. |
| 86 | + timeInputEditText.setText( |
| 87 | + questionnaireItemViewItemDateTimeAnswer?.toLocalizedString(timeInputEditText.context) |
| 88 | + ?: "", |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + override fun setReadOnly(isReadOnly: Boolean) { |
| 93 | + // The system outside this delegate should only be able to mark it read only. Otherwise, it |
| 94 | + // will change the state set by this delegate in bindView(). |
| 95 | + if (isReadOnly) { |
| 96 | + timeInputEditText.isEnabled = false |
| 97 | + timeInputLayout.isEnabled = false |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private fun buildMaterialTimePicker(context: Context, inputMode: Int) { |
| 102 | + val selectedTime = |
| 103 | + questionnaireViewItem.answers.singleOrNull()?.valueTimeType?.localTime ?: LocalTime.now() |
| 104 | + val timeFormat = |
| 105 | + if (DateFormat.is24HourFormat(context)) { |
| 106 | + TimeFormat.CLOCK_24H |
| 107 | + } else { |
| 108 | + TimeFormat.CLOCK_12H |
| 109 | + } |
| 110 | + MaterialTimePicker.Builder() |
| 111 | + .setTitleText(R.string.select_time) |
| 112 | + .setHour(selectedTime.hour) |
| 113 | + .setMinute(selectedTime.minute) |
| 114 | + .setTimeFormat(timeFormat) |
| 115 | + .setInputMode(inputMode) |
| 116 | + .build() |
| 117 | + .apply { |
| 118 | + addOnPositiveButtonClickListener { |
| 119 | + with(LocalTime.of(this.hour, this.minute, 0)) { |
| 120 | + timeInputEditText.setText(this.toLocalizedString(context)) |
| 121 | + setQuestionnaireItemViewItemAnswer(this) |
| 122 | + timeInputEditText.clearFocus() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + .show(context.tryUnwrapContext()!!.supportFragmentManager, TAG) |
| 127 | + } |
| 128 | + |
| 129 | + /** Set the answer in the [QuestionnaireResponse]. */ |
| 130 | + private fun setQuestionnaireItemViewItemAnswer(localDateTime: LocalTime) = |
| 131 | + context.lifecycleScope.launch { |
| 132 | + questionnaireViewItem.setAnswer( |
| 133 | + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent() |
| 134 | + .setValue(TimeType(localDateTime.format(DateTimeFormatter.ISO_TIME))), |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + private fun clearPreviousState() { |
| 139 | + timeInputEditText.isEnabled = true |
| 140 | + timeInputLayout.isEnabled = true |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private val TimeType.localTime |
| 145 | + get() = |
| 146 | + LocalTime.of( |
| 147 | + hour, |
| 148 | + minute, |
| 149 | + second.toInt(), |
| 150 | + ) |
| 151 | +} |
0 commit comments