Skip to content

Commit

Permalink
feat: [ANDROAPP-5568] hide program stage behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmateos authored and andresmr committed Jan 23, 2024
1 parent eb60c96 commit 4df784c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 {

Expand Down Expand Up @@ -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<Pair<String, String>>()
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",
)
}
}
10 changes: 10 additions & 0 deletions commons/src/main/java/org/dhis2/commons/data/EventViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventViewModel>.uids(): List<String> {
Expand Down

0 comments on commit 4df784c

Please sign in to comment.