aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ui-client/src/main/java/market/guess/ui/client/App.java48
-rw-r--r--ui-client/src/main/java/market/guess/ui/client/LoginController.java69
-rw-r--r--ui-client/src/main/resources/market/guess/ui/client/login.fxml39
3 files changed, 142 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 @@
1package market.guess.ui.client; 1package market.guess.ui.client;
2 2
3import java.io.IOException;
4import java.io.UncheckedIOException;
3import javafx.application.Application; 5import javafx.application.Application;
4import javafx.application.Platform; 6import javafx.application.Platform;
7import javafx.fxml.FXMLLoader;
8import javafx.scene.Scene;
5import javafx.stage.Stage; 9import javafx.stage.Stage;
6import market.guess.ui.desktop.AppView; 10import 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 @@
1package market.guess.ui.client;
2
3import java.util.concurrent.CompletableFuture;
4import java.util.function.BiConsumer;
5import javafx.application.Platform;
6import javafx.fxml.FXML;
7import javafx.scene.control.Button;
8import javafx.scene.control.Label;
9import javafx.scene.control.TextField;
10import market.guess.api.Result;
11
12public 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}
diff --git a/ui-client/src/main/resources/market/guess/ui/client/login.fxml b/ui-client/src/main/resources/market/guess/ui/client/login.fxml
new file mode 100644
index 0000000..db4cd1d
--- /dev/null
+++ b/ui-client/src/main/resources/market/guess/ui/client/login.fxml
@@ -0,0 +1,39 @@
1<?xml version="1.0" encoding="UTF-8"?>
2
3<?import javafx.geometry.Insets?>
4<?import javafx.scene.control.Button?>
5<?import javafx.scene.control.Label?>
6<?import javafx.scene.control.TextField?>
7<?import javafx.scene.layout.StackPane?>
8<?import javafx.scene.layout.VBox?>
9
10<StackPane styleClass="gm-root, gm-chrome" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="market.guess.ui.client.LoginController">
11 <padding>
12 <Insets bottom="24.0" left="24.0" right="24.0" top="24.0" />
13 </padding>
14 <children>
15 <VBox maxHeight="-Infinity" maxWidth="-Infinity" prefWidth="380.0" spacing="8.0" styleClass="gm-card">
16 <padding>
17 <Insets bottom="20.0" left="22.0" right="22.0" top="20.0" />
18 </padding>
19 <children>
20 <Label styleClass="gm-text-xl, gm-font-bold, gm-text-ink" text="Guess Market" />
21 <Label styleClass="gm-text-base, gm-font-regular, gm-text-ink2" text="Sign in to a market server" />
22 <Label styleClass="gm-text-base, gm-font-medium, gm-text-ink2" text="Server">
23 <VBox.margin>
24 <Insets top="8.0" />
25 </VBox.margin>
26 </Label>
27 <TextField fx:id="serverField" onAction="#handleLogin" styleClass="gm-textfield, gm-textfield-mono" />
28 <Label styleClass="gm-text-base, gm-font-medium, gm-text-ink2" text="User name" />
29 <TextField fx:id="userField" onAction="#handleLogin" styleClass="gm-textfield" />
30 <Label fx:id="errorLabel" managed="false" styleClass="gm-text-base, gm-font-medium, gm-text-no" visible="false" wrapText="true" />
31 <Button fx:id="loginBtn" defaultButton="true" maxWidth="1.7976931348623157E308" onAction="#handleLogin" styleClass="gm-btn, gm-btn-primary" text="Log in">
32 <VBox.margin>
33 <Insets top="8.0" />
34 </VBox.margin>
35 </Button>
36 </children>
37 </VBox>
38 </children>
39</StackPane>