/
rgd045
/
crypt-file
Обзор
Документация
Войти
/
rgd045
/
crypt-file
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java
94 строки
3 KB
Armin Schrenk
simplify app version code
28 июл 2022, 11:58
Не верифицирован
28 июл 2022, 11:58
80b5b6a
Код
Авторство
О чём код?
package org.cryptomator.ui.fxapp; import org.cryptomator.common.Environment; import org.cryptomator.common.settings.Settings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import javax.inject.Named; import javafx.beans.binding.BooleanBinding; import javafx.beans.property.ReadOnlyStringProperty; import javafx.beans.property.StringProperty; import javafx.concurrent.ScheduledService; import javafx.concurrent.Worker; import javafx.concurrent.WorkerStateEvent; import javafx.util.Duration; import java.util.Comparator; @FxApplicationScoped public class UpdateChecker { private static final Logger LOG = LoggerFactory.getLogger(UpdateChecker.class); private static final Duration AUTOCHECK_DELAY = Duration.seconds(5); private final Settings settings; private final String currentVersion; private final StringProperty latestVersionProperty; private final Comparator<String> semVerComparator; private final ScheduledService<String> updateCheckerService; @Inject UpdateChecker(Settings settings, Environment env, @Named("latestVersion") StringProperty latestVersionProperty, @Named("SemVer") Comparator<String> semVerComparator, ScheduledService<String> updateCheckerService) { this.settings = settings; this.latestVersionProperty = latestVersionProperty; this.semVerComparator = semVerComparator; this.updateCheckerService = updateCheckerService; this.currentVersion = env.getAppVersion(); } public void automaticallyCheckForUpdatesIfEnabled() { if (settings.checkForUpdates().get()) { startCheckingForUpdates(AUTOCHECK_DELAY); } } public void checkForUpdatesNow() { startCheckingForUpdates(Duration.ZERO); } private void startCheckingForUpdates(Duration initialDelay) { updateCheckerService.cancel(); updateCheckerService.reset(); updateCheckerService.setDelay(initialDelay); updateCheckerService.setOnRunning(this::checkStarted); updateCheckerService.setOnSucceeded(this::checkSucceeded); updateCheckerService.setOnFailed(this::checkFailed); updateCheckerService.start(); } private void checkStarted(WorkerStateEvent event) { LOG.debug("Checking for updates..."); } private void checkSucceeded(WorkerStateEvent event) { String latestVersion = updateCheckerService.getValue(); LOG.info("Current version: {}, lastest version: {}", currentVersion, latestVersion); if (semVerComparator.compare(currentVersion, latestVersion) < 0) { // update is available latestVersionProperty.set(latestVersion); } else { latestVersionProperty.set(null); } } private void checkFailed(WorkerStateEvent event) { LOG.warn("Error checking for updates", event.getSource().getException()); } /* Observable Properties */ public BooleanBinding checkingForUpdatesProperty() { return updateCheckerService.stateProperty().isEqualTo(Worker.State.RUNNING); } public ReadOnlyStringProperty latestVersionProperty() { return latestVersionProperty; } public String getCurrentVersion() { return currentVersion; } }