|
2 | 2 |
|
3 | 3 | import io.github.cdimascio.dotenv.Dotenv; |
4 | 4 | import javafx.application.Platform; |
| 5 | +import javafx.beans.property.SimpleStringProperty; |
| 6 | +import javafx.beans.property.StringProperty; |
5 | 7 | import javafx.collections.FXCollections; |
6 | 8 | import javafx.collections.ObservableList; |
7 | 9 | import tools.jackson.databind.ObjectMapper; |
|
18 | 20 | */ |
19 | 21 | public class HelloModel { |
20 | 22 |
|
21 | | - private final String hostName; |
22 | | - private final HttpClient http = HttpClient.newHttpClient(); |
23 | | - private final ObjectMapper mapper = new ObjectMapper(); |
| 23 | + private final NtfyConnection connection; |
24 | 24 | private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList(); |
| 25 | + private final StringProperty messageToSend = new SimpleStringProperty(); |
25 | 26 |
|
26 | | - public HelloModel() { |
27 | | - Dotenv dotenv = Dotenv.load(); |
28 | | - hostName = Objects.requireNonNull(dotenv.get("HOST_NAME")); |
| 27 | + public HelloModel(NtfyConnection connection) { |
| 28 | + this.connection = connection; |
29 | 29 | receiveMessage(); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | public ObservableList<NtfyMessageDto> getMessages() { |
33 | 33 | return messages; |
34 | 34 | } |
35 | 35 |
|
| 36 | + public String getMessageToSend() { |
| 37 | + return messageToSend.get(); |
| 38 | + } |
| 39 | + public StringProperty messageToSendProperty() { |
| 40 | + return messageToSend; |
| 41 | + } |
| 42 | + |
| 43 | + public void setMessageToSend(String message) { |
| 44 | + messageToSend.set(message); |
| 45 | + } |
| 46 | + |
36 | 47 | public String getGreeting() { |
37 | 48 | String javaVersion = System.getProperty("java.version"); |
38 | 49 | String javafxVersion = System.getProperty("javafx.version"); |
39 | 50 | return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "."; |
40 | 51 | } |
41 | 52 |
|
42 | 53 | public void sendMessage() { |
43 | | - HttpRequest httpRequest = HttpRequest.newBuilder() |
44 | | - .POST(HttpRequest.BodyPublishers.ofString("Hello World!🚀")) |
45 | | - .uri(URI.create(hostName + "/mytopic")) |
46 | | - .build(); |
47 | | - |
48 | | - try { |
49 | | - //TODO: handle long blockings end request to not freeze the JavaFX thread |
50 | | - //1, Use thread send message |
51 | | - //2, Use async |
52 | | - var response = http.send(httpRequest, HttpResponse.BodyHandlers.ofString()); |
53 | | - } catch (IOException e) { |
54 | | - System.out.println("Error sending message"); |
55 | | - } catch (InterruptedException e) { |
56 | | - System.out.println("Interrupted sending message"); |
57 | | - } |
| 54 | + connection.send(messageToSend.get()); |
58 | 55 | } |
59 | 56 |
|
60 | 57 | public void receiveMessage() { |
61 | | - HttpRequest httpRequest = HttpRequest.newBuilder() |
62 | | - .GET() |
63 | | - .uri(URI.create(hostName + "/mytopic/json")) |
64 | | - .build(); |
65 | | - |
66 | | - http.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofLines()) |
67 | | - .thenAccept(response -> response.body() |
68 | | - .map( s -> mapper.readValue(s, NtfyMessageDto.class)) |
69 | | - .filter(message -> message.event().equals("message")) |
70 | | - .peek(System.out::println) |
71 | | - .forEach( s -> Platform.runLater(() -> messages.add(s)))); |
72 | | - |
| 58 | + connection.receive(m -> Platform.runLater(() -> messages.add(m))); |
73 | 59 | } |
74 | 60 | } |
0 commit comments