From 4230afcbac91e3bb419d21b30115072060b8ebc4 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 23 Apr 2024 16:12:36 +0200 Subject: [PATCH] refactored timeDiff-message-method to updatePref controller --- .../cryptomator/ui/fxapp/UpdateChecker.java | 34 +------------ .../UpdatesPreferencesController.java | 48 +++++++++++++------ 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java b/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java index 0558a5ea3..3321128b8 100644 --- a/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java +++ b/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java @@ -33,7 +33,6 @@ public class UpdateChecker { private final Environment env; private final Settings settings; - private final ResourceBundle resourceBundle; private final StringProperty latestVersion = new SimpleStringProperty(); private final ScheduledService updateCheckerService; private final ObjectProperty state = new SimpleObjectProperty<>(UpdateCheckState.NOT_CHECKED); @@ -45,11 +44,9 @@ public class UpdateChecker { @Inject UpdateChecker(Settings settings, // Environment env, // - ResourceBundle resourceBundle, // ScheduledService updateCheckerService) { this.env = env; this.settings = settings; - this.resourceBundle = resourceBundle; this.updateCheckerService = updateCheckerService; this.latestVersion.bindBidirectional(settings.latestVersion); this.lastSuccessfulUpdateCheck.bindBidirectional(settings.lastSuccessfulUpdateCheck); @@ -58,9 +55,6 @@ public class UpdateChecker { var latestVersion = this.latestVersion.get(); return latestVersion != null && versionComparator.compare(getCurrentVersion(), latestVersion) < 0; }, latestVersion); - - //TODO - updateTimeDifferenceMessage(); } public void automaticallyCheckForUpdatesIfEnabled() { @@ -83,26 +77,6 @@ public class UpdateChecker { updateCheckerService.start(); } - private void updateTimeDifferenceMessage() { - Date updateCheckDate = Date.from(lastSuccessfulUpdateCheck.get()); - if (updateCheckDate == null || updateCheckDate.equals(Settings.DEFAULT_LAST_SUCCESSFUL_UPDATE_CHECK)) { - timeDifferenceMessage.set(resourceBundle.getString("preferences.updates.lastUpdateCheck.never")); - return; - } - - var duration = java.time.Duration.between(LocalDateTime.ofInstant(updateCheckDate.toInstant(), ZoneId.systemDefault()), LocalDateTime.now()); - - var hours = duration.toHours(); - - if (hours < 1) { - timeDifferenceMessage.set(resourceBundle.getString("preferences.updates.lastUpdateCheck.recently")); - } else if (hours < 24) { - timeDifferenceMessage.set(String.format(resourceBundle.getString("preferences.updates.lastUpdateCheck.hoursAgo"), hours)); - } else { - timeDifferenceMessage.set(String.format(resourceBundle.getString("preferences.updates.lastUpdateCheck.daysAgo"), duration.toDays())); - } - } - private void checkStarted(WorkerStateEvent event) { LOG.debug("Checking for updates..."); state.set(UpdateCheckState.IS_CHECKING); @@ -112,8 +86,6 @@ public class UpdateChecker { var latestVersionString = updateCheckerService.getValue(); LOG.info("Current version: {}, latest version: {}", getCurrentVersion(), latestVersionString); lastSuccessfulUpdateCheck.set(Instant.now()); - //TODO refactor - updateTimeDifferenceMessage(); latestVersion.set(latestVersionString); state.set(UpdateCheckState.CHECK_SUCCESSFUL); } @@ -147,14 +119,10 @@ public class UpdateChecker { return updateAvailable.get(); } - public ObjectProperty lastSuccessfulUpdateCheckProperty() { + public ObjectProperty lastSuccessfulUpdateCheckProperty() { return lastSuccessfulUpdateCheck; } - public StringProperty timeDifferenceMessageProperty() { - return timeDifferenceMessage; - } - public ObjectProperty updateCheckStateProperty() { return state; } diff --git a/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java b/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java index 7463d5a19..0d54a91c3 100644 --- a/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java +++ b/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java @@ -15,18 +15,20 @@ import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.ReadOnlyStringProperty; import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.layout.HBox; -import javafx.util.Duration; +import java.time.Duration; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; -import java.util.Date; import java.util.Locale; +import java.util.ResourceBundle; @PreferencesScoped @@ -36,12 +38,13 @@ public class UpdatesPreferencesController implements FxController { private final Application application; private final Environment environment; + private final ResourceBundle resourceBundle; private final Settings settings; private final UpdateChecker updateChecker; private final ObjectBinding checkForUpdatesButtonState; private final ReadOnlyStringProperty latestVersion; - private final ObjectProperty lastSuccessfulUpdateCheck; - private final ReadOnlyStringProperty timeDifferenceMessage; + private final ObservableValue lastSuccessfulUpdateCheck; + private final ObservableValue timeDifferenceMessage; private final String currentVersion; private final BooleanBinding updateAvailable; private final BooleanProperty upToDateLabelVisible = new SimpleBooleanProperty(false); @@ -53,15 +56,16 @@ public class UpdatesPreferencesController implements FxController { public Label upToDateLabel; @Inject - UpdatesPreferencesController(Application application, Environment environment, Settings settings, UpdateChecker updateChecker) { + UpdatesPreferencesController(Application application, Environment environment, ResourceBundle resourceBundle, Settings settings, UpdateChecker updateChecker) { this.application = application; this.environment = environment; + this.resourceBundle = resourceBundle; this.settings = settings; this.updateChecker = updateChecker; this.checkForUpdatesButtonState = Bindings.when(updateChecker.checkingForUpdatesProperty()).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY); this.latestVersion = updateChecker.latestVersionProperty(); this.lastSuccessfulUpdateCheck = updateChecker.lastSuccessfulUpdateCheckProperty(); - this.timeDifferenceMessage = updateChecker.timeDifferenceMessageProperty(); + this.timeDifferenceMessage = lastSuccessfulUpdateCheck.map(this::updateTimeDifferenceMessage); this.currentVersion = updateChecker.getCurrentVersion(); this.updateAvailable = updateChecker.updateAvailableProperty(); this.updateCheckState = updateChecker.updateCheckStateProperty(); @@ -75,7 +79,7 @@ public class UpdatesPreferencesController implements FxController { updateCheckState.addListener((_, _, _) -> { if (isUpdateSuccessfulAndCurrent.get()) { upToDateLabelVisible.set(true); - PauseTransition delay = new PauseTransition(Duration.seconds(5)); + PauseTransition delay = new PauseTransition(javafx.util.Duration.seconds(5)); delay.setOnFinished(_ -> upToDateLabelVisible.set(false)); delay.play(); } @@ -119,27 +123,43 @@ public class UpdatesPreferencesController implements FxController { return currentVersion; } - public ObjectProperty lastSuccessfulUpdateCheckProperty() { + public ObservableValue lastSuccessfulUpdateCheckProperty() { return lastSuccessfulUpdateCheck; } public String getLastSuccessfulUpdateCheck() { - Date date = lastSuccessfulUpdateCheck.get(); - if (date != null && !date.equals(Settings.DEFAULT_LAST_SUCCESSFUL_UPDATE_CHECK)) { - LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + Instant lastCheck = lastSuccessfulUpdateCheck.getValue(); + if (lastCheck != null && !lastCheck.equals(Settings.DEFAULT_LAST_SUCCESSFUL_UPDATE_CHECK)) { DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.getDefault()); - return formatter.format(localDateTime); + return formatter.format(LocalDateTime.ofInstant(lastCheck, ZoneId.systemDefault())); } else { return "-"; } } - public ReadOnlyStringProperty timeDifferenceMessageProperty(){ + private String updateTimeDifferenceMessage(Instant lastSuccessCheck) { + if (lastSuccessCheck.equals(Settings.DEFAULT_LAST_SUCCESSFUL_UPDATE_CHECK)) { + return resourceBundle.getString("preferences.updates.lastUpdateCheck.never"); + } + + var duration = Duration.between(lastSuccessCheck, Instant.now()); + var hours = duration.toHours(); + if (hours < 1) { + return resourceBundle.getString("preferences.updates.lastUpdateCheck.recently"); + } else if (hours < 24) { + return String.format(resourceBundle.getString("preferences.updates.lastUpdateCheck.hoursAgo"), hours); + } else { + return String.format(resourceBundle.getString("preferences.updates.lastUpdateCheck.daysAgo"), duration.toDays()); + } + } + + + public ObservableValue timeDifferenceMessageProperty() { return timeDifferenceMessage; } public String getTimeDifferenceMessage() { - return timeDifferenceMessage.get(); + return timeDifferenceMessage.getValue(); } public BooleanProperty upToDateLabelVisibleProperty() {