From f645cdccc7e5290a854d33802809f1524bf28458 Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Mon, 13 Jan 2025 22:29:26 -0800 Subject: [PATCH] Add updateStationSettings --- .../numbersstation/MainController.java | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/main/java/name/nathanmcrae/numbersstation/MainController.java b/src/main/java/name/nathanmcrae/numbersstation/MainController.java index f4089c1..4affa72 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/MainController.java +++ b/src/main/java/name/nathanmcrae/numbersstation/MainController.java @@ -7,12 +7,14 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Random; import java.util.ResourceBundle; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.control.Alert; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; @@ -51,6 +53,14 @@ public class MainController implements Initializable { Parent root = fxmlLoader.load(); MainSettingsController controller = fxmlLoader.getController(); + if (selectedStation == null) { + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle("Invalid Station Selected"); + alert.setHeaderText(null); + alert.setContentText("Invalid station selected. Please choose a station."); + alert.showAndWait(); + return; + } controller.setStationSettings(selectedStation); settingsStage = new Stage(); @@ -59,7 +69,7 @@ public class MainController implements Initializable { settingsStage.setScene(new Scene(root)); settingsStage.show(); settingsStage.setOnHiding(event -> { - settings.save(); + updateStationSettings(selectedStation); }); } catch (Exception e) { e.printStackTrace(); @@ -76,7 +86,6 @@ public class MainController implements Initializable { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("StationSelectionView.fxml")); Parent root = fxmlLoader.load(); - // Pass settings to the controller StationSelectionController controller = fxmlLoader.getController(); controller.setSettings(settings); @@ -92,13 +101,7 @@ public class MainController implements Initializable { .filter(station -> station.getName().equals(newStationName)) .findFirst() .orElse(null); - if (newSelectedStation != null) { - selectedStation = newSelectedStation; - selectedStationName.set(newStationName); - settings.setSelectedStationName(selectedStationName.get()); - - settings.save(); - } + updateStationSettings(newSelectedStation); }); } catch (Exception e) { e.printStackTrace(); @@ -111,6 +114,7 @@ public class MainController implements Initializable { private void loadSettings() throws IOException { XmlMapper xmlMapper = new XmlMapper(); String userHome = System.getProperty("user.home"); + // TODO: XDG Path directoryPath = Paths.get(userHome, ".numbers-station"); Path filePath = directoryPath.resolve("settings.xml"); try { @@ -203,4 +207,31 @@ public class MainController implements Initializable { } }); } + + private void updateStationSettings(StationSettings newStationSettings) { + if (newStationSettings == null) { + return; + } + + selectedStation = newStationSettings; + selectedStationName.set(newStationSettings.getName()); + settings.setSelectedStationName(selectedStationName.get()); + + settings.save(); + + // TODO: Load message from file + // If the message we're overwriting is different from its file, then prompt to confirm + + Random random = new Random(); + StringBuilder messageBuilder = new StringBuilder(); + + for (int i = 0; i < selectedStation.getMessageLength(); i++) { + if (i > 0 && i % selectedStation.getDigitsPerGroup() == 0) { + messageBuilder.append(" "); + } + messageBuilder.append(random.nextInt(10)); + } + + messageTextArea.setText(messageBuilder.toString()); + } }