From 4edb27d378e8b79931ef6a6abebfda66f894d538 Mon Sep 17 00:00:00 2001 From: Kostya Date: Tue, 13 Oct 2026 16:08:27 +0300 Subject: ui-client: add login window Ask for the server address and a user name before opening the market, and show the server's reason when it refuses (name taken, unreachable). The request runs off the JavaFX thread so a dead server cannot freeze the window. --server= and --user= now only prefill the form. --- .../src/main/java/market/guess/ui/client/App.java | 48 ++++++++++----- .../market/guess/ui/client/LoginController.java | 69 ++++++++++++++++++++++ 2 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 ui-client/src/main/java/market/guess/ui/client/LoginController.java (limited to 'ui-client/src/main/java') diff --git a/ui-client/src/main/java/market/guess/ui/client/App.java b/ui-client/src/main/java/market/guess/ui/client/App.java index d24eda1..4ba9600 100644 --- a/ui-client/src/main/java/market/guess/ui/client/App.java +++ b/ui-client/src/main/java/market/guess/ui/client/App.java @@ -1,7 +1,11 @@ package market.guess.ui.client; +import java.io.IOException; +import java.io.UncheckedIOException; import javafx.application.Application; import javafx.application.Platform; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; import javafx.stage.Stage; import market.guess.ui.desktop.AppView; @@ -9,33 +13,49 @@ public final class App extends Application { private ServerClient client; @Override - public void start(Stage stage) { + public void start(Stage loginStage) { var args = getParameters().getNamed(); - client = - new ServerClient( - args.getOrDefault("server", "http://localhost:8080/guess-market") + "/api"); - var login = client.login(args.getOrDefault("user", System.getProperty("user.name"))); - if (!login.isSuccess()) { - System.err.println(login.getMessage()); - Platform.exit(); - return; + var loader = new FXMLLoader(App.class.getResource("login.fxml")); + try { + var scene = new Scene(loader.load()); + scene.getStylesheets().add(AppView.class.getResource("theme.css").toExternalForm()); + loginStage.setScene(scene); + } catch (IOException e) { + throw new UncheckedIOException(e); } - client.pull(); + loader + .getController() + .init( + args.getOrDefault("server", "http://localhost:8080/guess-market"), + args.getOrDefault("user", ""), + (loggedIn, user) -> { + client = loggedIn; + openMarket(user); + loginStage.close(); + }); + loginStage.setTitle("Guess Market"); + loginStage.setResizable(false); + loginStage.show(); + } + private void openMarket(String user) { + var stage = new Stage(); var view = new AppView( stage, new RemoteCatalogContext(client), new RemoteGuessMarketContext(client), - new RemoteAccountContext(client)); - // refreshData reselects the event and user by id, so a refresh keeps the selection + new RemoteAccountContext(client), + user); client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); stage.show(); } @Override public void stop() { - client.logout(); - client.close(); + if (client != null) { + client.logout(); + client.close(); + } } } diff --git a/ui-client/src/main/java/market/guess/ui/client/LoginController.java b/ui-client/src/main/java/market/guess/ui/client/LoginController.java new file mode 100644 index 0000000..ae6c6f9 --- /dev/null +++ b/ui-client/src/main/java/market/guess/ui/client/LoginController.java @@ -0,0 +1,69 @@ +package market.guess.ui.client; + +import java.util.concurrent.CompletableFuture; +import java.util.function.BiConsumer; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import market.guess.api.Result; + +public final class LoginController { + @FXML private TextField serverField; + @FXML private TextField userField; + @FXML private Label errorLabel; + @FXML private Button loginBtn; + + private BiConsumer onLoggedIn; + + public void init(String server, String user, BiConsumer onLoggedIn) { + serverField.setText(server); + userField.setText(user); + this.onLoggedIn = onLoggedIn; + Platform.runLater(userField::requestFocus); + } + + @FXML + private void handleLogin() { + var server = serverField.getText().trim().replaceAll("/+$", ""); + var user = userField.getText(); + loginBtn.setDisable(true); + showError(null); + CompletableFuture.supplyAsync(() -> connect(server, user)) + .thenAcceptAsync( + attempt -> { + loginBtn.setDisable(false); + if (attempt.result().isSuccess()) { + onLoggedIn.accept(attempt.client(), attempt.result().getData()); + } else { + showError(attempt.result().getMessage()); + } + }, + Platform::runLater); + } + + private record Attempt(ServerClient client, Result result) {} + + private static Attempt connect(String server, String user) { + ServerClient client; + try { + client = new ServerClient(server + "/api"); + } catch (IllegalArgumentException e) { + return new Attempt(null, Result.error("That is not a server address.")); + } + var result = client.login(user); + if (result.isSuccess()) { + client.pull(); + } else { + client.close(); + } + return new Attempt(client, result); + } + + private void showError(String message) { + errorLabel.setText(message); + errorLabel.setVisible(message != null); + errorLabel.setManaged(message != null); + } +} -- cgit v1.2.3