refactor: simplified dayCellFactory for date selector
Instead of generating a list of dates for every single visible date and checking if it is contained in that list, we now use a (admittedly scary-looking) predicate to compare the date to the start and enddates
This commit is contained in:
parent
05e7bcc2e8
commit
90d2de65de
|
@ -93,8 +93,9 @@ public class CropDetailController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
|
public void setPlantFromCrop(Crop crop) throws PlantNotFoundException {
|
||||||
this.crop = crop;
|
this.crop = crop;
|
||||||
|
try {
|
||||||
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
|
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
|
||||||
.orElseThrow(PlantNotFoundException::new);
|
.orElseThrow(PlantNotFoundException::new);
|
||||||
|
|
||||||
|
@ -110,6 +111,9 @@ public class CropDetailController {
|
||||||
location_label.setText("");
|
location_label.setText("");
|
||||||
createTaskLists(crop);
|
createTaskLists(crop);
|
||||||
createPestList(plant);
|
createPestList(plant);
|
||||||
|
} catch (HardinessZoneNotSetException | IOException e) {
|
||||||
|
throw new PlantNotFoundException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createTaskLists(Crop crop) {
|
private void createTaskLists(Crop crop) {
|
||||||
|
|
|
@ -108,7 +108,7 @@ public class MyGardenController {
|
||||||
stage.initModality(Modality.APPLICATION_MODAL);
|
stage.initModality(Modality.APPLICATION_MODAL);
|
||||||
stage.setResizable(true);
|
stage.setResizable(true);
|
||||||
stage.showAndWait();
|
stage.showAndWait();
|
||||||
} catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) {
|
} catch (IOException | PlantNotFoundException e) {
|
||||||
// TODO: show error alert
|
// TODO: show error alert
|
||||||
LOG.log(Level.SEVERE, "Could not load plant details.", e);
|
LOG.log(Level.SEVERE, "Could not load plant details.", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import javafx.scene.control.*;
|
||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.time.MonthDay;
|
||||||
|
|
||||||
public class SelectSowDayController {
|
public class SelectSowDayController {
|
||||||
private Plant selectedPlant;
|
private Plant selectedPlant;
|
||||||
|
@ -61,36 +61,49 @@ public class SelectSowDayController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* date picker disable/enable dates according to selected plant: sow or harvest day
|
* date picker disable/enable dates according to selected plant: sow or harvest day
|
||||||
|
*
|
||||||
* @return cellFactory of datePicker
|
* @return cellFactory of datePicker
|
||||||
*/
|
*/
|
||||||
private Callback<DatePicker, DateCell> getDayCellFactory() {
|
private Callback<DatePicker, DateCell> getDayCellFactory() {
|
||||||
|
|
||||||
return (datePicker) -> new DateCell() {
|
return (datePicker) -> new DateCell() {
|
||||||
|
private final LocalDate today = LocalDate.now();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateItem(LocalDate item, boolean empty) {
|
public void updateItem(LocalDate item, boolean empty) {
|
||||||
super.updateItem(item, empty);
|
super.updateItem(item, empty);
|
||||||
setDisable(true);
|
setDisable(true);
|
||||||
setStyle("-fx-background-color: #ffc0cb;");
|
setStyle("-fx-background-color: #ffc0cb;");
|
||||||
List<LocalDate> dates;
|
|
||||||
LocalDate today = LocalDate.now();
|
if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) >= 0)) {
|
||||||
if (harvest_radio.isSelected()) {
|
GrowthPhaseType selectedPhase = harvest_radio.isSelected() ? GrowthPhaseType.HARVEST : GrowthPhaseType.SOW;
|
||||||
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST);
|
MonthDay minDate = selectedPlant.getMinDateForGrowthPhase(selectedPhase);
|
||||||
} else {
|
MonthDay maxDate = selectedPlant.getMaxDateForGrowthPhase(selectedPhase);
|
||||||
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW);
|
|
||||||
}
|
if (dateInRange(item, minDate, maxDate)) {
|
||||||
for (LocalDate date : dates) {
|
|
||||||
if (item.getMonth() == date.getMonth()
|
|
||||||
&& item.getDayOfMonth() == date.getDayOfMonth()
|
|
||||||
&& item.compareTo(today) > 0) {
|
|
||||||
setDisable(false);
|
setDisable(false);
|
||||||
setStyle("-fx-background-color: #32CD32;");
|
setStyle("-fx-background-color: #32CD32;");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((harvest_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) {
|
|
||||||
setDisable(true);
|
|
||||||
setStyle("-fx-background-color: #ffc0cb;");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given {@link LocalDate} is within the given {@link MonthDay} range.
|
||||||
|
* (regardless of year)
|
||||||
|
*
|
||||||
|
* @param subject The date to check
|
||||||
|
* @param min The start of the date-range
|
||||||
|
* @param max The end of the date-range
|
||||||
|
* @return Whether the subject is within the range.
|
||||||
|
*/
|
||||||
|
private boolean dateInRange(LocalDate subject, MonthDay min, MonthDay max) {
|
||||||
|
return subject.getMonth().compareTo(min.getMonth()) >= 0 &&
|
||||||
|
subject.getMonth().compareTo(max.getMonth()) <= 0 &&
|
||||||
|
// if the day is less than the minimum day, the minimum month must not be equal
|
||||||
|
(subject.getDayOfMonth() >= min.getDayOfMonth() || !subject.getMonth().equals(min.getMonth())) &&
|
||||||
|
// if the day is greater than the maximum day, the maximum month must not be equal
|
||||||
|
(subject.getDayOfMonth() <= max.getDayOfMonth() || !subject.getMonth().equals(max.getMonth()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package ch.zhaw.gartenverwaltung.types;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.LinkedList;
|
import java.time.MonthDay;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -69,37 +69,19 @@ public record Plant(
|
||||||
return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear));
|
return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* filter out the given growthPhase out of the lifecycle
|
|
||||||
* create list of dates for this growthPhase and return it
|
|
||||||
* @param growthPhase the wanted growthPhase
|
|
||||||
* @return a list of dates of the current year
|
|
||||||
*/
|
|
||||||
public List<LocalDate> getDateListOfGrowthPhase(GrowthPhaseType growthPhase) {
|
|
||||||
List<LocalDate> dates = new LinkedList<>();
|
|
||||||
for (GrowthPhase growth : lifecycle) {
|
|
||||||
if (growth.type().equals(growthPhase)) {
|
|
||||||
dates = addDatesFromMonthDay(growth);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dates;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public MonthDay getMaxDateForGrowthPhase(GrowthPhaseType growthPhase) {
|
||||||
* transform monthDay value of the given growthPhase to localDate
|
return lifecycle.stream()
|
||||||
* return a list of dates from start to end of growth phase
|
.filter(phase -> phase.type().equals(growthPhase))
|
||||||
* @param growthPhase the current growthPhase
|
.findFirst()
|
||||||
* @return a list of dates of the current year
|
.map(GrowthPhase::endDate)
|
||||||
*/
|
.orElse(MonthDay.of(12, 31));
|
||||||
private List<LocalDate> addDatesFromMonthDay(GrowthPhase growthPhase) {
|
|
||||||
List<LocalDate> dates = new LinkedList<>();
|
|
||||||
LocalDate today = LocalDate.now();
|
|
||||||
LocalDate start = growthPhase.startDate().atYear(today.getYear());
|
|
||||||
LocalDate end = growthPhase.endDate().atYear(today.getYear());
|
|
||||||
while (!start.isAfter(end)) {
|
|
||||||
dates.add(start);
|
|
||||||
start = start.plusDays(1);
|
|
||||||
}
|
}
|
||||||
return dates;
|
public MonthDay getMinDateForGrowthPhase(GrowthPhaseType growthPhase) {
|
||||||
|
return lifecycle.stream()
|
||||||
|
.filter(phase -> phase.type().equals(growthPhase))
|
||||||
|
.findFirst()
|
||||||
|
.map(GrowthPhase::startDate)
|
||||||
|
.orElse(MonthDay.of(1, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue