This is a relatively simple splash screen that uses CompletableFuture
constructs and shows a strategy to show real initialization status by using callbacks. It took inspiration from this gist.
A YT video of this can be found here.
The basic idea on the callbacks is to have the data layer provide a Consumer
or BiConsumer
parameter so the Controller can apply updates on the screen depending on what's happening on the data layer. In this particular case, I am using vavr.io.Function3
because I wanted to callback accept 3 parameters, but the basic idea remains. The magic happens here:
Try.run(() -> catalog.loadImages((image, current, count) -> {
Platform.runLater(() -> {
buildInfo.setText("Build v" + ConfigurationManager.getVersionInfo().orElse("1.0"));
statusText.setText(String.format("Loading %d of %d images...", current, count));
});
return null;
}));
This sets the statusText
FXML field that's bound to the label that has the same fx:id
field.
Note the use of Platform::runLater
since the process itself can happen in another thread (or itself spawns more threads) and JavaFx only allows UI updates in its own thread.
Note that were using Google Guice here to create/inject the controller and all its dependencies. For more inforation on that see this StackOverflow post.