From c67d5ff663b876a15a3831f5a2b564c4002e99b6 Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 15 Oct 2026 15:18:30 +0300 Subject: ui: add chat drawer to the desktop client 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. --- .../src/main/java/market/guess/ui/client/App.java | 17 +++++++++++++- .../java/market/guess/ui/client/ServerClient.java | 27 +++++++++++++++++++++- .../market/guess/ui/client/RemoteContextsTest.java | 24 ++++++++++++++++++- 3 files changed, 65 insertions(+), 3 deletions(-) (limited to 'ui-client') 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; import java.io.IOException; import java.io.UncheckedIOException; +import java.util.concurrent.CompletableFuture; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; +import market.guess.model.chat.ChatMessageDTO; import market.guess.ui.desktop.AppView; public final class App extends Application { @@ -47,7 +49,20 @@ public final class App extends Application { new RemoteGuessMarketContext(client), new RemoteAccountContext(client), user); - client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); + var state = view.getState(); + state.setChatSender( + text -> + CompletableFuture.runAsync( + () -> { + var sent = client.say(text); + if (!sent.isSuccess()) { + var note = new ChatMessageDTO(0, "", "Not sent", sent.getMessage()); + Platform.runLater(() -> state.getChat().add(note)); + } + })); + client.startPolling( + () -> Platform.runLater(state::refreshData), + said -> Platform.runLater(() -> state.getChat().addAll(said))); stage.show(); } 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; import com.google.gson.Gson; import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.lang.reflect.Type; import java.util.HashMap; @@ -11,7 +12,9 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import market.guess.api.Result; +import market.guess.model.chat.ChatMessageDTO; import market.guess.model.event.EventDetailDTO; import market.guess.model.event.EventSummaryDTO; import market.guess.model.ledger.LedgerDTO; @@ -51,6 +54,7 @@ public final class ServerClient implements AutoCloseable { return t; }); private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null); + private long lastChatId; // touched only under the pullChat lock public ServerClient(String apiUrl) { this.api = HttpUrl.get(apiUrl); @@ -115,13 +119,34 @@ public final class ServerClient implements AutoCloseable { return true; } - public void startPolling(Runnable onChange) { + public synchronized List pullChat() { + var url = url("chat").newBuilder().addQueryParameter("since", "" + lastChatId).build(); + Result> res = + call( + new Request.Builder().url(url).build(), + new TypeToken>() {}.getType()); + if (!res.isSuccess() || res.getData().isEmpty()) { + return List.of(); + } + lastChatId = res.getData().getLast().id(); + return res.getData(); + } + + public Result say(String text) { + return post(ChatMessageDTO.class, new FormBody.Builder().add("text", text).build(), "chat"); + } + + public void startPolling(Runnable onChange, Consumer> onChat) { poller.scheduleWithFixedDelay( () -> { try { if (pull()) { onChange.run(); } + var said = pullChat(); + if (!said.isEmpty()) { + onChat.accept(said); + } } catch (RuntimeException e) { } }, 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 { maya.pull(); var heard = new CountDownLatch(1); - maya.startPolling(heard::countDown); + maya.startPolling(heard::countDown, said -> {}); var bought = tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); assertTrue(bought.isSuccess(), bought.getMessage()); @@ -149,6 +149,28 @@ class RemoteContextsTest { return new BigDecimal(new RemoteAccountContext(client).getAccount(name).getData().balance()); } + @Test + void chatReachesEveryoneOnceAndLeavesTheMarketAlone() { + try (var yael = new ServerClient(server.api); + var zohar = new ServerClient(server.api)) { + yael.login("Yael"); + zohar.login("Zohar"); + zohar.pullChat(); // whatever other tests said + zohar.pull(); + + var hello = "שלום, anyone trading?"; + assertTrue(yael.say(hello).isSuccess()); + assertFalse(yael.say(" ").isSuccess()); + + var heard = zohar.pullChat(); + assertEquals(1, heard.size()); + assertEquals("Yael", heard.get(0).sender()); + assertEquals(hello, heard.get(0).text()); + assertTrue(zohar.pullChat().isEmpty(), "a message is delivered once"); + assertFalse(zohar.pull(), "chat must not look like a market change"); + } + } + @Test void anUnreachableServerIsAFailedResultNotAnException() { try (var nobody = new ServerClient("http://localhost:1/api")) { -- cgit v1.2.3