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

Implement inequality operators (<, >, <=, >=) in the enable when statement #848

Merged
merged 20 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support inequality operators > < >= <=
  • Loading branch information
santosh-pingle committed Oct 21, 2021
commit bd9477ddb39d923e2bc34ec68e7ee51f72349cfb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

package com.google.android.fhir.datacapture

import com.google.android.fhir.datacapture.utilities.UcumValue
import com.google.android.fhir.datacapture.utilities.UnitConverter
import com.google.android.fhir.datacapture.validation.compareTo
import org.hl7.fhir.r4.model.BooleanType
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Quantity
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Type

/**
Expand All @@ -38,3 +44,83 @@ internal fun equals(a: Type, b: Type): Boolean {

throw NotImplementedError("Comparison for type ${a::class.java} not supported.")
}

/** True if whether at least no answer has a value that is greater than the enableWhen answer. */
internal fun Questionnaire.QuestionnaireItemEnableWhenComponent.greaterThan(
santosh-pingle marked this conversation as resolved.
Show resolved Hide resolved
questionAnswer: Type
): Boolean {
if (answer::class != questionAnswer::class || answer is BooleanType) return false
if (answer === questionAnswer) return true
if (answer.isPrimitive) return questionAnswer <= answer
if (answer is Quantity && questionAnswer is Quantity) {
val answerUcumUnit =
UnitConverter.getCanonicalForm(
UcumValue((answer as Quantity).code, (answer as Quantity).value)
)
val questionAnswerUcumUnit =
UnitConverter.getCanonicalForm(UcumValue(questionAnswer.code, questionAnswer.value))
return questionAnswerUcumUnit.value < answerUcumUnit.value
}
return false
}

/**
* True if whether at least no answer has a value that is greater or equal to the enableWhen answer.
*/
internal fun Questionnaire.QuestionnaireItemEnableWhenComponent.greaterOrEqual(
questionAnswer: Type
): Boolean {
if (answer::class != questionAnswer::class || answer === questionAnswer || answer is BooleanType)
return false
if (answer.isPrimitive) return questionAnswer < answer
if (answer is Quantity && questionAnswer is Quantity) {
val answerUcumUnit =
UnitConverter.getCanonicalForm(
UcumValue((answer as Quantity).code, (answer as Quantity).value)
)
val questionAnswerUcumUnit =
UnitConverter.getCanonicalForm(UcumValue(questionAnswer.code, questionAnswer.value))
return questionAnswerUcumUnit.value < answerUcumUnit.value
}
return false
}

/** True if whether at least no answer has a value that is less than the enableWhen answer. */
internal fun Questionnaire.QuestionnaireItemEnableWhenComponent.lessThan(
questionAnswer: Type
): Boolean {
if (answer::class != questionAnswer::class || answer is BooleanType) return false
if (answer === questionAnswer) return true
if (answer.isPrimitive) return questionAnswer >= answer
if (answer is Quantity && questionAnswer is Quantity) {
val answerUcumUnit =
UnitConverter.getCanonicalForm(
UcumValue((answer as Quantity).code, (answer as Quantity).value)
)
val questionAnswerUcumUnit =
UnitConverter.getCanonicalForm(UcumValue(questionAnswer.code, questionAnswer.value))
return questionAnswerUcumUnit.value >= answerUcumUnit.value
}
return false
}

/**
* True if whether at least no answer has a value that is less or equal to the enableWhen answer.
*/
internal fun Questionnaire.QuestionnaireItemEnableWhenComponent.lessOrEqual(
questionAnswer: Type
): Boolean {
if (answer::class != questionAnswer::class || answer === questionAnswer || answer is BooleanType)
return false
if (answer.isPrimitive) return questionAnswer > answer
if (answer is Quantity && questionAnswer is Quantity) {
val answerUcumUnit =
UnitConverter.getCanonicalForm(
UcumValue((answer as Quantity).code, (answer as Quantity).value)
)
val questionAnswerUcumUnit =
UnitConverter.getCanonicalForm(UcumValue(questionAnswer.code, questionAnswer.value))
return questionAnswerUcumUnit.value > answerUcumUnit.value
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.google.android.fhir.datacapture.enablement

import com.google.android.fhir.datacapture.equals
import java.lang.IllegalStateException
import com.google.android.fhir.datacapture.greaterOrEqual
import com.google.android.fhir.datacapture.greaterThan
import com.google.android.fhir.datacapture.lessOrEqual
import com.google.android.fhir.datacapture.lessThan
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse

Expand Down Expand Up @@ -127,6 +130,18 @@ private val Questionnaire.QuestionnaireItemEnableWhenComponent.predicate:
Questionnaire.QuestionnaireItemOperator.NOT_EQUAL -> {
!equals(it.value, answer)
}
Questionnaire.QuestionnaireItemOperator.GREATER_OR_EQUAL -> {
greaterOrEqual(it.value)
}
Questionnaire.QuestionnaireItemOperator.LESS_OR_EQUAL -> {
lessOrEqual(it.value)
}
Questionnaire.QuestionnaireItemOperator.GREATER_THAN -> {
greaterThan(it.value)
}
Questionnaire.QuestionnaireItemOperator.LESS_THAN -> {
lessThan(it.value)
}
else -> throw NotImplementedError("Enable when operator $operator is not implemented.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,10 @@ internal object QuestionnaireItemAutoCompleteViewHolderFactory :
)
}
}

private fun setViewReadOnly(view: View) {
view.isEnabled = false
view.isFocusable = false
}
}
}
Loading