diff options
Diffstat (limited to 'ui-client/src/main/java')
| -rw-r--r-- | ui-client/src/main/java/market/guess/ui/client/App.java | 48 | ||||
| -rw-r--r-- | ui-client/src/main/java/market/guess/ui/client/LoginController.java | 69 |
2 files changed, 103 insertions, 14 deletions
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 @@ | |||
| 1 | package market.guess.ui.client; | 1 | package market.guess.ui.client; |
| 2 | 2 | ||
| 3 | import java.io.IOException; | ||
| 4 | import java.io.UncheckedIOException; | ||
| 3 | import javafx.application.Application; | 5 | import javafx.application.Application; |
| 4 | import javafx.application.Platform; | 6 | import javafx.application.Platform; |
| 7 | import javafx.fxml.FXMLLoader; | ||
| 8 | import javafx.scene.Scene; | ||
| 5 | import javafx.stage.Stage; | 9 | import javafx.stage.Stage; |
| 6 | import market.guess.ui.desktop.AppView; | 10 | import market.guess.ui.desktop.AppView; |
| 7 | 11 | ||
| @@ -9,33 +13,49 @@ public final class App extends Application { | |||
| 9 | private ServerClient client; | 13 | private ServerClient client; |
| 10 | 14 | ||
| 11 | @Override | 15 | @Override |
| 12 | public void start(Stage stage) { | 16 | public void start(Stage loginStage) { |
| 13 | var args = getParameters().getNamed(); | 17 | var args = getParameters().getNamed(); |
| 14 | client = | 18 | var loader = new FXMLLoader(App.class.getResource("login.fxml")); |
| 15 | new ServerClient( | 19 | try { |
| 16 | args.getOrDefault("server", "http://localhost:8080/guess-market") + "/api"); | 20 | var scene = new Scene(loader.load()); |
| 17 | var login = client.login(args.getOrDefault("user", System.getProperty("user.name"))); | 21 | scene.getStylesheets().add(AppView.class.getResource("theme.css").toExternalForm()); |
| 18 | if (!login.isSuccess()) { | 22 | loginStage.setScene(scene); |
| 19 | System.err.println(login.getMessage()); | 23 | } catch (IOException e) { |
| 20 | Platform.exit(); | 24 | throw new UncheckedIOException(e); |
| 21 | return; | ||
| 22 | } | 25 | } |
| 23 | client.pull(); | 26 | loader |
| 27 | .<LoginController>getController() | ||
| 28 | .init( | ||
| 29 | args.getOrDefault("server", "http://localhost:8080/guess-market"), | ||
| 30 | args.getOrDefault("user", ""), | ||
| 31 | (loggedIn, user) -> { | ||
| 32 | client = loggedIn; | ||
| 33 | openMarket(user); | ||
| 34 | loginStage.close(); | ||
| 35 | }); | ||
| 36 | loginStage.setTitle("Guess Market"); | ||
| 37 | loginStage.setResizable(false); | ||
| 38 | loginStage.show(); | ||
| 39 | } | ||
| 24 | 40 | ||
| 41 | private void openMarket(String user) { | ||
| 42 | var stage = new Stage(); | ||
| 25 | var view = | 43 | var view = |
| 26 | new AppView( | 44 | new AppView( |
| 27 | stage, | 45 | stage, |
| 28 | new RemoteCatalogContext(client), | 46 | new RemoteCatalogContext(client), |
| 29 | new RemoteGuessMarketContext(client), | 47 | new RemoteGuessMarketContext(client), |
| 30 | new RemoteAccountContext(client)); | 48 | new RemoteAccountContext(client), |
| 31 | // refreshData reselects the event and user by id, so a refresh keeps the selection | 49 | user); |
| 32 | client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); | 50 | client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); |
| 33 | stage.show(); | 51 | stage.show(); |
| 34 | } | 52 | } |
| 35 | 53 | ||
| 36 | @Override | 54 | @Override |
| 37 | public void stop() { | 55 | public void stop() { |
| 38 | client.logout(); | 56 | if (client != null) { |
| 39 | client.close(); | 57 | client.logout(); |
| 58 | client.close(); | ||
| 59 | } | ||
| 40 | } | 60 | } |
| 41 | } | 61 | } |
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 @@ | |||
| 1 | package market.guess.ui.client; | ||
| 2 | |||
| 3 | import java.util.concurrent.CompletableFuture; | ||
| 4 | import java.util.function.BiConsumer; | ||
| 5 | import javafx.application.Platform; | ||
| 6 | import javafx.fxml.FXML; | ||
| 7 | import javafx.scene.control.Button; | ||
| 8 | import javafx.scene.control.Label; | ||
| 9 | import javafx.scene.control.TextField; | ||
| 10 | import market.guess.api.Result; | ||
| 11 | |||
| 12 | public final class LoginController { | ||
| 13 | @FXML private TextField serverField; | ||
| 14 | @FXML private TextField userField; | ||
| 15 | @FXML private Label errorLabel; | ||
| 16 | @FXML private Button loginBtn; | ||
| 17 | |||
| 18 | private BiConsumer<ServerClient, String> onLoggedIn; | ||
| 19 | |||
| 20 | public void init(String server, String user, BiConsumer<ServerClient, String> onLoggedIn) { | ||
| 21 | serverField.setText(server); | ||
| 22 | userField.setText(user); | ||
| 23 | this.onLoggedIn = onLoggedIn; | ||
| 24 | Platform.runLater(userField::requestFocus); | ||
| 25 | } | ||
| 26 | |||
| 27 | @FXML | ||
| 28 | private void handleLogin() { | ||
| 29 | var server = serverField.getText().trim().replaceAll("/+$", ""); | ||
| 30 | var user = userField.getText(); | ||
| 31 | loginBtn.setDisable(true); | ||
| 32 | showError(null); | ||
| 33 | CompletableFuture.supplyAsync(() -> connect(server, user)) | ||
| 34 | .thenAcceptAsync( | ||
| 35 | attempt -> { | ||
| 36 | loginBtn.setDisable(false); | ||
| 37 | if (attempt.result().isSuccess()) { | ||
| 38 | onLoggedIn.accept(attempt.client(), attempt.result().getData()); | ||
| 39 | } else { | ||
| 40 | showError(attempt.result().getMessage()); | ||
| 41 | } | ||
| 42 | }, | ||
| 43 | Platform::runLater); | ||
| 44 | } | ||
| 45 | |||
| 46 | private record Attempt(ServerClient client, Result<String> result) {} | ||
| 47 | |||
| 48 | private static Attempt connect(String server, String user) { | ||
| 49 | ServerClient client; | ||
| 50 | try { | ||
| 51 | client = new ServerClient(server + "/api"); | ||
| 52 | } catch (IllegalArgumentException e) { | ||
| 53 | return new Attempt(null, Result.error("That is not a server address.")); | ||
| 54 | } | ||
| 55 | var result = client.login(user); | ||
| 56 | if (result.isSuccess()) { | ||
| 57 | client.pull(); | ||
| 58 | } else { | ||
| 59 | client.close(); | ||
| 60 | } | ||
| 61 | return new Attempt(client, result); | ||
| 62 | } | ||
| 63 | |||
| 64 | private void showError(String message) { | ||
| 65 | errorLabel.setText(message); | ||
| 66 | errorLabel.setVisible(message != null); | ||
| 67 | errorLabel.setManaged(message != null); | ||
| 68 | } | ||
| 69 | } | ||