-
Notifications
You must be signed in to change notification settings - Fork 246
/
EditTextIntegerViewHolderFactory.kt
118 lines (108 loc) · 4.4 KB
/
EditTextIntegerViewHolderFactory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2022-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.fhir.datacapture.views.factories
import android.icu.number.NumberFormatter
import android.icu.text.DecimalFormat
import android.os.Build
import android.text.Editable
import android.text.InputType
import androidx.annotation.RequiresApi
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.getValidationErrorMessage
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import java.util.Locale
import org.hl7.fhir.r4.model.IntegerType
import org.hl7.fhir.r4.model.QuestionnaireResponse
internal object EditTextIntegerViewHolderFactory :
EditTextViewHolderFactory(R.layout.edit_text_single_line_view) {
override fun getQuestionnaireItemViewHolderDelegate() =
object :
QuestionnaireItemEditTextViewHolderDelegate(
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED,
) {
override suspend fun handleInput(
editable: Editable,
questionnaireViewItem: QuestionnaireViewItem,
) {
val input = editable.toString()
if (input.isEmpty()) {
questionnaireViewItem.clearAnswer()
return
}
val inputInteger = input.toIntOrNull()
if (inputInteger != null) {
questionnaireViewItem.setAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent()
.setValue(IntegerType(input)),
)
} else {
questionnaireViewItem.setDraftAnswer(input)
}
}
override fun updateInputTextUI(
questionnaireViewItem: QuestionnaireViewItem,
textInputEditText: TextInputEditText,
) {
val answer =
questionnaireViewItem.answers.singleOrNull()?.valueIntegerType?.value?.toString()
val draftAnswer = questionnaireViewItem.draftAnswer?.toString()
// Update the text on the UI only if the value of the saved answer or draft answer
// is different from what the user is typing. We compare the two fields as integers to
// avoid shifting focus if the text values are different, but their integer representation
// is the same (e.g. "001" compared to "1")
if (answer.isNullOrEmpty() && draftAnswer.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (answer?.toIntOrNull() != textInputEditText.text.toString().toIntOrNull()) {
textInputEditText.setText(answer)
} else if (draftAnswer != null && draftAnswer != textInputEditText.text.toString()) {
textInputEditText.setText(draftAnswer)
}
}
override fun updateValidationTextUI(
questionnaireViewItem: QuestionnaireViewItem,
textInputLayout: TextInputLayout,
) {
textInputLayout.error =
getValidationErrorMessage(
textInputLayout.context,
questionnaireViewItem,
questionnaireViewItem.validationResult,
)
// Update error message if draft answer present
if (questionnaireViewItem.draftAnswer != null) {
textInputLayout.error =
textInputLayout.context.getString(
R.string.integer_format_validation_error_msg,
formatInteger(Int.MIN_VALUE),
formatInteger(Int.MAX_VALUE),
)
}
}
}
private fun formatInteger(value: Int): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
numberFormatter.format(value).toString()
} else {
decimalFormat.format(value)
}
}
private val numberFormatter
@RequiresApi(Build.VERSION_CODES.R) get() = NumberFormatter.withLocale(Locale.getDefault())
private val decimalFormat
get() = DecimalFormat.getInstance(Locale.getDefault())
}