From 7efc7bc933aecd980292c209b40b1940ce0da640 Mon Sep 17 00:00:00 2001 From: Pablo Pajuelo Cabezas Date: Thu, 12 Dec 2024 11:24:35 +0100 Subject: [PATCH] fix event repository --- .../org/dhis2/form/data/EventRepository.kt | 47 ++++--------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/form/src/main/java/org/dhis2/form/data/EventRepository.kt b/form/src/main/java/org/dhis2/form/data/EventRepository.kt index 0b17018504d..87948beb5f9 100644 --- a/form/src/main/java/org/dhis2/form/data/EventRepository.kt +++ b/form/src/main/java/org/dhis2/form/data/EventRepository.kt @@ -2,21 +2,18 @@ package org.dhis2.form.data import android.text.TextUtils import androidx.paging.PagingData -import androidx.paging.map import io.reactivex.Flowable import io.reactivex.Single import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.flow.map import org.dhis2.bindings.blockingGetValueCheck import org.dhis2.bindings.userFriendlyValue -import org.dhis2.commons.bindings.enrollment -import org.dhis2.commons.bindings.eventsBy import org.dhis2.commons.bindings.program import org.dhis2.commons.date.DateUtils import org.dhis2.commons.extensions.inDateRange import org.dhis2.commons.extensions.inOrgUnit import org.dhis2.commons.orgunitselector.OrgUnitSelectorScope +import org.dhis2.commons.periods.data.EventPeriodRepository import org.dhis2.commons.periods.domain.GetEventPeriods import org.dhis2.commons.periods.model.Period import org.dhis2.commons.resources.EventResourcesProvider @@ -51,7 +48,6 @@ import org.hisp.dhis.android.core.program.ProgramStageDataElement import org.hisp.dhis.android.core.program.ProgramStageSection import org.hisp.dhis.android.core.program.SectionRenderingType import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor -import java.util.Date class EventRepository( private val fieldFactory: FieldViewModelFactory, @@ -64,7 +60,9 @@ class EventRepository( private val eventMode: EventMode, ) : DataEntryBaseRepository(FormBaseConfiguration(d2), fieldFactory, metadataIconProvider) { - private val periodUseCase = GetEventPeriods(d2) + private val periodUseCase = GetEventPeriods( + EventPeriodRepository(d2), + ) private var event = d2.eventModule().events().uid(eventUid).blockingGet() @@ -446,20 +444,6 @@ class EventRepository( } } - private fun getUnavailableDates(): List { - val enrollment = event?.enrollment()?.let { d2.enrollment(it) } - return d2.eventsBy(enrollmentUid = enrollment?.uid()).mapNotNull { - if (it.programStage() == programStage?.uid() && - it.uid() != eventUid && - it.status() != EventStatus.SKIPPED - ) { - it.eventDate() - } else { - null - } - } - } - private fun createEventDetailsSection(): FieldUiModel { return fieldFactory.createSection( sectionUid = EVENT_DETAILS_SECTION_UID, @@ -491,33 +475,22 @@ class EventRepository( } override fun fetchPeriods(): Flow> { - val unavailableDates = getUnavailableDates() val periodType = programStage?.periodType() ?: PeriodType.Daily val stage = programStage ?: return flowOf() val eventEnrollmentUid = event?.enrollment() ?: return flowOf() return with(periodUseCase) { - fetchPeriods( + this.fetchPeriods( + eventUid = eventUid, periodType = periodType, selectedDate = if (eventMode == EventMode.SCHEDULE) { event?.dueDate() } else { event?.eventDate() }, - initialDate = getEventPeriodMinDate( - programStage = stage, - isScheduling = eventMode == EventMode.SCHEDULE, - eventEnrollmentUid = eventEnrollmentUid, - ), - maxDate = getEventPeriodMaxDate( - programStage = stage, - isScheduling = eventMode == EventMode.SCHEDULE, - eventEnrollmentUid = eventEnrollmentUid, - ), - ).map { paging -> - paging.map { period -> - period.copy(enabled = unavailableDates.contains(period.startDate).not()) - } - } + programStage = stage, + isScheduling = eventMode == EventMode.SCHEDULE, + eventEnrollmentUid = eventEnrollmentUid, + ) } }