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,23 +93,27 @@ public class CropDetailController {
|
|||
|
||||
}
|
||||
|
||||
public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
|
||||
public void setPlantFromCrop(Crop crop) throws PlantNotFoundException {
|
||||
this.crop = crop;
|
||||
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
|
||||
.orElseThrow(PlantNotFoundException::new);
|
||||
try {
|
||||
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
|
||||
.orElseThrow(PlantNotFoundException::new);
|
||||
|
||||
cropName_label.setText(plant.name());
|
||||
description_label.setText(plant.description());
|
||||
light_label.setText(String.valueOf(plant.light()));
|
||||
soil_label.setText(plant.soil());
|
||||
spacing_label.setText(plant.spacing());
|
||||
if (plant.image() != null) {
|
||||
imageView.setImage(plant.image());
|
||||
cropName_label.setText(plant.name());
|
||||
description_label.setText(plant.description());
|
||||
light_label.setText(String.valueOf(plant.light()));
|
||||
soil_label.setText(plant.soil());
|
||||
spacing_label.setText(plant.spacing());
|
||||
if (plant.image() != null) {
|
||||
imageView.setImage(plant.image());
|
||||
}
|
||||
area_label.setText("");
|
||||
location_label.setText("");
|
||||
createTaskLists(crop);
|
||||
createPestList(plant);
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
throw new PlantNotFoundException();
|
||||
}
|
||||
area_label.setText("");
|
||||
location_label.setText("");
|
||||
createTaskLists(crop);
|
||||
createPestList(plant);
|
||||
}
|
||||
|
||||
private void createTaskLists(Crop crop) {
|
||||
|
|
|
@ -108,7 +108,7 @@ public class MyGardenController {
|
|||
stage.initModality(Modality.APPLICATION_MODAL);
|
||||
stage.setResizable(true);
|
||||
stage.showAndWait();
|
||||
} catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) {
|
||||
} catch (IOException | PlantNotFoundException e) {
|
||||
// TODO: show error alert
|
||||
LOG.log(Level.SEVERE, "Could not load plant details.", e);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import javafx.scene.control.*;
|
|||
import javafx.util.Callback;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.time.MonthDay;
|
||||
|
||||
public class SelectSowDayController {
|
||||
private Plant selectedPlant;
|
||||
|
@ -61,36 +61,49 @@ public class SelectSowDayController {
|
|||
|
||||
/**
|
||||
* date picker disable/enable dates according to selected plant: sow or harvest day
|
||||
*
|
||||
* @return cellFactory of datePicker
|
||||
*/
|
||||
private Callback<DatePicker, DateCell> getDayCellFactory() {
|
||||
|
||||
return (datePicker) -> new DateCell() {
|
||||
private final LocalDate today = LocalDate.now();
|
||||
|
||||
@Override
|
||||
public void updateItem(LocalDate item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
setDisable(true);
|
||||
setStyle("-fx-background-color: #ffc0cb;");
|
||||
List<LocalDate> dates;
|
||||
LocalDate today = LocalDate.now();
|
||||
if (harvest_radio.isSelected()) {
|
||||
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST);
|
||||
} else {
|
||||
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW);
|
||||
}
|
||||
for (LocalDate date : dates) {
|
||||
if (item.getMonth() == date.getMonth()
|
||||
&& item.getDayOfMonth() == date.getDayOfMonth()
|
||||
&& item.compareTo(today) > 0) {
|
||||
|
||||
if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) >= 0)) {
|
||||
GrowthPhaseType selectedPhase = harvest_radio.isSelected() ? GrowthPhaseType.HARVEST : GrowthPhaseType.SOW;
|
||||
MonthDay minDate = selectedPlant.getMinDateForGrowthPhase(selectedPhase);
|
||||
MonthDay maxDate = selectedPlant.getMaxDateForGrowthPhase(selectedPhase);
|
||||
|
||||
if (dateInRange(item, minDate, maxDate)) {
|
||||
setDisable(false);
|
||||
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 java.time.LocalDate;
|
||||
import java.util.LinkedList;
|
||||
import java.time.MonthDay;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -69,37 +69,19 @@ public record Plant(
|
|||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* transform monthDay value of the given growthPhase to localDate
|
||||
* return a list of dates from start to end of growth phase
|
||||
* @param growthPhase the current growthPhase
|
||||
* @return a list of dates of the current year
|
||||
*/
|
||||
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 getMaxDateForGrowthPhase(GrowthPhaseType growthPhase) {
|
||||
return lifecycle.stream()
|
||||
.filter(phase -> phase.type().equals(growthPhase))
|
||||
.findFirst()
|
||||
.map(GrowthPhase::endDate)
|
||||
.orElse(MonthDay.of(12, 31));
|
||||
}
|
||||
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