From 46c7d8076008cdb78e7ce654a6ac4a8dbc5fb2c0 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 20 Oct 2020 02:54:32 +0200 Subject: [PATCH] Add settings --- src/main/java/it/cavallium/App.java | 11 +++- .../it/cavallium/SecondaryController.java | 13 ++++ .../java/it/cavallium/SettingsService.java | 9 +++ .../it/cavallium/SettingsServiceImpl.java | 60 +++++++++++++++++++ .../it/cavallium/TransferServiceImpl.java | 1 + src/main/resources/it/cavallium/settings.fxml | 24 ++++++-- 6 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 src/main/java/it/cavallium/SettingsService.java create mode 100644 src/main/java/it/cavallium/SettingsServiceImpl.java diff --git a/src/main/java/it/cavallium/App.java b/src/main/java/it/cavallium/App.java index 8bd7844..8aa5fe5 100644 --- a/src/main/java/it/cavallium/App.java +++ b/src/main/java/it/cavallium/App.java @@ -22,6 +22,7 @@ public class App extends Application { public static final String VERSION = "1.0.0"; private static TdClusterManager clusterManager; + private static SettingsService settingsService; private static TransferService transferService; private static LogService logService; @@ -32,6 +33,8 @@ public class App extends Application { Init.start(); clusterManager = new TdClusterManager(null, null, Vertx.vertx(), null); + settingsService = new SettingsServiceImpl(); + logService = new LogServiceImpl(); logService.append(Level.INFO, "Initializing"); @@ -65,11 +68,15 @@ public class App extends Application { launch(); } - public static TransferService getTransferService() { - return transferService; + public static SettingsService getSettingsService() { + return settingsService; } public static LogService getLogService() { return logService; } + + public static TransferService getTransferService() { + return transferService; + } } \ No newline at end of file diff --git a/src/main/java/it/cavallium/SecondaryController.java b/src/main/java/it/cavallium/SecondaryController.java index bd667e9..a6461cd 100644 --- a/src/main/java/it/cavallium/SecondaryController.java +++ b/src/main/java/it/cavallium/SecondaryController.java @@ -1,12 +1,25 @@ package it.cavallium; import java.io.IOException; +import java.time.Duration; import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Spinner; public class SecondaryController { + public Button closeButton; + public Spinner delaySeconds; + + @FXML + protected void initialize() { + delaySeconds.getValueFactory().setValue((int) App.getSettingsService().getDelayBetweenAdds().toSeconds()); + } + @FXML private void switchToPrimary() throws IOException { + var delaySecondsValue = Duration.ofSeconds(Math.max (0, (int) delaySeconds.getValueFactory().getValue())); + App.getSettingsService().setDelayBetweenAdds(delaySecondsValue); App.setRoot("primary"); } } \ No newline at end of file diff --git a/src/main/java/it/cavallium/SettingsService.java b/src/main/java/it/cavallium/SettingsService.java new file mode 100644 index 0000000..5854d7b --- /dev/null +++ b/src/main/java/it/cavallium/SettingsService.java @@ -0,0 +1,9 @@ +package it.cavallium; + +import java.time.Duration; + +public interface SettingsService { + void setDelayBetweenAdds(Duration duration); + + Duration getDelayBetweenAdds(); +} diff --git a/src/main/java/it/cavallium/SettingsServiceImpl.java b/src/main/java/it/cavallium/SettingsServiceImpl.java new file mode 100644 index 0000000..9206b6b --- /dev/null +++ b/src/main/java/it/cavallium/SettingsServiceImpl.java @@ -0,0 +1,60 @@ +package it.cavallium; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.time.Duration; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; +import org.slf4j.event.Level; + +public class SettingsServiceImpl implements SettingsService { + + private static final Gson gson = new Gson(); + private final AtomicReference delayBetweenAdds = new AtomicReference<>(Duration.ZERO); + + public SettingsServiceImpl() { + synchronized (gson) { + if (Files.exists(Paths.get("settings.json"))) { + try { + var settings = gson.fromJson(Files.readString(Paths.get("settings.json"), StandardCharsets.UTF_8), JsonObject.class); + if (settings != null) { + Optional.ofNullable(settings.getAsJsonPrimitive("delayBetweenAdds")).ifPresent(seconds -> { + this.delayBetweenAdds.set(Duration.ofSeconds(seconds.getAsInt())); + }); + } + } catch (IOException e) { + e.printStackTrace(); + }; + } + } + } + + private void saveSettings() { + synchronized (gson) { + var settings = new JsonObject(); + settings.addProperty("delayBetweenAdds", delayBetweenAdds.get().toSeconds()); + try { + Files.writeString(Paths.get("settings.json"), gson.toJson(settings), StandardCharsets.UTF_8, StandardOpenOption.CREATE); + } catch (IOException e) { + App.getLogService().append(Level.ERROR, e.getLocalizedMessage()); + } + } + } + + @Override + public void setDelayBetweenAdds(Duration duration) { + delayBetweenAdds.set(duration); + saveSettings(); + } + + @Override + public Duration getDelayBetweenAdds() { + return delayBetweenAdds.get(); + } +} diff --git a/src/main/java/it/cavallium/TransferServiceImpl.java b/src/main/java/it/cavallium/TransferServiceImpl.java index f3e46d5..df2d8fc 100644 --- a/src/main/java/it/cavallium/TransferServiceImpl.java +++ b/src/main/java/it/cavallium/TransferServiceImpl.java @@ -537,6 +537,7 @@ public class TransferServiceImpl implements TransferService { return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.DONE, "")).then(Mono.empty()).thenReturn(user); }); }) + .delayElements(App.getSettingsService().getDelayBetweenAdds()) .collect(Collectors.toSet()) .map(resolvedUsers -> Tuple2.of(clients, resolvedUsers)); }) diff --git a/src/main/resources/it/cavallium/settings.fxml b/src/main/resources/it/cavallium/settings.fxml index c59d94b..3e7f83f 100644 --- a/src/main/resources/it/cavallium/settings.fxml +++ b/src/main/resources/it/cavallium/settings.fxml @@ -1,14 +1,26 @@ - - - + + + + + - + -