refactored ObservableValues to BooleanBindings and code cleanup

This commit is contained in:
Jan-Peter Klein 2024-05-10 15:45:17 +02:00
parent 5519eefcfa
commit dda2afda92
No known key found for this signature in database
GPG Key ID: 90EDA3A7C822FD0E
2 changed files with 28 additions and 27 deletions

View File

@ -1,7 +1,6 @@
package org.cryptomator.ui.fxapp;
import org.cryptomator.common.Environment;
import org.cryptomator.common.ObservableUtil;
import org.cryptomator.common.SemVerComparator;
import org.cryptomator.common.settings.Settings;
import org.slf4j.Logger;
@ -15,7 +14,6 @@ import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Worker;
import javafx.concurrent.WorkerStateEvent;
@ -36,8 +34,8 @@ public class UpdateChecker {
private final ObjectProperty<UpdateCheckState> state = new SimpleObjectProperty<>(UpdateCheckState.NOT_CHECKED);
private final ObjectProperty<Instant> lastSuccessfulUpdateCheck;
private final Comparator<String> versionComparator = new SemVerComparator();
private final ObservableValue<Boolean> updateAvailable;
private final ObservableValue<Boolean> checkFailed;
private final BooleanBinding updateAvailable;
private final BooleanBinding checkFailed;
@Inject
UpdateChecker(Settings settings, //
@ -47,7 +45,7 @@ public class UpdateChecker {
this.settings = settings;
this.updateCheckerService = updateCheckerService;
this.lastSuccessfulUpdateCheck = settings.lastSuccessfulUpdateCheck;
this.updateAvailable = ObservableUtil.mapWithDefault(latestVersion, latest -> versionComparator.compare(getCurrentVersion(), latest) < 0, false);
this.updateAvailable = Bindings.createBooleanBinding(this::isUpdateAvailable, latestVersion);
this.checkFailed = Bindings.equal(UpdateCheckState.CHECK_FAILED, state);
}
@ -104,15 +102,23 @@ public class UpdateChecker {
return latestVersion;
}
public ObservableValue<Boolean> updateAvailableProperty() {
public BooleanBinding updateAvailableProperty() {
return updateAvailable;
}
public ObservableValue<Boolean> checkFailedProperty() {
public BooleanBinding checkFailedProperty() {
return checkFailed;
}
public boolean isUpdateAvailable() {
return updateAvailable.getValue();
String currentVersion = getCurrentVersion();
String latestVersionString = latestVersion.get();
if (currentVersion == null || latestVersionString == null) {
return false;
} else {
return versionComparator.compare(currentVersion, latestVersionString) < 0;
}
}
public ObjectProperty<Instant> lastSuccessfulUpdateCheckProperty() {

View File

@ -45,8 +45,8 @@ public class UpdatesPreferencesController implements FxController {
private final StringBinding lastUpdateCheckMessage;
private final ObservableValue<String> timeDifferenceMessage;
private final String currentVersion;
private final ObservableValue<Boolean> updateAvailable;
private final ObservableValue<Boolean> checkFailed;
private final BooleanBinding updateAvailable;
private final BooleanBinding checkFailed;
private final BooleanProperty upToDateLabelVisible = new SimpleBooleanProperty(false);
private final DateTimeFormatter formatter;
private final BooleanBinding upToDate;
@ -64,7 +64,7 @@ public class UpdatesPreferencesController implements FxController {
this.checkForUpdatesButtonState = Bindings.when(updateChecker.checkingForUpdatesProperty()).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY);
this.latestVersion = updateChecker.latestVersionProperty();
this.lastSuccessfulUpdateCheck = updateChecker.lastSuccessfulUpdateCheckProperty();
this.timeDifferenceMessage = lastSuccessfulUpdateCheck.map(this::updateTimeDifferenceMessage);
this.timeDifferenceMessage = Bindings.createStringBinding(this::getTimeDifferenceMessage, lastSuccessfulUpdateCheck);
this.currentVersion = updateChecker.getCurrentVersion();
this.updateAvailable = updateChecker.updateAvailableProperty();
this.formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.getDefault());
@ -126,6 +126,7 @@ public class UpdatesPreferencesController implements FxController {
public StringBinding lastUpdateCheckMessageProperty() {
return lastUpdateCheckMessage;
}
public String getLastUpdateCheckMessage() {
Instant lastCheck = lastSuccessfulUpdateCheck.getValue();
if (lastCheck != null && !lastCheck.equals(Settings.DEFAULT_TIMESTAMP)) {
@ -135,14 +136,17 @@ public class UpdatesPreferencesController implements FxController {
}
}
private String updateTimeDifferenceMessage(Instant lastSuccessCheck) {
if (lastSuccessCheck.equals(Settings.DEFAULT_TIMESTAMP)) {
return resourceBundle.getString("preferences.updates.lastUpdateCheck.never");
}
public ObservableValue<String> timeDifferenceMessageProperty() {
return timeDifferenceMessage;
}
public String getTimeDifferenceMessage() {
var lastSuccessCheck = lastSuccessfulUpdateCheck.getValue();
var duration = Duration.between(lastSuccessCheck, Instant.now());
var hours = duration.toHours();
if (hours < 1) {
if (lastSuccessCheck.equals(Settings.DEFAULT_TIMESTAMP)) {
return resourceBundle.getString("preferences.updates.lastUpdateCheck.never");
} else if (hours < 1) {
return resourceBundle.getString("preferences.updates.lastUpdateCheck.recently");
} else if (hours < 24) {
return String.format(resourceBundle.getString("preferences.updates.lastUpdateCheck.hoursAgo"), hours);
@ -151,15 +155,6 @@ public class UpdatesPreferencesController implements FxController {
}
}
public ObservableValue<String> timeDifferenceMessageProperty() {
return timeDifferenceMessage;
}
public String getTimeDifferenceMessage() {
return timeDifferenceMessage.getValue();
}
public BooleanProperty upToDateLabelVisibleProperty() {
return upToDateLabelVisible;
}
@ -168,7 +163,7 @@ public class UpdatesPreferencesController implements FxController {
return upToDateLabelVisible.get();
}
public ObservableValue<Boolean> updateAvailableProperty() {
public BooleanBinding updateAvailableProperty() {
return updateAvailable;
}
@ -176,7 +171,7 @@ public class UpdatesPreferencesController implements FxController {
return updateAvailable.getValue();
}
public ObservableValue<Boolean> checkFailedProperty() {
public BooleanBinding checkFailedProperty() {
return checkFailed;
}