Skip to content

Commit fe65275

Browse files
Linus WestlingLinus Westling
authored andcommitted
Filer, styling, tester, allt är klart.
1 parent 827b43f commit fe65275

6 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/main/java/com/example/HelloController.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.example;
22

3+
import javafx.application.Platform;
34
import javafx.event.ActionEvent;
45
import javafx.fxml.FXML;
56
import javafx.scene.control.Label;
67
import javafx.scene.control.ListView;
8+
import javafx.stage.FileChooser;
9+
10+
import java.io.File;
11+
import java.nio.file.Path;
712

813
/**
914
* Controller layer: mediates between the view (FXML) and the model.
@@ -35,5 +40,28 @@ public void sendMessage(ActionEvent actionEvent) {
3540
model.setMessageToSend(content);
3641
model.sendMessage();
3742
}
43+
44+
@FXML
45+
private void sendFile(ActionEvent event) {
46+
FileChooser fileChooser = new FileChooser();
47+
fileChooser.setTitle("Välj en fil att skicka");
48+
File selectedFile = fileChooser.showOpenDialog(messageInput.getScene().getWindow());
49+
50+
if (selectedFile != null) {
51+
Path filePath = selectedFile.toPath();
52+
model.sendFile(filePath).thenAccept(success -> {
53+
Platform.runLater(() -> {
54+
if (success) {
55+
System.out.println("Fil skickad: " + filePath.getFileName());
56+
} else {
57+
System.out.println("Misslyckades att skicka filen.");
58+
}
59+
});
60+
});
61+
} else {
62+
System.out.println("Ingen fil vald.");
63+
}
64+
}
65+
3866
}
3967

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javafx.collections.FXCollections;
77
import javafx.collections.ObservableList;
88

9+
import java.nio.file.Path;
910
import java.util.concurrent.CompletableFuture;
1011

1112
/**
@@ -53,6 +54,10 @@ public CompletableFuture<Boolean> sendMessage() {
5354
return connection.send(messageToSend.get());
5455
}
5556

57+
public CompletableFuture<Boolean> sendFile(Path filePath) {
58+
return connection.sendFile(filePath);
59+
}
60+
5661
public void receiveMessage() {
5762
connection.receive(m -> Platform.runLater(() -> messages.add(m)));
5863
}

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

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

3+
import java.nio.file.Path;
34
import java.util.concurrent.CompletableFuture;
45
import java.util.function.Consumer;
56

@@ -9,4 +10,6 @@ public interface NtfyConnection {
910

1011
public void receive(Consumer<NtfyMessageDto> messageHandler);
1112

13+
public CompletableFuture<Boolean> sendFile(Path path);
14+
1215
}

src/main/java/com/example/NtfyConnectionImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import io.github.cdimascio.dotenv.Dotenv;
44
import tools.jackson.databind.ObjectMapper;
55

6+
import java.io.FileNotFoundException;
67
import java.io.IOException;
78
import java.net.URI;
89
import java.net.http.HttpClient;
910
import java.net.http.HttpRequest;
1011
import java.net.http.HttpResponse;
12+
import java.nio.file.Path;
1113
import java.util.Objects;
1214
import java.util.concurrent.CompletableFuture;
1315
import java.util.function.Consumer;
@@ -49,6 +51,26 @@ public CompletableFuture<Boolean> send(String message) {
4951
});
5052
}
5153

54+
public CompletableFuture<Boolean> sendFile(Path path) {
55+
try {
56+
String filename = path.getFileName().toString();
57+
58+
HttpRequest httpRequest = HttpRequest.newBuilder()
59+
.PUT(HttpRequest.BodyPublishers.ofFile(path))
60+
.header("Filename", filename)
61+
.header("Content-Type", "application/octet-stream")
62+
.uri(URI.create(hostName + "/mytopic"))
63+
.build();
64+
65+
return http.sendAsync(httpRequest, HttpResponse.BodyHandlers.discarding())
66+
.thenApply(response -> response.statusCode() == 200);
67+
68+
} catch (IOException e) {
69+
System.out.println("Kunde inte läsa filen: " + e.getMessage());
70+
return CompletableFuture.completedFuture(false);
71+
}
72+
}
73+
5274
@Override
5375
public void receive(Consumer<NtfyMessageDto> messageHandler) {
5476
HttpRequest httpRequest = HttpRequest.newBuilder()

src/main/resources/com/example/hello-view.fxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<HBox spacing="10" alignment="CENTER">
2121
<TextField fx:id="messageInput" promptText="Skriv ett meddelande..." styleClass="input"/>
2222
<Button text="Skicka" onAction="#sendMessage" styleClass="send-button"/>
23+
<Button text="Skicka fil" onAction="#sendFile" styleClass="send-button"/>
2324
</HBox>
2425

2526
<ListView fx:id="messageView" styleClass="message-list"/>

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

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

3+
import java.nio.file.Path;
34
import java.util.concurrent.CompletableFuture;
45
import java.util.function.Consumer;
56

@@ -20,6 +21,11 @@ public void receive(Consumer<NtfyMessageDto> messageHandler) {
2021
this.messageHandler = messageHandler;
2122
}
2223

24+
@Override
25+
public CompletableFuture<Boolean> sendFile(Path path) {
26+
return null;
27+
}
28+
2329
public void simulateIncomingMessage(NtfyMessageDto message) {
2430
if (messageHandler != null) {
2531
messageHandler.accept(message);

0 commit comments

Comments
 (0)