Load next message file to message buffer

This commit is contained in:
Nathan McRae 2025-02-22 14:20:39 -08:00
parent e4ad5d16d6
commit 12df6d97fa
2 changed files with 55 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import com.leakyabstractions.result.core.Results;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -121,18 +122,14 @@ public class MainController implements Initializable {
@FXML
private void handleSetAsNextMessageButtonPress() {
Path stationDirPath = selectedStation.stationPath();
try {
if (!Files.exists(stationDirPath)) {
Files.createDirectories(stationDirPath);
if (!Files.exists(selectedStation.stationPath())) {
Files.createDirectories(selectedStation.stationPath());
}
Path nextMessagePath = stationDirPath.resolve("next-message.txt");
Files.write(nextMessagePath, messageTextArea.getText().getBytes());
Files.write(selectedStation.nextMessagePath(), messageTextArea.getText().getBytes());
} catch (IOException e) {
String message = "Failed to write next message to " + stationDirPath.toString();
String message = "Failed to write next message to " + selectedStation.nextMessagePath().toString();
logger.log(Level.SEVERE, message, e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception");
@ -180,6 +177,8 @@ public class MainController implements Initializable {
selectedStationName.set(selectedStation.getName());
}
updateStationSettings(selectedStation);
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
int cursorPosition = messageTextArea.getCaretPosition();
@ -250,17 +249,53 @@ public class MainController implements Initializable {
// TODO: Load message from file
// If the message we're overwriting is different from its file, then prompt to confirm
Path nextMessagePath = selectedStation.stationPath().resolve("next-message.txt");
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(" ");
String messageText;
if (!Files.exists(nextMessagePath)) {
try {
messageText = selectedStation.generateMessage(settings.getMessageGenerationAttempts());
} catch (StationSettings.MessageGenerationException e) {
String errMessage = "Failed to generate message";
logger.log(Level.SEVERE, errMessage, e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception");
alert.setHeaderText(null);
alert.setContentText(errMessage + ": " + e.getMessage());
alert.showAndWait();
return;
}
try {
if (!Files.exists(selectedStation.stationPath())) {
Files.createDirectories(selectedStation.stationPath());
}
Files.write(nextMessagePath, messageText.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
String errMessage = "Failed to save message";
logger.log(Level.SEVERE, errMessage, e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception");
alert.setHeaderText(null);
alert.setContentText(errMessage + ": " + e.getMessage());
alert.showAndWait();
return;
}
} else {
try {
messageText = new String(Files.readAllBytes(nextMessagePath));
} catch (IOException e) {
String errMessage = "Failed to read message";
logger.log(Level.SEVERE, errMessage, e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception");
alert.setHeaderText(null);
alert.setContentText(errMessage + ": " + e.getMessage());
alert.showAndWait();
return;
}
messageBuilder.append(random.nextInt(10));
}
messageTextArea.setText(messageBuilder.toString());
messageTextArea.setText(messageText);
}
}

View File

@ -81,6 +81,10 @@ public class StationSettings {
});
}
public Path nextMessagePath() {
return stationPath().resolve("next-message.txt");
}
public Path stationPath() {
String configHome = System.getenv("XDG_CONFIG_HOME");
Path directoryPath;