Skip to content

Commit

Permalink
feat: [ANDROAPP-5794] sonar fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy committed Feb 23, 2024
1 parent 31ae15a commit afd699e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventDa
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventInputDateUiModel
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvideInputDate
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvidePeriodSelector
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.willShowCalendar
import org.dhis2.usescases.eventsWithoutRegistration.eventInitial.EventInitialActivity
import org.dhis2.usescases.general.ActivityGlobalAbstract
import org.dhis2.utils.DateUtils
Expand Down Expand Up @@ -109,7 +110,7 @@ class ScheduledEventActivity : ActivityGlobalAbstract(), ScheduledEventContract.
dateValue = DateUtils.uiDateFormat().format(event.dueDate() ?: ""),
)

if (programStage.periodType() == null || (programStage.periodType() != null && programStage.periodType() == PeriodType.Daily)) {
if (willShowCalendar(programStage.periodType())) {
ProvideInputDate(
EventInputDateUiModel(
eventDate = eventDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.hisp.dhis.android.core.arch.helpers.Result
import org.hisp.dhis.android.core.common.FeatureType
import org.hisp.dhis.android.core.common.Geometry
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.android.core.period.PeriodType
import org.hisp.dhis.mobile.ui.designsystem.component.Coordinates
import org.hisp.dhis.mobile.ui.designsystem.component.DateTimeActionType
import org.hisp.dhis.mobile.ui.designsystem.component.DropdownInputField
Expand Down Expand Up @@ -439,6 +440,10 @@ fun ProvideRadioButtons(
}
}

fun willShowCalendar(periodType: PeriodType?): Boolean {
return (periodType == null || periodType == PeriodType.Daily)
}

const val INPUT_EVENT_INITIAL_DATE = "INPUT_EVENT_INITIAL_DATE"
const val EMPTY_CATEGORY_SELECTOR = "EMPTY_CATEGORY_SELECTOR"
const val CATEGORY_SELECTOR = "CATEGORY_SELECTOR"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import org.dhis2.R
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventCatCombo
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventCatComboUiModel
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventDate
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventInputDateUiModel
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvideCategorySelector
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvideInputDate
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.ProvidePeriodSelector
import org.hisp.dhis.android.core.period.PeriodType
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.willShowCalendar
import org.hisp.dhis.android.core.program.ProgramStage
import org.hisp.dhis.mobile.ui.designsystem.component.BottomSheetShell
import org.hisp.dhis.mobile.ui.designsystem.component.Button
Expand Down Expand Up @@ -91,67 +93,14 @@ fun SchedulingDialogUi(
},
)
if (scheduleNew) {
if (programStages.size > 1) {
InputDropDown(
title = stringResource(id = R.string.program_stage),
state = InputShellState.UNFOCUSED,
dropdownItems = programStages.map { DropdownItem(it.displayName().orEmpty()) },
selectedItem = DropdownItem(selectedProgramStage.displayName().orEmpty()),
onResetButtonClicked = {},
onItemSelected = { item ->
programStages.find { it.displayName() == item.label }
?.let { viewModel.updateStage(it) }
},
)
}

if (selectedProgramStage.periodType() == null || (selectedProgramStage.periodType() != null && selectedProgramStage.periodType() == PeriodType.Daily)) {
ProvideInputDate(
EventInputDateUiModel(
eventDate = date,
detailsEnabled = true,
onDateClick = {},
onDateSelected = { viewModel.onDateSet(it.year, it.month, it.day) },
onClear = { viewModel.onClearEventReportDate() },
),
)
} else {
ProvidePeriodSelector(
uiModel = EventInputDateUiModel(
eventDate = date,
detailsEnabled = true,
onDateClick = { viewModel.showPeriodDialog() },
onDateSelected = {},
onClear = { viewModel.onClearEventReportDate() },
required = true,
showField = date.active,
selectableDates = viewModel.getSelectableDates(),
),
modifier = Modifier,
)
}

if (!catCombo.isDefault) {
catCombo.categories.forEach { category ->
ProvideCategorySelector(
eventCatComboUiModel = EventCatComboUiModel(
category = category,
eventCatCombo = catCombo,
detailsEnabled = true,
currentDate = date.currentDate,
selectedOrgUnit = orgUnitUid,
onClearCatCombo = { viewModel.onClearCatCombo() },
onOptionSelected = {
val selectedOption = Pair(category.uid, it?.uid())
viewModel.setUpCategoryCombo(selectedOption)
},
required = true,
noOptionsText = stringResource(R.string.no_options),
catComboText = stringResource(R.string.cat_combo),
),
)
}
}
ProvideScheduleNewEventForm(
programStages = programStages,
viewModel = viewModel,
selectedProgramStage = selectedProgramStage,
date = date,
catCombo = catCombo,
orgUnitUid = orgUnitUid,
)
}
}
},
Expand All @@ -172,3 +121,75 @@ fun buttonTitle(scheduleNew: Boolean): String = when (scheduleNew) {
true -> stringResource(id = R.string.schedule)
false -> stringResource(id = R.string.done)
}

@Composable
fun ProvideScheduleNewEventForm(
programStages: List<ProgramStage>,
viewModel: SchedulingViewModel,
selectedProgramStage: ProgramStage,
date: EventDate,
catCombo: EventCatCombo,
orgUnitUid: String?,
) {
if (programStages.size > 1) {
InputDropDown(
title = stringResource(id = R.string.program_stage),
state = InputShellState.UNFOCUSED,
dropdownItems = programStages.map { DropdownItem(it.displayName().orEmpty()) },
selectedItem = DropdownItem(selectedProgramStage.displayName().orEmpty()),
onResetButtonClicked = {},
onItemSelected = { item ->
programStages.find { it.displayName() == item.label }
?.let { viewModel.updateStage(it) }
},
)
}

if (willShowCalendar(selectedProgramStage.periodType())) {
ProvideInputDate(
EventInputDateUiModel(
eventDate = date,
detailsEnabled = true,
onDateClick = {},
onDateSelected = { viewModel.onDateSet(it.year, it.month, it.day) },
onClear = { viewModel.onClearEventReportDate() },
),
)
} else {
ProvidePeriodSelector(
uiModel = EventInputDateUiModel(
eventDate = date,
detailsEnabled = true,
onDateClick = { viewModel.showPeriodDialog() },
onDateSelected = {},
onClear = { viewModel.onClearEventReportDate() },
required = true,
showField = date.active,
selectableDates = viewModel.getSelectableDates(),
),
modifier = Modifier,
)
}

if (!catCombo.isDefault) {
catCombo.categories.forEach { category ->
ProvideCategorySelector(
eventCatComboUiModel = EventCatComboUiModel(
category = category,
eventCatCombo = catCombo,
detailsEnabled = true,
currentDate = date.currentDate,
selectedOrgUnit = orgUnitUid,
onClearCatCombo = { viewModel.onClearCatCombo() },
onOptionSelected = {
val selectedOption = Pair(category.uid, it?.uid())
viewModel.setUpCategoryCombo(selectedOption)
},
required = true,
noOptionsText = stringResource(R.string.no_options),
catComboText = stringResource(R.string.cat_combo),
),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ fun ProvideInputDate(
},
)
}
val yearIntRange = if (fieldUiModel.allowFutureDates == true) IntRange(1924, 2124) else (IntRange(1924, Calendar.getInstance().get(Calendar.YEAR)))
val yearIntRange = if (fieldUiModel.allowFutureDates == true) IntRange(1924, 2124) else (IntRange(1924,
Calendar.getInstance()[Calendar.YEAR]
))
val selectableDates = if (fieldUiModel.allowFutureDates == true) {
SelectableDates(initialDate = DEFAULT_MIN_DATE, endDate = DEFAULT_MAX_DATE)
} else {
Expand Down

0 comments on commit afd699e

Please sign in to comment.