-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathHelloModel.java
More file actions
78 lines (57 loc) · 1.8 KB
/
HelloModel.java
File metadata and controls
78 lines (57 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.*;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;
/**
* Model layer: encapsulates application data and business logic.
*/
public class HelloModel {
/**
* Returns a greeting based on the current Java and JavaFX versions.
*/
private final NtfyConnection connection;
private final StringProperty messageToSend = new SimpleStringProperty("");
private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList();
private final String clientId = UUID.randomUUID().toString();
//Konstuktor för prod
public HelloModel() {
this.connection = new NtfyConnectionImpl();
receiveMessage();
}
//Konstuktor för test
public HelloModel(NtfyConnection connection) {
this.connection = connection;
receiveMessage();
}
public ObservableList<NtfyMessageDto> getMessages() {
return messages;
}
public void setMessageToSend(String message) {
messageToSend.set(message);
}
public void sendMessage() {
String text = messageToSend.get().trim();
if (text.isEmpty()) return;
connection.send(text);
messageToSend.set("");
}
public void sendFile(File file) {
try {
connection.sendFile(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
public void receiveMessage() {
connection.receive(m ->
Platform.runLater(() -> messages.add(m))
);
}
}