Skip to content

Commit

Permalink
missing refactor in SchedulingViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Dec 12, 2024
1 parent 7efc7bc commit f101cec
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/org/dhis2/data/server/ServerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand All @@ -37,6 +39,7 @@ class SchedulingViewModelFactory @AssistedInject constructor(
dateUtils = dateUtils,
dispatchersProvider = dispatcherProvider,
launchMode = launchMode,
getEventPeriods = getEventPeriods,
) as T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class SchedulingViewModelTest {
showYesNoOptions = false,
eventCreationType = EventCreationType.SCHEDULE,
),
getEventPeriods = mock(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GetEventPeriods(
private val periodLabelProvider: PeriodLabelProvider = PeriodLabelProvider(),
) {
fun fetchPeriods(
eventUid: String,
eventUid: String?,
periodType: PeriodType,
selectedDate: Date?,
programStage: ProgramStage,
Expand Down

0 comments on commit f101cec

Please sign in to comment.