Skip to content

Commit

Permalink
move displayOrgUnitName logic to ui layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Agarwal committed Jan 10, 2024
1 parent 470d5ce commit 533400b
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class ProgramEventDetailLiveAdapter(
event = it,
editable = it.event?.uid()
?.let { eventViewModel.isEditable(it) } ?: true,
displayOrgUnit = it.event?.program()
?.let { program -> eventViewModel.displayOrganisationUnit(program) } ?: true,
onSyncIconClick = {
eventViewModel.eventSyncClicked.value = it.event?.uid()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ interface ProgramEventDetailRepository {
fun programHasCoordinates(): Boolean
fun programHasAnalytics(): Boolean
fun isEventEditable(eventUid: String): Boolean
fun displayOrganisationUnit(programUid: String): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@ class ProgramEventDetailRepositoryImpl internal constructor(
override fun isEventEditable(eventUid: String): Boolean {
return d2.eventModule().eventService().blockingIsEditable(eventUid)
}

override fun displayOrganisationUnit(programUid: String): Boolean {
return d2.organisationUnitModule().organisationUnits()
.byProgramUids(listOf(programUid))
.blockingGet().size > 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class ProgramEventDetailViewModel(
val eventClicked = MutableLiveData<Pair<String, String>?>(null)
var updateEvent: String? = null
var recreationActivity: Boolean = false

enum class EventProgramScreen {
LIST, MAP, ANALYTICS
}

private val _currentScreen = MutableLiveData(EventProgramScreen.LIST)
val currentScreen: LiveData<EventProgramScreen>
get() = _currentScreen.distinctUntilChanged()
Expand Down Expand Up @@ -60,4 +62,8 @@ class ProgramEventDetailViewModel(
fun isEditable(eventUid: String): Boolean {
return eventRepository.isEventEditable(eventUid)
}

fun displayOrganisationUnit(programUid: String): Boolean {
return eventRepository.displayOrganisationUnit(programUid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.dhis2.commons.data.EventViewModel
import org.dhis2.commons.data.EventViewModelType
import org.dhis2.commons.data.ProgramEventViewModel
import org.dhis2.commons.data.tuples.Pair
import org.dhis2.commons.reporting.CrashReportController
import org.dhis2.data.dhislogic.DhisPeriodUtils
import org.dhis2.utils.DateUtils
import org.hisp.dhis.android.core.D2
Expand All @@ -25,22 +24,12 @@ import javax.inject.Inject
class ProgramEventMapper @Inject constructor(
val d2: D2,
val periodUtils: DhisPeriodUtils,
val crashReportController: CrashReportController,
) {

fun eventToEventViewModel(event: Event): EventViewModel {
val programStage =
d2.programModule().programStages().uid(event.programStage()).blockingGet()

val dataSetOrgUnitNumber = d2.organisationUnitModule().organisationUnits()
.byProgramUids(listOf(event.program()!!))
.blockingGet().size

crashReportController.addBreadCrumb(
"ProgramEventMapper.eventToEventViewModel",
"Event: $event",
)

val eventDate = event.eventDate() ?: event.dueDate()

return EventViewModel(
Expand All @@ -66,7 +55,6 @@ class ProgramEventMapper @Inject constructor(
},
nameCategoryOptionCombo =
getCategoryComboFromOptionCombo(event.attributeOptionCombo())?.displayName(),
displayOrgUnitName = dataSetOrgUnitNumber > 1,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class EventCardMapper(
fun map(
event: EventViewModel,
editable: Boolean,
displayOrgUnit: Boolean,
onSyncIconClick: () -> Unit,
onCardClick: () -> Unit,
): ListCardUiModel {
return ListCardUiModel(
title = event.displayDate ?: "",
lastUpdated = event.lastUpdate.toDateSpan(context),
additionalInfo = getAdditionalInfoList(event, editable),
additionalInfo = getAdditionalInfoList(event, editable, displayOrgUnit),
actionButton = {
ProvideSyncButton(
state = event.event?.aggregatedSyncState(),
Expand All @@ -60,6 +61,7 @@ class EventCardMapper(
private fun getAdditionalInfoList(
event: EventViewModel,
editable: Boolean,
displayOrgUnit: Boolean,
): List<AdditionalInfoItem> {
val list = event.dataElementValues?.filter {
!it.second.isNullOrEmpty()
Expand All @@ -70,7 +72,7 @@ class EventCardMapper(
)
}?.toMutableList() ?: mutableListOf()

if (event.displayOrgUnitName) {
if (displayOrgUnit) {
checkRegisteredIn(
list = list,
orgUnit = event.orgUnitName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,7 @@ public List<EventViewModel> getEventsForMap(List<SearchTeiModel> teis) {
false,
false,
periodUtils.getPeriodUIString(stage.periodType(), event.eventDate() != null ? event.eventDate() : event.dueDate(), Locale.getDefault()),
null,
true
null
));
}

Expand Down Expand Up @@ -704,8 +703,7 @@ public EventViewModel getEventInfo(String uid) {
false,
false,
periodUtils.getPeriodUIString(stage.periodType(), event.eventDate() != null ? event.eventDate() : event.dueDate(), Locale.getDefault()),
null,
true
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class TeiDataRepositoryImpl(
dataElementValues = null,
displayDate = null,
nameCategoryOptionCombo = null,
displayOrgUnitName = true,
)
}
}
Expand Down Expand Up @@ -185,7 +184,6 @@ class TeiDataRepositoryImpl(
groupedByStage = true,
displayDate = null,
nameCategoryOptionCombo = null,
displayOrgUnitName = true,
),
)
if (isSelected) {
Expand Down Expand Up @@ -219,7 +217,6 @@ class TeiDataRepositoryImpl(
),
nameCategoryOptionCombo =
getCategoryComboFromOptionCombo(event.attributeOptionCombo())?.displayName(),
displayOrgUnitName = true,
),
)
}
Expand Down Expand Up @@ -265,7 +262,6 @@ class TeiDataRepositoryImpl(
),
nameCategoryOptionCombo =
getCategoryComboFromOptionCombo(event.attributeOptionCombo())?.displayName(),
displayOrgUnitName = true,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.dhis2.usescases.programEventDetail

import org.dhis2.commons.reporting.CrashReportController
import org.dhis2.data.dhislogic.DhisPeriodUtils
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.category.CategoryOptionCombo
Expand All @@ -25,11 +24,10 @@ class ProgramEventMapperTest {

private val d2: D2 = Mockito.mock(D2::class.java, RETURNS_DEEP_STUBS)
private val periodUtil: DhisPeriodUtils = mock()
private val crashReportController: CrashReportController = mock()

@Before
fun setUp() {
mapper = ProgramEventMapper(d2, periodUtil, crashReportController)
mapper = ProgramEventMapper(d2, periodUtil)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class EventCardMapperTest {
val result = mapper.map(
event = model,
editable = true,
displayOrgUnit = true,
onSyncIconClick = {},
onCardClick = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class TEICardMapperTest {
.aggregatedSyncState(State.SYNCED)
.build()
enrolledOrgUnit = "OrgUnit"
displayOrgUnit = true
setCurrentEnrollment(
Enrollment.builder()
.uid("EnrollmentUid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ data class EventViewModel(
val showBottomShadow: Boolean = false,
val displayDate: String?,
val nameCategoryOptionCombo: String?,
val displayOrgUnitName: Boolean,
) {
fun toggleValueList() {
this.valueListIsOpen = !valueListIsOpen
Expand Down

0 comments on commit 533400b

Please sign in to comment.