aboutsummaryrefslogtreecommitdiffstats
path: root/ui-client
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-15 15:18:30 +0300
committerKostya <mail@sartin.in>2026-10-15 15:18:55 +0300
commitc67d5ff663b876a15a3831f5a2b564c4002e99b6 (patch)
tree98dcbbd1b8b21c5e88865ae0ebd8b270ff80d9a9 /ui-client
parent4563c9d6d6c1576b6fb45899c2b6d6073966d8b8 (diff)
downloadguess-market-3.0.tar.gz
guess-market-3.0.tar.xz
guess-market-3.0.zip
ui: add chat drawer to the desktop client 3.0
ServerClient fetches new chat lines on the same one-second poll as the market and hands them over separately from market changes. The shared UI gets a chat icon in the status bar, beside the eject button, shown only against a server. It opens a drawer above the status bar with everyone's lines and a field to send one, laid over the content so the window keeps its size. While the drawer is closed the icon counts unread lines. A line the server refuses is reported in the drawer itself.
Diffstat (limited to 'ui-client')
-rw-r--r--ui-client/src/main/java/market/guess/ui/client/App.java17
-rw-r--r--ui-client/src/main/java/market/guess/ui/client/ServerClient.java27
-rw-r--r--ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java24
3 files changed, 65 insertions, 3 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 4ba9600..93ae414 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
@@ -2,11 +2,13 @@ package market.guess.ui.client;
2 2
3import java.io.IOException; 3import java.io.IOException;
4import java.io.UncheckedIOException; 4import java.io.UncheckedIOException;
5import java.util.concurrent.CompletableFuture;
5import javafx.application.Application; 6import javafx.application.Application;
6import javafx.application.Platform; 7import javafx.application.Platform;
7import javafx.fxml.FXMLLoader; 8import javafx.fxml.FXMLLoader;
8import javafx.scene.Scene; 9import javafx.scene.Scene;
9import javafx.stage.Stage; 10import javafx.stage.Stage;
11import market.guess.model.chat.ChatMessageDTO;
10import market.guess.ui.desktop.AppView; 12import market.guess.ui.desktop.AppView;
11 13
12public final class App extends Application { 14public final class App extends Application {
@@ -47,7 +49,20 @@ public final class App extends Application {
47 new RemoteGuessMarketContext(client), 49 new RemoteGuessMarketContext(client),
48 new RemoteAccountContext(client), 50 new RemoteAccountContext(client),
49 user); 51 user);
50 client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); 52 var state = view.getState();
53 state.setChatSender(
54 text ->
55 CompletableFuture.runAsync(
56 () -> {
57 var sent = client.say(text);
58 if (!sent.isSuccess()) {
59 var note = new ChatMessageDTO(0, "", "Not sent", sent.getMessage());
60 Platform.runLater(() -> state.getChat().add(note));
61 }
62 }));
63 client.startPolling(
64 () -> Platform.runLater(state::refreshData),
65 said -> Platform.runLater(() -> state.getChat().addAll(said)));
51 stage.show(); 66 stage.show();
52 } 67 }
53 68
diff --git a/ui-client/src/main/java/market/guess/ui/client/ServerClient.java b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java
index 01962a2..00f3d68 100644
--- a/ui-client/src/main/java/market/guess/ui/client/ServerClient.java
+++ b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java
@@ -2,6 +2,7 @@ package market.guess.ui.client;
2 2
3import com.google.gson.Gson; 3import com.google.gson.Gson;
4import com.google.gson.JsonObject; 4import com.google.gson.JsonObject;
5import com.google.gson.reflect.TypeToken;
5import java.io.IOException; 6import java.io.IOException;
6import java.lang.reflect.Type; 7import java.lang.reflect.Type;
7import java.util.HashMap; 8import java.util.HashMap;
@@ -11,7 +12,9 @@ import java.util.concurrent.ConcurrentHashMap;
11import java.util.concurrent.Executors; 12import java.util.concurrent.Executors;
12import java.util.concurrent.ScheduledExecutorService; 13import java.util.concurrent.ScheduledExecutorService;
13import java.util.concurrent.TimeUnit; 14import java.util.concurrent.TimeUnit;
15import java.util.function.Consumer;
14import market.guess.api.Result; 16import market.guess.api.Result;
17import market.guess.model.chat.ChatMessageDTO;
15import market.guess.model.event.EventDetailDTO; 18import market.guess.model.event.EventDetailDTO;
16import market.guess.model.event.EventSummaryDTO; 19import market.guess.model.event.EventSummaryDTO;
17import market.guess.model.ledger.LedgerDTO; 20import market.guess.model.ledger.LedgerDTO;
@@ -51,6 +54,7 @@ public final class ServerClient implements AutoCloseable {
51 return t; 54 return t;
52 }); 55 });
53 private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null); 56 private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null);
57 private long lastChatId; // touched only under the pullChat lock
54 58
55 public ServerClient(String apiUrl) { 59 public ServerClient(String apiUrl) {
56 this.api = HttpUrl.get(apiUrl); 60 this.api = HttpUrl.get(apiUrl);
@@ -115,13 +119,34 @@ public final class ServerClient implements AutoCloseable {
115 return true; 119 return true;
116 } 120 }
117 121
118 public void startPolling(Runnable onChange) { 122 public synchronized List<ChatMessageDTO> pullChat() {
123 var url = url("chat").newBuilder().addQueryParameter("since", "" + lastChatId).build();
124 Result<List<ChatMessageDTO>> res =
125 call(
126 new Request.Builder().url(url).build(),
127 new TypeToken<List<ChatMessageDTO>>() {}.getType());
128 if (!res.isSuccess() || res.getData().isEmpty()) {
129 return List.of();
130 }
131 lastChatId = res.getData().getLast().id();
132 return res.getData();
133 }
134
135 public Result<ChatMessageDTO> say(String text) {
136 return post(ChatMessageDTO.class, new FormBody.Builder().add("text", text).build(), "chat");
137 }
138
139 public void startPolling(Runnable onChange, Consumer<List<ChatMessageDTO>> onChat) {
119 poller.scheduleWithFixedDelay( 140 poller.scheduleWithFixedDelay(
120 () -> { 141 () -> {
121 try { 142 try {
122 if (pull()) { 143 if (pull()) {
123 onChange.run(); 144 onChange.run();
124 } 145 }
146 var said = pullChat();
147 if (!said.isEmpty()) {
148 onChat.accept(said);
149 }
125 } catch (RuntimeException e) { 150 } catch (RuntimeException e) {
126 } 151 }
127 }, 152 },
diff --git a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java
index e3b11f0..ea9d5ea 100644
--- a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java
+++ b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java
@@ -72,7 +72,7 @@ class RemoteContextsTest {
72 72
73 maya.pull(); 73 maya.pull();
74 var heard = new CountDownLatch(1); 74 var heard = new CountDownLatch(1);
75 maya.startPolling(heard::countDown); 75 maya.startPolling(heard::countDown, said -> {});
76 var bought = 76 var bought =
77 tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); 77 tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10);
78 assertTrue(bought.isSuccess(), bought.getMessage()); 78 assertTrue(bought.isSuccess(), bought.getMessage());
@@ -150,6 +150,28 @@ class RemoteContextsTest {
150 } 150 }
151 151
152 @Test 152 @Test
153 void chatReachesEveryoneOnceAndLeavesTheMarketAlone() {
154 try (var yael = new ServerClient(server.api);
155 var zohar = new ServerClient(server.api)) {
156 yael.login("Yael");
157 zohar.login("Zohar");
158 zohar.pullChat(); // whatever other tests said
159 zohar.pull();
160
161 var hello = "שלום, anyone trading?";
162 assertTrue(yael.say(hello).isSuccess());
163 assertFalse(yael.say(" ").isSuccess());
164
165 var heard = zohar.pullChat();
166 assertEquals(1, heard.size());
167 assertEquals("Yael", heard.get(0).sender());
168 assertEquals(hello, heard.get(0).text());
169 assertTrue(zohar.pullChat().isEmpty(), "a message is delivered once");
170 assertFalse(zohar.pull(), "chat must not look like a market change");
171 }
172 }
173
174 @Test
153 void anUnreachableServerIsAFailedResultNotAnException() { 175 void anUnreachableServerIsAFailedResultNotAnException() {
154 try (var nobody = new ServerClient("http://localhost:1/api")) { 176 try (var nobody = new ServerClient("http://localhost:1/api")) {
155 assertFalse(nobody.login("Zed").isSuccess()); 177 assertFalse(nobody.login("Zed").isSuccess());