diff options
| author | Kostya <mail@sartin.in> | 2026-10-15 15:18:30 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-15 15:18:55 +0300 |
| commit | c67d5ff663b876a15a3831f5a2b564c4002e99b6 (patch) | |
| tree | 98dcbbd1b8b21c5e88865ae0ebd8b270ff80d9a9 /ui-client/src/main/java | |
| parent | 4563c9d6d6c1576b6fb45899c2b6d6073966d8b8 (diff) | |
| download | guess-market-c67d5ff663b876a15a3831f5a2b564c4002e99b6.tar.gz guess-market-c67d5ff663b876a15a3831f5a2b564c4002e99b6.tar.xz guess-market-c67d5ff663b876a15a3831f5a2b564c4002e99b6.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/src/main/java')
| -rw-r--r-- | ui-client/src/main/java/market/guess/ui/client/App.java | 17 | ||||
| -rw-r--r-- | ui-client/src/main/java/market/guess/ui/client/ServerClient.java | 27 |
2 files changed, 42 insertions, 2 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 | ||
| 3 | import java.io.IOException; | 3 | import java.io.IOException; |
| 4 | import java.io.UncheckedIOException; | 4 | import java.io.UncheckedIOException; |
| 5 | import java.util.concurrent.CompletableFuture; | ||
| 5 | import javafx.application.Application; | 6 | import javafx.application.Application; |
| 6 | import javafx.application.Platform; | 7 | import javafx.application.Platform; |
| 7 | import javafx.fxml.FXMLLoader; | 8 | import javafx.fxml.FXMLLoader; |
| 8 | import javafx.scene.Scene; | 9 | import javafx.scene.Scene; |
| 9 | import javafx.stage.Stage; | 10 | import javafx.stage.Stage; |
| 11 | import market.guess.model.chat.ChatMessageDTO; | ||
| 10 | import market.guess.ui.desktop.AppView; | 12 | import market.guess.ui.desktop.AppView; |
| 11 | 13 | ||
| 12 | public final class App extends Application { | 14 | public 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 | ||
| 3 | import com.google.gson.Gson; | 3 | import com.google.gson.Gson; |
| 4 | import com.google.gson.JsonObject; | 4 | import com.google.gson.JsonObject; |
| 5 | import com.google.gson.reflect.TypeToken; | ||
| 5 | import java.io.IOException; | 6 | import java.io.IOException; |
| 6 | import java.lang.reflect.Type; | 7 | import java.lang.reflect.Type; |
| 7 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| @@ -11,7 +12,9 @@ import java.util.concurrent.ConcurrentHashMap; | |||
| 11 | import java.util.concurrent.Executors; | 12 | import java.util.concurrent.Executors; |
| 12 | import java.util.concurrent.ScheduledExecutorService; | 13 | import java.util.concurrent.ScheduledExecutorService; |
| 13 | import java.util.concurrent.TimeUnit; | 14 | import java.util.concurrent.TimeUnit; |
| 15 | import java.util.function.Consumer; | ||
| 14 | import market.guess.api.Result; | 16 | import market.guess.api.Result; |
| 17 | import market.guess.model.chat.ChatMessageDTO; | ||
| 15 | import market.guess.model.event.EventDetailDTO; | 18 | import market.guess.model.event.EventDetailDTO; |
| 16 | import market.guess.model.event.EventSummaryDTO; | 19 | import market.guess.model.event.EventSummaryDTO; |
| 17 | import market.guess.model.ledger.LedgerDTO; | 20 | import 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 | }, |