Skip to content

Commit

Permalink
Merge pull request #3919 from dhis2/optionset_fix
Browse files Browse the repository at this point in the history
fix: [ANDROAPP-6653] options not hiding/showing with program rules
  • Loading branch information
xavimolloy authored Dec 4, 2024
2 parents 3a43b54 + ba3b8cd commit 5786295
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
18 changes: 16 additions & 2 deletions form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FormRepositoryImpl(
private var runDataIntegrity: Boolean = false
private var calculationLoop: Int = 0
private var backupList: List<FieldUiModel> = emptyList()
private val fieldsWithOptionEffects = mutableListOf<FieldUiModel>()

private val disableCollapsableSections: Boolean? =
dataEntryRepository.disableCollapsableSections()
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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().any { optionGroup ->
optionGroup.options()?.map { it.uid() }?.contains(option.uid()) == true
}

val optionInGroupToShow = d2.optionModule().optionGroups()
.withOptions()
.byUid().`in`(optionGroupsToShow)
.blockingGet().any { optionGroup ->
optionGroup.options()?.map { it.uid() }?.contains(option.uid()) == true
}

val hideOption = if (optionGroupsToShow.isEmpty()) {
optionsToHide.contains(option.uid()) || optionInGroupToHide
} else {
!optionInGroupToShow
}

!hideOption
}
}
}
Expand Down
40 changes: 38 additions & 2 deletions form/src/test/java/org/dhis2/form/data/FormRepositoryImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package org.dhis2.form.data

import androidx.databinding.ObservableField
import io.reactivex.Flowable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.emptyFlow
import org.dhis2.commons.prefs.PreferenceProvider
import org.dhis2.form.model.ActionType
import org.dhis2.form.model.EventCategory
import org.dhis2.form.model.FieldUiModel
import org.dhis2.form.model.FieldUiModelImpl
import org.dhis2.form.model.OptionSetConfiguration
import org.dhis2.form.model.RowAction
import org.dhis2.form.model.SectionUiModelImpl
import org.dhis2.form.model.StoreResult
Expand All @@ -30,6 +33,7 @@ import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.atLeast
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doReturnConsecutively
import org.mockito.kotlin.mock
Expand Down Expand Up @@ -200,6 +204,14 @@ class FormRepositoryImplTest {
mutableMapOf(Pair("field", "uid001")),
),
),
RuleEffect(
"rule2",
RuleAction(
"option1",
ProgramRuleActionType.HIDEOPTION.name,
mutableMapOf(Pair("field", "uid004")),
),
),
)

whenever(dataEntryRepository.isEvent()) doReturn true
Expand All @@ -215,12 +227,21 @@ class FormRepositoryImplTest {
fieldsToUpdate = listOf(FieldWithNewValue("uid001", "newValue")),
configurationErrors = emptyList(),
stagesToHide = emptyList(),
optionsToHide = emptyMap(),
optionsToHide = mapOf(
"uid004" to listOf("option1"),
),
optionGroupsToHide = emptyMap(),
optionGroupsToShow = emptyMap(),
)

verify(rulesUtilsProvider, times(1)).applyRuleEffects(
whenever(dataEntryRepository.options(any(), any(), any(), any()))doReturn Pair(
MutableStateFlow(""),
emptyFlow(),
)

repository.composeList()

verify(rulesUtilsProvider, atLeast(1)).applyRuleEffects(
any(),
any(),
any(),
Expand Down Expand Up @@ -441,6 +462,21 @@ class FormRepositoryImplTest {
optionSetConfiguration = null,
autocompleteList = null,
),
FieldUiModelImpl(
uid = "uid004",
value = null,
label = "field4",
valueType = ValueType.TEXT,
programStageSection = "section1",
uiEventFactory = null,
optionSet = "optionSetUid",
optionSetConfiguration = OptionSetConfiguration(
MutableStateFlow(""),
{},
emptyFlow(),
),
autocompleteList = null,
),
)

private fun section1() = SectionUiModelImpl(
Expand Down

0 comments on commit 5786295

Please sign in to comment.