Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing the old state of date and date-time ViewHolders #1581

Merged
merged 5 commits into from
Sep 12, 2022
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 @@ -41,6 +41,7 @@ import java.time.ZoneId
import java.time.chrono.IsoChronology
import java.time.format.DateTimeFormatterBuilder
import java.time.format.FormatStyle
import java.util.Date
import java.util.Locale
import kotlin.math.abs
import kotlin.math.log10
Expand Down Expand Up @@ -102,8 +103,12 @@ internal object QuestionnaireItemDatePickerViewHolderFactory :
header.bind(questionnaireItemViewItem.questionnaireItem)
textInputLayout.hint = localePattern
textInputEditText.removeTextChangedListener(textWatcher)

if (textInputEditText.text.isNullOrEmpty()) {
if (isTextUpdateRequired(
textInputEditText.context,
questionnaireItemViewItem.answers.singleOrNull()?.valueDateType,
textInputEditText.text.toString()
)
) {
textInputEditText.setText(
questionnaireItemViewItem.answers.singleOrNull()
?.valueDateType
Expand Down Expand Up @@ -157,6 +162,20 @@ internal object QuestionnaireItemDatePickerViewHolderFactory :
}
}
}

private fun isTextUpdateRequired(
context: Context,
answer: DateType?,
inputText: String?
): Boolean {
val inputDate =
try {
parseDate(inputText, context)
} catch (e: Exception) {
null
}
return answer?.localDate != inputDate
}
}

internal const val TAG = "date-picker"
Expand Down Expand Up @@ -194,14 +213,17 @@ internal val DateType.localDate
internal val LocalDate.dateType
get() = DateType(year, monthValue - 1, dayOfMonth)

internal val Date.localDate
get() = LocalDate.of(year + 1900, month + 1, date)

internal fun parseDate(text: CharSequence?, context: Context): LocalDate {
val date =
val localDate =
if (isAndroidIcuSupported()) {
DateFormat.getDateInstance(DateFormat.SHORT).parse(text.toString())
} else {
android.text.format.DateFormat.getDateFormat(context).parse(text.toString())
}
val localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
DateFormat.getDateInstance(DateFormat.SHORT).parse(text.toString())
} else {
android.text.format.DateFormat.getDateFormat(context).parse(text.toString())
}
.localDate
// date/localDate with year more than 4 digit throws data format exception if deep copy
// operation get performed on QuestionnaireResponse,
// QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent in org.hl7.fhir.r4.model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ internal object QuestionnaireItemDateTimePickerViewHolderFactory :
val dateTime = questionnaireItemViewItem.answers.singleOrNull()?.valueDateTimeType
updateDateTimeInput(
dateTime?.let {
LocalDateTime.of(it.year, it.month + 1, it.day, it.hour, it.minute, it.second)
it.localDateTime.also {
localDate = it.toLocalDate()
localTime = it.toLocalTime()
}
}
)
textWatcher =
Expand Down Expand Up @@ -197,7 +200,12 @@ internal object QuestionnaireItemDateTimePickerViewHolderFactory :
/** Update the date and time input fields in the UI. */
private fun updateDateTimeInput(localDateTime: LocalDateTime?) {
enableOrDisableTimePicker(enableIt = localDateTime != null)
if (dateInputEditText.text.isNullOrEmpty()) {
if (isTextUpdateRequired(
dateInputEditText.context,
localDateTime,
dateInputEditText.text.toString()
)
) {
dateInputEditText.setText(localDateTime?.localizedDateString ?: "")
}
timeInputEditText.setText(
Expand Down Expand Up @@ -281,6 +289,21 @@ internal object QuestionnaireItemDateTimePickerViewHolderFactory :
timeInputLayout.isEnabled = enableIt
timeInputLayout.isEnabled = enableIt
}

private fun isTextUpdateRequired(
context: Context,
answer: LocalDateTime?,
inputText: String?
): Boolean {
val inputDate =
try {
generateLocalDateTime(parseDate(inputText, context), localTime)
} catch (e: Exception) {
null
}
if (answer == null || inputDate == null) return true
return answer.toLocalDate() != inputDate.toLocalDate()
}
}
}

Expand All @@ -301,3 +324,14 @@ internal val DateTimeType.localTime
minute,
second,
)

internal val DateTimeType.localDateTime
get() =
LocalDateTime.of(
year,
month + 1,
day,
hour,
minute,
second,
)
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
)
)

assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString()
)
.isEqualTo("")
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("")
}

@Test
Expand All @@ -86,10 +83,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString()
)
.isEqualTo("11/19/20")
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("11/19/20")
}

@Test
Expand All @@ -107,10 +101,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString()
)
.isEqualTo("2020/11/19")
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("2020/11/19")
}

@Test
Expand All @@ -128,10 +119,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString()
)
.isEqualTo("11/19/20")
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("11/19/20")
}

@Test
Expand All @@ -146,7 +134,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
)

viewHolder.bind(item)
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text = "11/19/2020"
viewHolder.dateInputView.text = "11/19/2020"

val answer = item.answers.singleOrNull()?.value as DateType

Expand All @@ -166,7 +154,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
answersChangedCallback = { _, _, _ -> },
)
viewHolder.bind(item)
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text = "2020/11/19"
viewHolder.dateInputView.text = "2020/11/19"
val answer = item.answers.singleOrNull()?.value as DateType

assertThat(answer.day).isEqualTo(19)
Expand All @@ -190,7 +178,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
)
viewHolder.bind(questionnaireItem)

viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text = "11/19/"
viewHolder.dateInputView.text = "11/19/"
val answer = questionnaireItem.answers.singleOrNull()?.value
assertThat(answer).isNull()
}
Expand Down Expand Up @@ -265,12 +253,59 @@ class QuestionnaireItemDatePickerViewHolderFactoryTest {
)
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).isEnabled)
.isFalse()
assertThat(viewHolder.dateInputView.isEnabled).isFalse()
}

@Test
fun `bind multiple times with different QuestionnaireItemViewItem should show proper date`() {
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
setLocale(Locale.US)

viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply { text = "Question?" },
QuestionnaireResponse.QuestionnaireResponseItemComponent()
.addAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent()
.setValue(DateType(2020, 10, 19))
),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("11/19/20")

viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply { text = "Question?" },
QuestionnaireResponse.QuestionnaireResponseItemComponent()
.addAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent()
.setValue(DateType(2021, 10, 19))
),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(viewHolder.dateInputView.text.toString()).isEqualTo("11/19/21")
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved

viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply { text = "Question?" },
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)
)
assertThat(viewHolder.dateInputView.text.toString()).isEmpty()
}

private fun setLocale(locale: Locale) {
Locale.setDefault(locale)
context.resources.configuration.setLocale(locale)
}

private val QuestionnaireItemViewHolder.dateInputView: TextView
get() {
return itemView.findViewById(R.id.text_input_edit_text)
}
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading