Skip to content

Commit

Permalink
Androapp 5724 remove organization unit in tei event ds card if user o…
Browse files Browse the repository at this point in the history
…nly has access to one org unit (#3459)

* add logic to show organisation unit

organisation unit in TEI list, DataSet and tracker program list will only be shown when user has access to more than one organisation unit.

* fix lint error

* move `displayOrgUnitName` logic to ui layer

---------

Co-authored-by: Siddharth Agarwal <[email protected]>
  • Loading branch information
siddh1004 and Siddharth Agarwal authored Jan 16, 2024
1 parent cd0925e commit a4c4c85
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 19 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,18 +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()

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

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

return EventViewModel(
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,10 +72,12 @@ class EventCardMapper(
)
}?.toMutableList() ?: mutableListOf()

checkRegisteredIn(
list = list,
orgUnit = event.orgUnitName,
)
if (displayOrgUnit) {
checkRegisteredIn(
list = list,
orgUnit = event.orgUnitName,
)
}

checkCategoryCombination(
list = list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ private SearchTeiModel transform(TrackedEntitySearchItem searchItem, @Nullable P
searchTei.setHeader(searchItem.getHeader());
searchTei.setSortingValue(sortingValueSetter.setSortingItem(searchTei, sortingItem));
searchTei.setTEType(searchItem.getType().displayName());
searchTei.setDisplayOrgUnit(displayOrgUnit());
return searchTei;
}

Expand Down Expand Up @@ -948,4 +949,10 @@ public boolean canCreateInProgramWithoutSearch() {
return programConfiguration != null && Boolean.TRUE.equals(programConfiguration.optionalSearch());
}
}

private boolean displayOrgUnit() {
return d2.organisationUnitModule().organisationUnits()
.byProgramUids(Collections.singletonList(currentProgram))
.blockingGet().size() > 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ class TEICardMapper(
attributeList.removeIf { it.value.isEmpty() || it.value == "-" }

return attributeList.also { list ->
checkEnrolledIn(
list = list,
enrolledOrgUnit = searchTEIModel.enrolledOrgUnit,
)
if (searchTEIModel.displayOrgUnit) {
checkEnrolledIn(
list = list,
enrolledOrgUnit = searchTEIModel.enrolledOrgUnit,
)
}

checkEnrolledPrograms(
list = list,
enrolledPrograms = searchTEIModel.programInfo,
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
10 changes: 10 additions & 0 deletions commons/src/main/java/org/dhis2/commons/data/SearchTeiModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SearchTeiModel implements CarouselItemModel {
private String sortingValue;
private String teTypeName;
private String enrolledOrgUnit;

private Boolean displayOrgUnit;
private boolean showNavigationButton = false;
@Nullable public String onlineErrorMessage;
@Nullable public D2ErrorCode onlineErrorCode;
Expand Down Expand Up @@ -245,6 +247,14 @@ public String getEnrolledOrgUnit() {
return enrolledOrgUnit;
}

public void setDisplayOrgUnit(Boolean display) {
displayOrgUnit = display;
}

public Boolean getDisplayOrgUnit() {
return displayOrgUnit;
}

public void setShowNavigationButton(boolean showNavigationButton) {
this.showNavigationButton = showNavigationButton;
}
Expand Down

0 comments on commit a4c4c85

Please sign in to comment.