Skip to content

Commit e04e60b

Browse files
Refactor HelloModel to enhance async message/file sending logic and improve NtfyMessageDto constructors for clarity and consistency. Clean up unused dependencies and imports.
1 parent 065761a commit e04e60b

6 files changed

Lines changed: 57 additions & 65 deletions

File tree

pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
<version>${mockito.version}</version>
3636
<scope>test</scope>
3737
</dependency>
38-
<dependency>
39-
<groupId>tools.jackson.core</groupId>
40-
<artifactId>jackson-databind</artifactId>
41-
<version>3.0.1</version>
42-
</dependency>
43-
<dependency>
44-
<groupId>org.wiremock</groupId>
45-
<artifactId>wiremock</artifactId>
46-
<version>4.0.0-beta.15</version>
47-
<scope>test</scope>
48-
</dependency>
4938
<dependency>
5039
<groupId>org.openjfx</groupId>
5140
<artifactId>javafx-controls</artifactId>

src/main/java/com/example/HelloFX.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example;
22

3-
import io.github.cdimascio.dotenv.Dotenv;
3+
44
import javafx.application.Application;
55
import javafx.fxml.FXMLLoader;
66
import javafx.scene.Parent;

src/main/java/com/example/HelloModel.java

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
import java.io.File;
1515

16+
import java.io.FileNotFoundException;
17+
import java.io.IOException;
1618
import java.util.*;
19+
import java.util.concurrent.CompletableFuture;
1720

1821

1922
/**
@@ -54,32 +57,28 @@ private static void runOnFx(Runnable task) {
5457
}
5558

5659
/**
57-
* Sends the current text message to the Ntfy server via the connection.
58-
* The message is added to the local list before sending.
60+
* Sends the current text message asynchronously.
61+
* The network call is moved off the FX thread. UI updates happen via runOnFx.
5962
*/
6063
public void sendMessage() {
61-
String message = messageToSend.get();
62-
if (message != null && !message.trim().isEmpty()) {
63-
64-
// 1. Skapa den lokala DTO:n (med isLocal = true)
65-
NtfyMessageDto localMessage = new NtfyMessageDto(
66-
UUID.randomUUID().toString(),
67-
System.currentTimeMillis() / 1000L,
68-
"message",
69-
currentTopic.get(),
70-
message.trim(),
71-
true // Markera som lokalt skickat
72-
);
73-
74-
// 2. Lägg till i listan (UI-uppdatering) PÅ RÄTT TRÅD
75-
runOnFx(() -> messages.add(localMessage));
76-
77-
// 3. Skicka meddelandet via anslutningen
78-
connection.send(message, currentTopic.get());
79-
80-
// 4. Rensa meddelandefältet efter skickning
81-
messageToSend.set("");
64+
final String message = messageToSend.get();
65+
if (message == null || message.trim().isEmpty()) {
66+
System.err.println("Cannot send empty message.");
67+
return;
8268
}
69+
70+
// 1. Markera meddelandet som lokalt skickat och lägg till i listan (UI-uppdatering)
71+
final NtfyMessageDto localSentDto = new NtfyMessageDto(message, currentTopic.get(), "text", true);
72+
runOnFx(() -> {
73+
messages.add(localSentDto);
74+
messageToSend.set(""); // Rensa inmatningsfältet
75+
});
76+
77+
// 2. Utför nätverksanropet på en bakgrundstråd
78+
CompletableFuture.runAsync(() -> {
79+
// Nätverksanropet sker här i bakgrunden
80+
connection.send(message, currentTopic.get());
81+
});
8382
}
8483

8584
/**
@@ -89,27 +88,19 @@ public void sendMessage() {
8988
*/
9089
public void sendFile() {
9190
File file = fileToSend.get();
92-
if (file != null) {
93-
94-
// 1. Skapa den lokala DTO:n för fil (ofta med tom message)
95-
NtfyMessageDto localFileMessage = new NtfyMessageDto(
96-
UUID.randomUUID().toString(),
97-
System.currentTimeMillis() / 1000L,
98-
"file", // Använd "file" event om det är en fil
99-
currentTopic.get(),
100-
"Fil skickad: " + file.getName(), // Detta meddelande visas bara i logik, CellFactory hanterar visning
101-
true
102-
);
103-
104-
// 2. Lägg till i listan (UI-uppdatering) PÅ RÄTT TRÅD
105-
runOnFx(() -> messages.add(localFileMessage));
106-
107-
// 3. Skicka filen
108-
connection.sendFile(file, currentTopic.get());
91+
final NtfyMessageDto localSentDto = new NtfyMessageDto(file.getName(), currentTopic.get(), "file", true);
92+
runOnFx(() -> {
93+
messages.add(localSentDto);
94+
fileToSend.set(null); // Rensa filbilagan i UI
95+
});
10996

110-
// 4. Rensa filbilagan efter skickning
111-
fileToSend.set(null);
112-
}
97+
// 2. Utför nätverksanropet på en bakgrundstråd
98+
99+
// 2. Utför nätverksanropet på en bakgrundstråd
100+
CompletableFuture.runAsync(() -> {
101+
// Nätverksanropet sker här i bakgrunden
102+
connection.sendFile(file, currentTopic.get());
103+
});
113104
}
114105

115106
/**

src/main/java/com/example/NtfyConnection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.example;
22

33
import java.io.File;
4-
import java.io.FileNotFoundException;
4+
55
import java.util.function.Consumer;
66

77
public interface NtfyConnection {
@@ -14,9 +14,9 @@ public interface NtfyConnection {
1414

1515
void connect(String topic, Consumer<NtfyMessageDto> messageHandler);
1616

17-
//public boolean send(String message);
17+
1818

1919
public void receive(Consumer<NtfyMessageDto> messageHandler);
2020

21-
//public boolean sendFile(File file) throws FileNotFoundException;
21+
2222
}

src/main/java/com/example/NtfyMessageDto.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@
33

44

55

6-
public record NtfyMessageDto(String id, long time, String event, String topic, String message, boolean isLocal) {
6+
public record NtfyMessageDto(String id, long time, String event, String topic, String type, String message, boolean isLocal) {
77

8-
public NtfyMessageDto(String id, long time, String event, String topic, String message) {
9-
this(id, time, event, topic, message, false);
8+
// Sekundär konstruktor för mottagna meddelanden (isLocal = false)
9+
public NtfyMessageDto(String id, long time, String event, String topic, String type, String message) {
10+
this(id, time, event, topic, type, message, false);
1011
}
1112

12-
public NtfyMessageDto(String message) {
13-
this(null, 0, "message", null, message, true);
13+
/**
14+
* Konstruktor för att skapa ett lokalt skickat meddelande (som visas i listan innan bekräftelse).
15+
* @param message Den huvudsakliga meddelandetexten eller filnamnet.
16+
* @param topic Den aktuella topicen.
17+
* @param type Typ av innehåll ("text" eller "file").
18+
* @param isLocal Alltid true för lokalt skickade meddelanden.
19+
*/
20+
public NtfyMessageDto(String message, String topic, String type, boolean isLocal) {
21+
// Fix: Vi använder 0L för att explicit kasta 0 som en long
22+
this(null, 0L, "message", topic, type, message, isLocal);
1423
}
1524

25+
// Kort konstruktor för att bara skicka en meddelandetext (kanske inte används men fixas för konsekvens)
26+
public NtfyMessageDto(String message) {
27+
this(null, 0L, "message", null, "text", message, true);
28+
}
1629

1730
@Override
1831
public String toString(){
19-
return message;
32+
return message;
2033
}
2134

2235
}

src/test/java/com/example/NtfyConnectionSpy.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example;
22

33
import java.io.File;
4-
import java.io.FileNotFoundException;
54
import java.util.function.Consumer;
65

76
public class NtfyConnectionSpy implements NtfyConnection {

0 commit comments

Comments
 (0)