diff --git a/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt b/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt index ac791f613d..0895fd5083 100644 --- a/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt +++ b/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt @@ -48,6 +48,7 @@ class FormRepositoryImpl( private var runDataIntegrity: Boolean = false private var calculationLoop: Int = 0 private var backupList: List = emptyList() + private val fieldsWithOptionEffects = mutableListOf() private val disableCollapsableSections: Boolean? = dataEntryRepository.disableCollapsableSections() @@ -522,9 +523,22 @@ class FormRepositoryImpl( } } + fieldsWithOptionEffects.forEach { field -> + field.optionSet?.let { optionSetUid -> + fetchOptions(field.uid, optionSetUid) + } + } + + fieldsWithOptionEffects.clear() + ruleEffectsResult?.fieldsWithOptionEffects()?.forEach { fieldWithOptionEffect -> - itemList.find { it.uid == fieldWithOptionEffect }?.let { - it.optionSet?.let { optionSetUid -> fetchOptions(it.uid, optionSetUid) } + val item = itemList.find { it.uid == fieldWithOptionEffect } + + item?.let { field -> + field.optionSet?.let { optionSetUid -> + fetchOptions(field.uid, optionSetUid) + } + fieldsWithOptionEffects.add(field) } } diff --git a/form/src/main/java/org/dhis2/form/data/metadata/FormBaseConfiguration.kt b/form/src/main/java/org/dhis2/form/data/metadata/FormBaseConfiguration.kt index 511e379d38..cacc2cc5ea 100644 --- a/form/src/main/java/org/dhis2/form/data/metadata/FormBaseConfiguration.kt +++ b/form/src/main/java/org/dhis2/form/data/metadata/FormBaseConfiguration.kt @@ -41,12 +41,28 @@ open class FormBaseConfiguration(private val d2: D2) { .getPagingData(10) }.map { pagingData -> pagingData.filter { option -> - !optionsToHide.contains(option.uid()) && - !optionGroupsToHide.contains(option.uid()) && - ( - optionGroupsToShow.isEmpty() || - optionGroupsToShow.contains(option.uid()) - ) + + val optionInGroupToHide = d2.optionModule().optionGroups() + .withOptions() + .byUid().`in`(optionGroupsToHide) + .blockingGet().find { optionGroup -> + optionGroup.options()?.map { it.uid() }?.contains(option.uid()) == true + } != null + + val optionInGroupToShow = d2.optionModule().optionGroups() + .withOptions() + .byUid().`in`(optionGroupsToShow) + .blockingGet().find { optionGroup -> + optionGroup.options()?.map { it.uid() }?.contains(option.uid()) == true + } != null + + val hideOption = if (optionGroupsToShow.isEmpty()) { + optionsToHide.contains(option.uid()) || optionInGroupToHide + } else { + !optionInGroupToShow + } + + !hideOption } } }