refactor: first attempt at dependency injection

also some more renaming and improving date-picker dialog
This commit is contained in:
David Guler
2022-11-14 20:00:01 +01:00
parent 4f80a0a3e0
commit 15279838b7
14 changed files with 378 additions and 333 deletions
@@ -1,18 +1,16 @@
package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.io.PlantList;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.models.PlantListModel;
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
@@ -20,20 +18,29 @@ import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyScheduleController {
private static final Logger LOG = Logger.getLogger(MyScheduleController.class.getName());
public class MyScheduleController implements Initializable {
private Crop selectedCrop = null;
private final GardenSchedule gardenSchedule = new GardenSchedule();
private final Garden garden = new Garden(gardenSchedule);
private final PlantListModel plantListModel = new PlantListModel();
private GardenSchedule gardenSchedule;
private Garden garden;
private PlantList plantList;
private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@SuppressWarnings("unused")
public void injectDependencies(Garden garden, GardenSchedule gardenSchedule, PlantList plantList) {
this.garden = garden;
this.gardenSchedule = gardenSchedule;
this.plantList = plantList;
init();
}
@FXML
private Label day1_label;
@@ -82,11 +89,7 @@ public class MyScheduleController implements Initializable {
@FXML
private ListView<Crop> scheduledPlants_listview;
public MyScheduleController() throws IOException {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
public void init() {
List<Crop> cropList;
try {
cropList = garden.getCrops();
@@ -107,15 +110,12 @@ public class MyScheduleController implements Initializable {
}
private void lookForSelectedListEntries() {
scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Crop>() {
@Override
public void changed(ObservableValue<? extends Crop> observable, Crop oldValue, Crop newValue) {
selectedCrop = newValue;
try {
loadTaskList();
} catch (IOException e) {
e.printStackTrace();
}
scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
selectedCrop = newValue;
try {
loadTaskList();
} catch (IOException e) {
e.printStackTrace();
}
});
}
@@ -132,7 +132,7 @@ public class MyScheduleController implements Initializable {
}
private void setCellFactoryListView() {
scheduledPlants_listview.setCellFactory(param -> new ListCell<Crop>() {
scheduledPlants_listview.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(Crop crop, boolean empty) {
super.updateItem(crop, empty);
@@ -141,9 +141,12 @@ public class MyScheduleController implements Initializable {
setText(null);
} else {
try {
setText(plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0).name());
String text = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
.map(Plant::name)
.orElse("");
setText(text);
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
LOG.log(Level.WARNING, "Could not get plant for Cell", e);
}
}
}
@@ -151,7 +154,7 @@ public class MyScheduleController implements Initializable {
}
private void loadTaskList() throws IOException {
List<List<Task>> taskLists = new LinkedList<>();
List<List<Task>> taskLists;
if (selectedCrop != null) {
taskLists = gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get());
} else {