Skip to content

Commit

Permalink
Updated age and unit implementation for Mpox at case edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Flava177 committed Jan 27, 2025
1 parent 38a6de0 commit 7e5b446
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

import androidx.fragment.app.FragmentActivity;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -67,11 +70,14 @@
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.infrastructure.facility.FacilityDto;
import de.symeda.sormas.api.infrastructure.facility.FacilityTypeGroup;
import de.symeda.sormas.api.person.ApproximateAgeType;
import de.symeda.sormas.api.person.Sex;
import de.symeda.sormas.api.user.DefaultUserRole;
import de.symeda.sormas.api.user.JurisdictionLevel;
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.api.utils.CardOrHistory;
import de.symeda.sormas.api.utils.DataHelper;
import de.symeda.sormas.api.utils.DateHelper;
import de.symeda.sormas.api.utils.VaccineTypes;
import de.symeda.sormas.api.utils.YesNoUnknown;
import de.symeda.sormas.api.utils.fieldaccess.UiFieldAccessCheckers;
Expand All @@ -96,6 +102,7 @@
import de.symeda.sormas.app.component.dialog.InfoDialog;
import de.symeda.sormas.app.databinding.DialogClassificationRulesLayoutBinding;
import de.symeda.sormas.app.databinding.FragmentCaseEditLayoutBinding;
import de.symeda.sormas.app.databinding.FragmentPersonEditLayoutBinding;
import de.symeda.sormas.app.util.DataUtils;
import de.symeda.sormas.app.util.DiseaseConfigurationCache;
//import de.symeda.sormas.app.util.InfrastructureHelper;
Expand Down Expand Up @@ -144,6 +151,10 @@ public class CaseEditFragment extends BaseEditFragment<FragmentCaseEditLayoutBin
private List<Item> vaccinationList;
private List<Item> surveillanceOfficerList;

private List<Item> approximateAgeTypeList;
private List<Item> monthList;
private List<Item> yearList;

// Static methods

public static CaseEditFragment newInstance(Case activityRootData) {
Expand Down Expand Up @@ -439,6 +450,9 @@ record = getActivityRootData();
vaccineList = DataUtils.getEnumItems(VaccineTypes.class, true);
List<User> surveillanceOfficersList = DatabaseHelper.getUserDao().getUsersByUserRoleCaption(I18nProperties.getEnumCaption(DefaultUserRole.SURVEILLANCE_OFFICER));
surveillanceOfficerList = DataUtils.toItems(surveillanceOfficersList, true);
approximateAgeTypeList = DataUtils.getEnumItems(ApproximateAgeType.class, true);
monthList = DataUtils.getMonthItems(true);
yearList = DataUtils.toItems(DateHelper.getYearsToNow(), true);
}

@Override
Expand Down Expand Up @@ -655,6 +669,44 @@ public void onChange(ControlPropertyField field) {
contentBinding.caseDataNotifiedByList.initializeSpinner(notifyByList);
contentBinding.caseDataVaccineType.initializeSpinner(vaccineList);

contentBinding.personBirthdateDD.initializeSpinner(new ArrayList<>(), field -> updateApproximateAgeField(contentBinding));
contentBinding.personBirthdateMM.initializeSpinner(monthList, field -> {
updateApproximateAgeField(contentBinding);
DataUtils.updateListOfDays(
contentBinding.personBirthdateDD,
(Integer) contentBinding.personBirthdateYYYY.getValue(),
(Integer) field.getValue());
});
contentBinding.personBirthdateYYYY.initializeSpinner(yearList, field -> {
updateApproximateAgeField(contentBinding);
DataUtils.updateListOfDays(
contentBinding.personBirthdateDD,
(Integer) field.getValue(),
(Integer) contentBinding.personBirthdateMM.getValue());
});
int year = Calendar.getInstance().get(Calendar.YEAR);
contentBinding.personBirthdateYYYY.setSelectionOnOpen(year - 35);
contentBinding.personApproximateAgeType.initializeSpinner(approximateAgeTypeList);

contentBinding.personApproximateAge.addValueChangedListener(field -> {
if (DataHelper.isNullOrEmpty((String) field.getValue())) {
contentBinding.personApproximateAgeType.setRequired(false);
contentBinding.personApproximateAgeType.setValue(null);
} else {
contentBinding.personApproximateAgeType.setRequired(true);
if (contentBinding.personApproximateAgeType.getValue() == null) {
contentBinding.personApproximateAgeType.setValue(ApproximateAgeType.YEARS);
}
}
});

if (!DataHelper.isNullOrEmpty(contentBinding.personApproximateAge.getValue())) {
contentBinding.personApproximateAgeType.setRequired(true);
if (contentBinding.personApproximateAgeType.getValue() == null) {
contentBinding.personApproximateAgeType.setValue(ApproximateAgeType.YEARS);
}
}

contentBinding.caseDataQuarantineTo.addValueChangedListener(new ValueChangeListener() {

private Date currentQuarantineTo = record.getQuarantineTo();
Expand Down Expand Up @@ -1125,4 +1177,42 @@ private void handleCholera() {
}
}

public static Date calculateBirthDateValue(FragmentCaseEditLayoutBinding contentBinding) {
Integer birthYear = (Integer) contentBinding.personBirthdateYYYY.getValue();

if (birthYear != null) {
contentBinding.personApproximateAge.setEnabled(false);
contentBinding.personApproximateAgeType.setEnabled(false);

Integer birthDay = (Integer) contentBinding.personBirthdateDD.getValue();
Integer birthMonth = (Integer) contentBinding.personBirthdateMM.getValue();

Calendar birthDate = new GregorianCalendar();
birthDate.set(birthYear, birthMonth != null ? birthMonth - 1 : 0, birthDay != null ? birthDay : 1);
return birthDate.getTime();
}
return null;
}

private static void updateApproximateAgeField(FragmentCaseEditLayoutBinding contentBinding) {

Date birthDate = calculateBirthDateValue(contentBinding);
if (birthDate != null) {
contentBinding.personApproximateAge.setEnabled(false);
contentBinding.personApproximateAgeType.setEnabled(false);

DataHelper.Pair<Integer, ApproximateAgeType> approximateAge = ApproximateAgeType.ApproximateAgeHelper.getApproximateAge(birthDate);
ApproximateAgeType ageType = approximateAge.getElement1();
contentBinding.personApproximateAge.setValue(String.valueOf(approximateAge.getElement0()));
contentBinding.personApproximateAgeType.setValue(ageType);
} else {
if (contentBinding.personApproximateAge.isEnabled() == false && contentBinding.personApproximateAgeType.isEnabled() == false) {
contentBinding.personApproximateAge.setValue(null);
contentBinding.personApproximateAgeType.setValue(null);
}
contentBinding.personApproximateAge.setEnabled(true);
contentBinding.personApproximateAgeType.setEnabled(true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1418,12 +1418,14 @@
android:id="@+id/person_approximateAge"
app:value="@={data.person.approximateAge}"
app:inputType="number"
app:required="true"
app:maxLength="3"
style="@style/ControlFirstOfTwoColumnsStyle" />

<de.symeda.sormas.app.component.controls.ControlSpinnerField
android:id="@+id/person_approximateAgeType"
app:value="@={data.person.approximateAgeType}"
app:required="true"
style="@style/ControlSecondOfTwoColumnsStyle" />

</LinearLayout>
Expand Down

0 comments on commit 7e5b446

Please sign in to comment.