Add settings

This commit is contained in:
Andrea Cavalli 2020-10-20 02:54:32 +02:00
parent a5b06b8d47
commit 46c7d80760
6 changed files with 110 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -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<Integer> 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");
}
}

View File

@ -0,0 +1,9 @@
package it.cavallium;
import java.time.Duration;
public interface SettingsService {
void setDelayBetweenAdds(Duration duration);
Duration getDelayBetweenAdds();
}

View File

@ -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<Duration> 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();
}
}

View File

@ -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));
})

View File

@ -1,14 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.cavallium.SecondaryController">
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.cavallium.SecondaryController">
<children>
<Label text="Secondary View" />
<Button fx:id="secondaryButton" text="Close" onAction="#switchToPrimary" />
<Label text="Settings" />
<HBox alignment="CENTER" spacing="10.0">
<children>
<Label text="Delay between adds (seconds)" />
<Spinner fx:id="delaySeconds" editable="true" prefWidth="100.0">
<valueFactory>
<javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory min="0" max="120" amountToStepBy="1"/>
</valueFactory>
</Spinner>
</children>
</HBox>
<Button fx:id="closeButton" onAction="#switchToPrimary" text="Close" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />