refactored timeDiff-message-method to updatePref controller

This commit is contained in:
Armin Schrenk 2024-04-23 16:12:36 +02:00
parent 7fc6ab05a4
commit 4230afcbac
No known key found for this signature in database
2 changed files with 35 additions and 47 deletions

View File

@ -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<String> updateCheckerService;
private final ObjectProperty<UpdateCheckState> state = new SimpleObjectProperty<>(UpdateCheckState.NOT_CHECKED);
@ -45,11 +44,9 @@ public class UpdateChecker {
@Inject
UpdateChecker(Settings settings, //
Environment env, //
ResourceBundle resourceBundle, //
ScheduledService<String> 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<Date> lastSuccessfulUpdateCheckProperty() {
public ObjectProperty<Instant> lastSuccessfulUpdateCheckProperty() {
return lastSuccessfulUpdateCheck;
}
public StringProperty timeDifferenceMessageProperty() {
return timeDifferenceMessage;
}
public ObjectProperty<UpdateCheckState> updateCheckStateProperty() {
return state;
}

View File

@ -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<ContentDisplay> checkForUpdatesButtonState;
private final ReadOnlyStringProperty latestVersion;
private final ObjectProperty<Date> lastSuccessfulUpdateCheck;
private final ReadOnlyStringProperty timeDifferenceMessage;
private final ObservableValue<Instant> lastSuccessfulUpdateCheck;
private final ObservableValue<String> 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<Date> lastSuccessfulUpdateCheckProperty() {
public ObservableValue<Instant> 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<String> timeDifferenceMessageProperty() {
return timeDifferenceMessage;
}
public String getTimeDifferenceMessage() {
return timeDifferenceMessage.get();
return timeDifferenceMessage.getValue();
}
public BooleanProperty upToDateLabelVisibleProperty() {