From f101ceca38d2dede45dbe767891e263fce838ede Mon Sep 17 00:00:00 2001 From: Pablo Pajuelo Cabezas Date: Thu, 12 Dec 2024 12:55:40 +0100 Subject: [PATCH] missing refactor in SchedulingViewModel --- .../org/dhis2/data/server/ServerModule.kt | 9 ++++- .../dialogs/scheduling/SchedulingViewModel.kt | 38 +++++-------------- .../scheduling/SchedulingViewModelFactory.kt | 3 ++ .../scheduling/SchedulingViewModelTest.kt | 1 + .../commons/periods/domain/GetEventPeriods.kt | 2 +- 5 files changed, 22 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/dhis2/data/server/ServerModule.kt b/app/src/main/java/org/dhis2/data/server/ServerModule.kt index ea1a8417be1..99a36b7ef46 100644 --- a/app/src/main/java/org/dhis2/data/server/ServerModule.kt +++ b/app/src/main/java/org/dhis2/data/server/ServerModule.kt @@ -12,6 +12,7 @@ import org.dhis2.R import org.dhis2.bindings.app import org.dhis2.commons.di.dagger.PerServer import org.dhis2.commons.filters.data.GetFiltersApplyingWebAppConfig +import org.dhis2.commons.periods.data.EventPeriodRepository import org.dhis2.commons.periods.domain.GetEventPeriods import org.dhis2.commons.prefs.PreferenceProvider import org.dhis2.commons.reporting.CrashReportController @@ -175,7 +176,13 @@ class ServerModule { @Provides @PerServer - fun providePeriodUseCase(d2: D2) = GetEventPeriods(d2) + fun provideEventPeriodRepository(d2: D2): EventPeriodRepository = + EventPeriodRepository(d2) + + @Provides + @PerServer + fun providePeriodUseCase(eventPeriodRepository: EventPeriodRepository) = + GetEventPeriods(eventPeriodRepository) companion object { @JvmStatic diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModel.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModel.kt index 94cded480e7..d6792830dcf 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModel.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModel.kt @@ -3,13 +3,11 @@ package org.dhis2.usescases.teiDashboard.dialogs.scheduling import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData -import androidx.paging.map import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flowOn -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.dhis2.commons.bindings.enrollment @@ -50,10 +48,9 @@ class SchedulingViewModel( private val dispatchersProvider: DispatcherProvider, private val launchMode: LaunchMode, private val dateUtils: DateUtils, + private val getEventPeriods: GetEventPeriods, ) : ViewModel() { - private val periodUseCase = GetEventPeriods(d2) - lateinit var repository: EventDetailsRepository lateinit var configureEventReportDate: ConfigureEventReportDate lateinit var configureEventCatCombo: ConfigureEventCatCombo @@ -331,30 +328,13 @@ class SchedulingViewModel( val programStage = programStage.value ?: return emptyFlow() val periodType = programStage.periodType() ?: PeriodType.Daily val enrollmentUid = enrollment.value?.uid() ?: return emptyFlow() - return with(periodUseCase) { - val unavailableDates = getEventUnavailableDates( - programStage.uid(), - enrollmentUid, - null, - ) - fetchPeriods( - periodType = periodType, - selectedDate = eventDate.value.currentDate, - initialDate = getEventPeriodMinDate( - programStage = programStage, - isScheduling = true, - eventEnrollmentUid = enrollmentUid, - ), - maxDate = getEventPeriodMaxDate( - programStage = programStage, - isScheduling = true, - eventEnrollmentUid = enrollmentUid, - ), - ).map { paging -> - paging.map { period -> - period.copy(enabled = unavailableDates.contains(period.startDate).not()) - } - } - } + return getEventPeriods.fetchPeriods( + eventUid = null, + periodType = periodType, + selectedDate = eventDate.value.currentDate, + programStage = programStage, + isScheduling = true, + eventEnrollmentUid = enrollmentUid, + ) } } diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelFactory.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelFactory.kt index dbe9523c9a2..10dedca6ded 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelFactory.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelFactory.kt @@ -6,6 +6,7 @@ import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import org.dhis2.commons.date.DateUtils +import org.dhis2.commons.periods.domain.GetEventPeriods import org.dhis2.commons.resources.DhisPeriodUtils import org.dhis2.commons.resources.EventResourcesProvider import org.dhis2.commons.resources.ResourceManager @@ -20,6 +21,7 @@ class SchedulingViewModelFactory @AssistedInject constructor( private val periodUtils: DhisPeriodUtils, private val dateUtils: DateUtils, private val dispatcherProvider: DispatcherProvider, + private val getEventPeriods: GetEventPeriods, @Assisted private val launchMode: SchedulingDialog.LaunchMode, ) : ViewModelProvider.Factory { @@ -37,6 +39,7 @@ class SchedulingViewModelFactory @AssistedInject constructor( dateUtils = dateUtils, dispatchersProvider = dispatcherProvider, launchMode = launchMode, + getEventPeriods = getEventPeriods, ) as T } } diff --git a/app/src/test/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelTest.kt b/app/src/test/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelTest.kt index 4277dbba697..215a4aa8db3 100644 --- a/app/src/test/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelTest.kt +++ b/app/src/test/java/org/dhis2/usescases/teiDashboard/dialogs/scheduling/SchedulingViewModelTest.kt @@ -96,6 +96,7 @@ class SchedulingViewModelTest { showYesNoOptions = false, eventCreationType = EventCreationType.SCHEDULE, ), + getEventPeriods = mock(), ) } diff --git a/commons/src/main/java/org/dhis2/commons/periods/domain/GetEventPeriods.kt b/commons/src/main/java/org/dhis2/commons/periods/domain/GetEventPeriods.kt index 0d898467b7c..f75fd0caef5 100644 --- a/commons/src/main/java/org/dhis2/commons/periods/domain/GetEventPeriods.kt +++ b/commons/src/main/java/org/dhis2/commons/periods/domain/GetEventPeriods.kt @@ -19,7 +19,7 @@ class GetEventPeriods( private val periodLabelProvider: PeriodLabelProvider = PeriodLabelProvider(), ) { fun fetchPeriods( - eventUid: String, + eventUid: String?, periodType: PeriodType, selectedDate: Date?, programStage: ProgramStage,