Skip to content

Commit

Permalink
fix event repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Dec 12, 2024
1 parent aa31204 commit 7efc7bc
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions form/src/main/java/org/dhis2/form/data/EventRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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()

Expand Down Expand Up @@ -446,20 +444,6 @@ class EventRepository(
}
}

private fun getUnavailableDates(): List<Date> {
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,
Expand Down Expand Up @@ -491,33 +475,22 @@ class EventRepository(
}

override fun fetchPeriods(): Flow<PagingData<Period>> {
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,
)
}
}

Expand Down

0 comments on commit 7efc7bc

Please sign in to comment.