Skip to content

Commit

Permalink
add invoke to GetEventPeriods
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Dec 13, 2024
1 parent 687cbdb commit 2e28971
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScheduledEventPresenterImpl(
) : ScheduledEventContract.Presenter {

private lateinit var disposable: CompositeDisposable
private val periodUseCase = GetEventPeriods(EventPeriodRepository(d2))
private val getEventPeriods = GetEventPeriods(EventPeriodRepository(d2))

override fun init() {
disposable = CompositeDisposable()
Expand Down Expand Up @@ -130,23 +130,21 @@ class ScheduledEventPresenterImpl(
}

override fun fetchPeriods(scheduling: Boolean): Flow<PagingData<Period>> {
return with(periodUseCase) {
val event = d2.event(eventUid) ?: return emptyFlow()
val stage = event.programStage()?.let { d2.programStage(it) } ?: return emptyFlow()

fetchPeriods(
eventUid = eventUid,
periodType = stage.periodType() ?: PeriodType.Daily,
selectedDate = if (scheduling) {
event.dueDate()
} else {
event.eventDate()
},
programStage = stage,
isScheduling = scheduling,
eventEnrollmentUid = event.enrollment(),
)
}
val event = d2.event(eventUid) ?: return emptyFlow()
val stage = event.programStage()?.let { d2.programStage(it) } ?: return emptyFlow()

return getEventPeriods(
eventUid = eventUid,
periodType = stage.periodType() ?: PeriodType.Daily,
selectedDate = if (scheduling) {
event.dueDate()
} else {
event.eventDate()
},
programStage = stage,
isScheduling = scheduling,
eventEnrollmentUid = event.enrollment(),
)
}

override fun setDueDate(date: Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ import org.hisp.dhis.android.core.period.PeriodType

class ConfigurePeriodSelector(
private val eventDetailRepository: EventDetailsRepository,
private val periodUseCase: GetEventPeriods,
private val getEventPeriods: GetEventPeriods,
) {
operator fun invoke(): Flow<PagingData<Period>> {
val programStage = eventDetailRepository.getProgramStage() ?: return emptyFlow()
val event = eventDetailRepository.getEvent() ?: return emptyFlow()
val periodType = programStage.periodType() ?: PeriodType.Daily
return with(periodUseCase) {
fetchPeriods(
eventUid = event.uid(),
periodType = periodType,
selectedDate = if (eventDetailRepository.isScheduling()) {
event.dueDate()
} else {
event.eventDate()
},
programStage = programStage,
isScheduling = eventDetailRepository.isScheduling(),
eventEnrollmentUid = event.enrollment(),
)
}
return getEventPeriods(
eventUid = event.uid(),
periodType = periodType,
selectedDate = if (eventDetailRepository.isScheduling()) {
event.dueDate()
} else {
event.eventDate()
},
programStage = programStage,
isScheduling = eventDetailRepository.isScheduling(),
eventEnrollmentUid = event.enrollment(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class EventDetailsModule(
): ConfigurePeriodSelector {
return ConfigurePeriodSelector(
eventDetailRepository = eventDetailsRepository,
periodUseCase = periodUseCase,
getEventPeriods = periodUseCase,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class SchedulingViewModel(
val programStage = programStage.value ?: return emptyFlow()
val periodType = programStage.periodType() ?: PeriodType.Daily
val enrollmentUid = enrollment.value?.uid() ?: return emptyFlow()
return getEventPeriods.fetchPeriods(
return getEventPeriods(
eventUid = null,
periodType = periodType,
selectedDate = eventDate.value.currentDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GetEventPeriods(
private val eventPeriodRepository: EventPeriodRepository,
private val periodLabelProvider: PeriodLabelProvider = PeriodLabelProvider(),
) {
fun fetchPeriods(
operator fun invoke(
eventUid: String?,
periodType: PeriodType,
selectedDate: Date?,
Expand Down
28 changes: 13 additions & 15 deletions form/src/main/java/org/dhis2/form/data/EventRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class EventRepository(
private val eventMode: EventMode,
) : DataEntryBaseRepository(FormBaseConfiguration(d2), fieldFactory, metadataIconProvider) {

private val periodUseCase = GetEventPeriods(
private val getEventPeriods = GetEventPeriods(
EventPeriodRepository(d2),
)

Expand Down Expand Up @@ -478,20 +478,18 @@ class EventRepository(
val periodType = programStage?.periodType() ?: PeriodType.Daily
val stage = programStage ?: return flowOf()
val eventEnrollmentUid = event?.enrollment() ?: return flowOf()
return with(periodUseCase) {
this.fetchPeriods(
eventUid = eventUid,
periodType = periodType,
selectedDate = if (eventMode == EventMode.SCHEDULE) {
event?.dueDate()
} else {
event?.eventDate()
},
programStage = stage,
isScheduling = eventMode == EventMode.SCHEDULE,
eventEnrollmentUid = eventEnrollmentUid,
)
}
return getEventPeriods(
eventUid = eventUid,
periodType = periodType,
selectedDate = if (eventMode == EventMode.SCHEDULE) {
event?.dueDate()
} else {
event?.eventDate()
},
programStage = stage,
isScheduling = eventMode == EventMode.SCHEDULE,
eventEnrollmentUid = eventEnrollmentUid,
)
}

private fun getFieldsForSingleSection(): Single<List<FieldUiModel>> {
Expand Down

0 comments on commit 2e28971

Please sign in to comment.