diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataPresenter.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataPresenter.kt index 2feff90014..cb65dff8a3 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataPresenter.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataPresenter.kt @@ -17,7 +17,6 @@ import org.dhis2.commons.bindings.event import org.dhis2.commons.bindings.program import org.dhis2.commons.data.EventCreationType import org.dhis2.commons.data.EventViewModel -import org.dhis2.commons.data.EventViewModelType import org.dhis2.commons.data.StageSection import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.filters.data.FilterRepository @@ -260,11 +259,8 @@ class TEIDataPresenter( valueStore, ) stagesToHide = stagesToHide1 - return events.filter { - when (it.type) { - EventViewModelType.STAGE -> !stagesToHide.contains(it.stage?.uid()) - EventViewModelType.EVENT -> !stagesToHide.contains(it.event?.programStage()) - } + return events.mapNotNull { + it.applyHideStage(stagesToHide.contains(it.stage?.uid())) } } diff --git a/app/src/test/java/org/dhis2/usescases/teiDashboard/dashboardfragments/data/TeiDataPresenterTest.kt b/app/src/test/java/org/dhis2/usescases/teiDashboard/dashboardfragments/data/TeiDataPresenterTest.kt index 772fe2f1bc..a0e3d67478 100644 --- a/app/src/test/java/org/dhis2/usescases/teiDashboard/dashboardfragments/data/TeiDataPresenterTest.kt +++ b/app/src/test/java/org/dhis2/usescases/teiDashboard/dashboardfragments/data/TeiDataPresenterTest.kt @@ -1,6 +1,8 @@ package org.dhis2.usescases.teiDashboard.dashboardfragments.data import io.reactivex.Single +import org.dhis2.commons.data.EventViewModel +import org.dhis2.commons.data.EventViewModelType import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.filters.data.FilterRepository import org.dhis2.commons.prefs.PreferenceProvider @@ -18,6 +20,7 @@ import org.dhis2.usescases.teiDashboard.domain.GetNewEventCreationTypeOptions import org.dhis2.utils.analytics.AnalyticsHelper import org.hisp.dhis.android.core.D2 import org.hisp.dhis.android.core.organisationunit.OrganisationUnit +import org.hisp.dhis.android.core.program.ProgramStage import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test @@ -27,6 +30,7 @@ import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.whenever +import java.util.Date class TeiDataPresenterTest { @@ -119,4 +123,49 @@ class TeiDataPresenterTest { whenever(teiDataRepository.getOrgUnitName(uid)) doReturn "OrgUnitDisplayName" assertTrue(teiDataPresenter.getOrgUnitName(uid) == "OrgUnitDisplayName") } + + @Test + fun `Should return null when apply effects of hide program stage with no events`() { + val stage = fakeModel() + assert(stage.applyHideStage(true) == null) + assert(stage.applyHideStage(false) == stage) + } + + @Test + fun `Should not be able to add event when apply effects of hide program stage with events`() { + val stage = fakeModel(3) + assert(stage.applyHideStage(true)?.canAddNewEvent == false) + } + + @Test + fun `Should not apply effects of hide program stage for events`() { + val stage = fakeModel(0, EventViewModelType.EVENT) + assert(stage.applyHideStage(true) == stage) + } + + private fun fakeModel(eventCount: Int = 0, type: EventViewModelType = EventViewModelType.STAGE): EventViewModel { + val dataElements = mutableListOf>() + dataElements.add( + Pair("Name", "Peter"), + ) + + return EventViewModel( + type = type, + stage = ProgramStage.builder().uid("stage").build(), + event = null, + eventCount = eventCount, + lastUpdate = Date(), + isSelected = false, + canAddNewEvent = true, + orgUnitName = "Org unit name", + catComboName = "Cat combo name", + dataElementValues = dataElements, + groupedByStage = null, + valueListIsOpen = false, + showTopShadow = false, + showBottomShadow = false, + displayDate = "21/11/2023", + nameCategoryOptionCombo = "Name Category option combo", + ) + } } diff --git a/commons/src/main/java/org/dhis2/commons/data/EventViewModel.kt b/commons/src/main/java/org/dhis2/commons/data/EventViewModel.kt index 8da67fbc08..d3643cc0e9 100644 --- a/commons/src/main/java/org/dhis2/commons/data/EventViewModel.kt +++ b/commons/src/main/java/org/dhis2/commons/data/EventViewModel.kt @@ -38,6 +38,16 @@ data class EventViewModel( return type == EventViewModelType.EVENT && event?.eventDate() != null && event.eventDate()?.after(today) == true } + + fun applyHideStage(hidden: Boolean): EventViewModel? { + return when { + type == EventViewModelType.STAGE && hidden -> when { + eventCount > 0 -> copy(canAddNewEvent = false) + else -> null + } + else -> this + } + } } fun List.uids(): List {