Add updateStationSettings

This commit is contained in:
Nathan McRae 2025-01-13 22:29:26 -08:00
parent 55dc4c0181
commit f645cdccc7

View File

@ -7,12 +7,14 @@ import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Random;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
@ -51,6 +53,14 @@ public class MainController implements Initializable {
Parent root = fxmlLoader.load(); Parent root = fxmlLoader.load();
MainSettingsController controller = fxmlLoader.getController(); 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); controller.setStationSettings(selectedStation);
settingsStage = new Stage(); settingsStage = new Stage();
@ -59,7 +69,7 @@ public class MainController implements Initializable {
settingsStage.setScene(new Scene(root)); settingsStage.setScene(new Scene(root));
settingsStage.show(); settingsStage.show();
settingsStage.setOnHiding(event -> { settingsStage.setOnHiding(event -> {
settings.save(); updateStationSettings(selectedStation);
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -76,7 +86,6 @@ public class MainController implements Initializable {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("StationSelectionView.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("StationSelectionView.fxml"));
Parent root = fxmlLoader.load(); Parent root = fxmlLoader.load();
// Pass settings to the controller
StationSelectionController controller = fxmlLoader.getController(); StationSelectionController controller = fxmlLoader.getController();
controller.setSettings(settings); controller.setSettings(settings);
@ -92,13 +101,7 @@ public class MainController implements Initializable {
.filter(station -> station.getName().equals(newStationName)) .filter(station -> station.getName().equals(newStationName))
.findFirst() .findFirst()
.orElse(null); .orElse(null);
if (newSelectedStation != null) { updateStationSettings(newSelectedStation);
selectedStation = newSelectedStation;
selectedStationName.set(newStationName);
settings.setSelectedStationName(selectedStationName.get());
settings.save();
}
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -111,6 +114,7 @@ public class MainController implements Initializable {
private void loadSettings() throws IOException { private void loadSettings() throws IOException {
XmlMapper xmlMapper = new XmlMapper(); XmlMapper xmlMapper = new XmlMapper();
String userHome = System.getProperty("user.home"); String userHome = System.getProperty("user.home");
// TODO: XDG
Path directoryPath = Paths.get(userHome, ".numbers-station"); Path directoryPath = Paths.get(userHome, ".numbers-station");
Path filePath = directoryPath.resolve("settings.xml"); Path filePath = directoryPath.resolve("settings.xml");
try { 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());
}
} }