Implement next send time label
This commit is contained in:
		| @@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets; | |||||||
| 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.time.format.DateTimeFormatter; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.logging.Level; | import java.util.logging.Level; | ||||||
| import java.util.logging.Logger; | import java.util.logging.Logger; | ||||||
| @@ -46,6 +47,8 @@ public class MainController implements Initializable { | |||||||
|     private MainSettings settings; |     private MainSettings settings; | ||||||
|     private StationSettings selectedStation; |     private StationSettings selectedStation; | ||||||
|  |  | ||||||
|  |     private StringProperty nextSendTime = new SimpleStringProperty(); | ||||||
|  |  | ||||||
|     private StringProperty selectedStationName = new SimpleStringProperty(); |     private StringProperty selectedStationName = new SimpleStringProperty(); | ||||||
|  |  | ||||||
|     private SimpleBooleanProperty unsavedChanges = new SimpleBooleanProperty(); |     private SimpleBooleanProperty unsavedChanges = new SimpleBooleanProperty(); | ||||||
| @@ -53,7 +56,7 @@ public class MainController implements Initializable { | |||||||
|     public StringProperty windowTitle = new SimpleStringProperty("Numbers Station"); |     public StringProperty windowTitle = new SimpleStringProperty("Numbers Station"); | ||||||
|  |  | ||||||
|     @FXML |     @FXML | ||||||
|     private Label lastRetrievedLabel; |     private Label nextSendTimeLabel; | ||||||
|  |  | ||||||
|     @FXML |     @FXML | ||||||
|     private TextArea messageTextArea; |     private TextArea messageTextArea; | ||||||
| @@ -171,6 +174,9 @@ public class MainController implements Initializable { | |||||||
|     @Override |     @Override | ||||||
|     public void initialize(URL location, ResourceBundle resources) { |     public void initialize(URL location, ResourceBundle resources) { | ||||||
|         stationNameField.textProperty().bindBidirectional(selectedStationName); |         stationNameField.textProperty().bindBidirectional(selectedStationName); | ||||||
|  |         nextSendTimeLabel.textProperty().bind(nextSendTime); | ||||||
|  |  | ||||||
|  |         nextSendTime.set("sup brah"); | ||||||
|  |  | ||||||
|         Result<MainSettings, Exception> result = MainSettings.load(); |         Result<MainSettings, Exception> result = MainSettings.load(); | ||||||
|         if (!result.hasSuccess()) { |         if (!result.hasSuccess()) { | ||||||
| @@ -283,6 +289,21 @@ public class MainController implements Initializable { | |||||||
|  |  | ||||||
|         logger.info("Updating station settings: " + newStationSettings.getName()); |         logger.info("Updating station settings: " + newStationSettings.getName()); | ||||||
|  |  | ||||||
|  |         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |         nextSendTime.set(newStationSettings.nextSendTime().format(dateFormatter)); | ||||||
|  |         } catch (StationSettings.StationSettingsException e) { | ||||||
|  |             String errMessage = "Failed to calculate next message time"; | ||||||
|  |             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; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         selectedStation = newStationSettings; |         selectedStation = newStationSettings; | ||||||
|         selectedStationName.set(newStationSettings.getName()); |         selectedStationName.set(newStationSettings.getName()); | ||||||
|         settings.setSelectedStationName(selectedStationName.get()); |         settings.setSelectedStationName(selectedStationName.get()); | ||||||
|   | |||||||
| @@ -11,7 +11,9 @@ import java.nio.file.Paths; | |||||||
| import java.nio.file.SimpleFileVisitor; | import java.nio.file.SimpleFileVisitor; | ||||||
| import java.security.SecureRandom; | import java.security.SecureRandom; | ||||||
| import java.time.LocalDate; | import java.time.LocalDate; | ||||||
|  | import java.time.LocalDateTime; | ||||||
| import java.time.LocalTime; | import java.time.LocalTime; | ||||||
|  | import java.time.Period; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.logging.Logger; | import java.util.logging.Logger; | ||||||
| import java.util.logging.Level; | import java.util.logging.Level; | ||||||
| @@ -191,6 +193,49 @@ public class StationSettings { | |||||||
|         return messageFormattedBuilder.toString(); |         return messageFormattedBuilder.toString(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public LocalDateTime nextSendTime() throws StationSettingsException { | ||||||
|  |         Period sinceScheduleStart = Period.between(scheduleStartDate , LocalDate.now()); | ||||||
|  |         // If this period's time has not yet passed, then show that time. | ||||||
|  |         // Otherwise, show the next period's time. | ||||||
|  |         switch (messagePeriod) { | ||||||
|  |             case DAILY: | ||||||
|  |                 if (LocalTime.now().isBefore(scheduleStartTime)) { | ||||||
|  |                     return LocalDate.now().atTime(scheduleStartTime); | ||||||
|  |                 } else { | ||||||
|  |                     return LocalDate.now().plusDays(1).atTime(scheduleStartTime); | ||||||
|  |                 } | ||||||
|  |             case WEEKLY: | ||||||
|  |                 int weekdayDifference = scheduleStartDate.getDayOfWeek().getValue() - LocalDate.now().getDayOfWeek().getValue(); | ||||||
|  |                 System.out.println("weekdayDifference: " + weekdayDifference); | ||||||
|  |                 if (weekdayDifference > 0) { | ||||||
|  |                     return LocalDate.now().plusDays(weekdayDifference).atTime(scheduleStartTime); | ||||||
|  |                  } else if (weekdayDifference == 0){ | ||||||
|  |                     if (LocalTime.now().isBefore(scheduleStartTime)) { | ||||||
|  |                         return LocalDate.now().atTime(scheduleStartTime); | ||||||
|  |                     } else { | ||||||
|  |                         return LocalDate.now().plusWeeks(1).atTime(scheduleStartTime); | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     return LocalDate.now().plusDays(7 + weekdayDifference).atTime(scheduleStartTime); | ||||||
|  |                 } | ||||||
|  |             case MONTHLY: | ||||||
|  |                 int monthdayDifference = scheduleStartDate.getDayOfMonth() - LocalDate.now().getDayOfMonth(); | ||||||
|  |                 if (monthdayDifference > 0) { | ||||||
|  |                     return LocalDate.now().plusDays(monthdayDifference).atTime(scheduleStartTime); | ||||||
|  |                 } else if (monthdayDifference == 0) { | ||||||
|  |                     if (LocalTime.now().isBefore(scheduleStartTime)) { | ||||||
|  |                         return LocalDate.now().atTime(scheduleStartTime); | ||||||
|  |                     } else { | ||||||
|  |                         return LocalDate.now().plusMonths(1).atTime(scheduleStartTime); | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     return LocalDate.now().plusMonths(1).plusDays(monthdayDifference).atTime(scheduleStartTime); | ||||||
|  |                 } | ||||||
|  |             default: | ||||||
|  |                 throw new StationSettingsException("Invalid period value: " + messagePeriod); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public String getAddress() { |     public String getAddress() { | ||||||
|         return address; |         return address; | ||||||
|     } |     } | ||||||
| @@ -304,4 +349,11 @@ public class StationSettings { | |||||||
|             super(message); |             super(message); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static class StationSettingsException extends Exception { | ||||||
|  |         public StationSettingsException(String message) { | ||||||
|  |             super(message); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -77,7 +77,7 @@ | |||||||
|                         <Font size="14.0" /> |                         <Font size="14.0" /> | ||||||
|                      </font> |                      </font> | ||||||
|                   </Label> |                   </Label> | ||||||
|                   <Label fx:id="lastRetrievedLabel" layoutX="201.0" layoutY="15.0" text="2024-12-25T12:59:47" AnchorPane.leftAnchor="201.0" AnchorPane.topAnchor="15.0"> |                   <Label fx:id="nextSendTimeLabel" layoutX="202.0" layoutY="14.0" text="2024-12-25T12:59:47" AnchorPane.leftAnchor="202.0" AnchorPane.topAnchor="14.0"> | ||||||
|                      <font> |                      <font> | ||||||
|                         <Font size="14.0" /> |                         <Font size="14.0" /> | ||||||
|                      </font> |                      </font> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user