Skip to content

Commit 065761a

Browse files
Add Javadoc comments for improved code clarity and maintainability.
1 parent 0afcc08 commit 065761a

4 files changed

Lines changed: 125 additions & 52 deletions

File tree

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

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,60 @@
11
package com.example;
22

33
import javafx.beans.binding.Bindings;
4-
import javafx.collections.FXCollections;
5-
import javafx.event.ActionEvent;
4+
65
import javafx.fxml.FXML;
7-
import javafx.geometry.Pos;
6+
87
import javafx.scene.control.*;
9-
import javafx.scene.layout.HBox;
10-
import javafx.scene.layout.Priority;
11-
import javafx.scene.text.TextFlow;
8+
129
import javafx.stage.FileChooser;
1310
import javafx.stage.Stage;
1411

1512
import java.io.File;
16-
import java.io.FileNotFoundException;
17-
import java.util.Arrays;
1813

14+
15+
16+
/**
17+
* Controller layer: Manages the user interface and mediates communication between the View (FXML) and the Model (HelloModel).
18+
* Handles user interactions such as sending messages, attaching files, and displaying chat content.
19+
*/
1920
public class HelloController {
2021

21-
// Uppdaterad för att använda den externa Ntfy-servern: https://ntfy.fungover.org
22-
// Topicen "mytopic" läggs till automatiskt av modellen.
23-
private final HelloModel model = new HelloModel(new NtfyConnectionImpl("https://ntfy.fungover.org"));
2422

23+
private static final String HOST_NAME = System.getenv("HOST_NAME");
24+
25+
26+
27+
28+
29+
/**
30+
* The model instance that holds application data and business logic.
31+
* Initializes connection to the specified Ntfy server.
32+
*/
33+
private final HelloModel model = new HelloModel(new NtfyConnectionImpl(HOST_NAME));
34+
35+
36+
/**
37+
* The ListView component displaying the list of chat messages.
38+
*/
2539
@FXML
2640
public ListView<NtfyMessageDto> chatListView;
41+
42+
/**
43+
* The label displaying the current fixed topic being used.
44+
*/
2745
@FXML
2846
public Label topicLabel;
2947

48+
/**
49+
* The label indicating the name of the file currently attached for sending.
50+
*/
3051
@FXML
3152
public Label attachedFileLabel;
3253

54+
55+
/**
56+
* The button used to send the message or the attached file.
57+
*/
3358
@FXML
3459
private Button sendButton;
3560

@@ -39,6 +64,11 @@ public class HelloController {
3964
@FXML
4065
private Button attachFile;
4166

67+
68+
/**
69+
* Initializes the controller. This method is called automatically by the FXML loader.
70+
* It sets up bindings between the view components and the model and configures the chat list view.
71+
*/
4272
@FXML
4373
private void initialize() {
4474
if (sendButton != null) {
@@ -95,8 +125,8 @@ private void initialize() {
95125
}
96126

97127
/**
98-
* En mycket enkel ListCell som enbart visar texten utan anpassad layout (bubblor/färger)
99-
* men hanterar att visa "[File Uploaded]" när meddelandetexten är tom.
128+
* A simple custom ListCell implementation for the chatListView.
129+
* It displays the message text or a placeholder for file uploads, and indicates if the message was sent locally.
100130
*/
101131
private static class SimpleMessageCell extends ListCell<NtfyMessageDto> {
102132

@@ -126,6 +156,11 @@ protected void updateItem(NtfyMessageDto item, boolean empty) {
126156
}
127157
}
128158

159+
160+
/**
161+
* Handles the action of the send button.
162+
* If a file is attached, it calls the model to send the file; otherwise, it calls the model to send the text message.
163+
*/
129164
@FXML
130165
protected void sendMessage() {
131166
if (model.fileToSendProperty().get() != null) {
@@ -141,6 +176,10 @@ protected void sendMessage() {
141176
}
142177
}
143178

179+
/**
180+
* Handles the action of the attach file button.
181+
* Opens a FileChooser dialog and sets the selected file in the model.
182+
*/
144183
@FXML
145184
protected void attachFile() {
146185
// Hämta scenen från en av kontrollerna

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

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
package com.example;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import io.github.cdimascio.dotenv.Dotenv;
3+
54
import javafx.application.Platform;
6-
import javafx.beans.InvalidationListener;
5+
76
import javafx.beans.property.ObjectProperty;
87
import javafx.beans.property.SimpleObjectProperty;
98
import javafx.beans.property.SimpleStringProperty;
109
import javafx.beans.property.StringProperty;
1110
import javafx.collections.FXCollections;
12-
import javafx.collections.ListChangeListener;
11+
1312
import javafx.collections.ObservableList;
1413

1514
import java.io.File;
16-
import java.io.FileNotFoundException;
17-
import java.io.IOException;
18-
import java.net.URI;
19-
import java.net.http.HttpClient;
20-
import java.net.http.HttpRequest;
21-
import java.net.http.HttpResponse;
22-
import java.time.LocalDate;
23-
import java.time.format.DateTimeFormatter;
15+
2416
import java.util.*;
25-
import java.util.concurrent.Executors;
26-
import java.util.concurrent.ScheduledExecutorService;
27-
import java.util.concurrent.TimeUnit;
17+
2818

2919
/**
3020
* Model layer: encapsulates application data and business logic.
21+
* Manages the current message, topic, list of received messages, and connection to Ntfy.
3122
*/
3223
public class HelloModel {
3324
private final NtfyConnection connection;
@@ -36,11 +27,18 @@ public class HelloModel {
3627
private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList();
3728
private final ObjectProperty<File> fileToSend = new SimpleObjectProperty<>(null); // Ny egenskap för filbilaga
3829

30+
/**
31+
* Initializes the model and establishes connection to the specified Ntfy server.
32+
* @param connection The Ntfy connection implementation to use (e.g., NtfyConnectionImpl or a Spy).
33+
*/
3934
public HelloModel(NtfyConnection connection) {
4035
this.connection = connection;
4136
connection.connect(currentTopic.get(), this::receiveMessage);
4237
}
43-
38+
/**
39+
* Returns a standard greeting string.
40+
* @return The greeting string.
41+
*/
4442
public String getGreeting() {
4543
return "Skicka meddelande";
4644
}
@@ -51,12 +49,14 @@ private static void runOnFx(Runnable task) {
5149
if (Platform.isFxApplicationThread()) task.run();
5250
else Platform.runLater(task);
5351
} catch (IllegalStateException notInitialized) {
54-
// JavaFX toolkit not initialized (e.g., unit tests or CI without graphics): run inline
5552
task.run();
5653
}
5754
}
5855

59-
56+
/**
57+
* Sends the current text message to the Ntfy server via the connection.
58+
* The message is added to the local list before sending.
59+
*/
6060
public void sendMessage() {
6161
String message = messageToSend.get();
6262
if (message != null && !message.trim().isEmpty()) {
@@ -82,7 +82,11 @@ public void sendMessage() {
8282
}
8383
}
8484

85-
// Argumentlös metod, som controlleren använder för att skicka den bifogade filen
85+
/**
86+
* Sends the currently attached file to the Ntfy server.
87+
* The file's name and a temporary message are added to the local list before sending.
88+
* The file attachment is cleared after sending.
89+
*/
8690
public void sendFile() {
8791
File file = fileToSend.get();
8892
if (file != null) {
@@ -108,46 +112,61 @@ public void sendFile() {
108112
}
109113
}
110114

111-
// Används av HelloController för att hämta filbilagan
115+
/**
116+
* Property for the file currently attached to be sent.
117+
* @return The ObjectProperty containing the File object, or null if no file is attached.
118+
*/
112119
public ObjectProperty<File> fileToSendProperty() {
113120
return fileToSend;
114121
}
115122

116-
// Används av HelloController för att ställa in filbilagan
123+
/**
124+
* Sets the file to be sent with the next message.
125+
* @param file The file to attach. Set to null to clear the attachment.
126+
*/
117127
public void setFileToSend(File file) {
118128
this.fileToSend.set(file);
119129
}
120130

131+
132+
/**
133+
* Handles an incoming message from the Ntfy connection and adds it to the message list on the FX thread.
134+
* @param message The received NtfyMessageDto.
135+
*/
121136
private void receiveMessage(NtfyMessageDto message) {
122-
// ANVÄNDER runOnFx FÖR ATT SÄKRA ATT UPPDATERINGEN SKER PÅ RÄTT TRÅD (eller direkt i testmiljö)
137+
123138
runOnFx(() -> messages.add(message));
124139
}
125140

141+
/**
142+
* Returns the observable list of messages received and sent.
143+
* @return The ObservableList of NtfyMessageDto objects.
144+
*/
126145
public ObservableList<NtfyMessageDto> getMessages() {
127146
return messages;
128147
}
129148

149+
/**
150+
* Property for the message currently being composed to send.
151+
* @return The StringProperty holding the message content.
152+
*/
130153
public StringProperty messageToSendProperty() {
131154
return messageToSend;
132155
}
133156

157+
/**
158+
* Property for the current Ntfy topic being subscribed to.
159+
* @return The StringProperty holding the current topic name.
160+
*/
134161
public StringProperty currentTopicProperty() {
135162
return currentTopic;
136163
}
137164

138-
public void reconnectToTopic(String newTopic) {
139-
if (!currentTopic.get().equals(newTopic)) {
140-
// connection.disconnect(currentTopic.get()); // Förutsätter att disconnect implementeras i NtfyConnection
141-
currentTopic.set(newTopic);
142-
143-
// Säkerställ att rensningen sker på FX-tråden om vi kör i en FX-miljö
144-
runOnFx(messages::clear);
145-
146-
connection.connect(newTopic, this::receiveMessage);
147-
}
148-
}
149165

150-
// KORRIGERAD: Denna metod måste ta en String för att matcha testet!
166+
/**
167+
* Sets the message content to send. Used by the controller for bidirectional binding.
168+
* @param message The new message content.
169+
*/
151170
public void setMessageToSend(String message) {
152171
this.messageToSend.set(message);
153172
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
import java.util.concurrent.CompletableFuture;
1616
import java.util.function.Consumer;
1717

18+
19+
/**
20+
* Implementation of NtfyConnection using Java's built-in HttpClient for both sending messages and subscribing to a topic.
21+
* Manages the HTTP connection logic for text messages, file uploads.
22+
*/
1823
public class NtfyConnectionImpl implements NtfyConnection {
1924

2025
private final HttpClient http = HttpClient.newHttpClient();
@@ -34,7 +39,10 @@ public NtfyConnectionImpl() {
3439
this.hostName = Objects.requireNonNull(dotenv.get("HOST_NAME"));
3540
}
3641

37-
42+
/**
43+
* Creates a new connection implementation.
44+
* hostName The base URL of the Ntfy server.
45+
*/
3846
public NtfyConnectionImpl(String hostName) {
3947
this.hostName = hostName;
4048
}
@@ -44,6 +52,11 @@ public String getTopic() {
4452
return currentTopic;
4553
}
4654

55+
56+
/**
57+
* Establishes a connection to the Ntfy topic to receive messages in real-time.
58+
* This method runs asynchronously in a dedicated thread.
59+
*/
4760
@Override
4861
public void connect(String newTopic, Consumer<NtfyMessageDto> messageHandler) {
4962

@@ -82,7 +95,9 @@ public void connect(String newTopic, Consumer<NtfyMessageDto> messageHandler) {
8295

8396

8497
/**
85-
* Skickar ett textmeddelande SYNKRONT (nödvändigt för integrationstester).
98+
* Sends a text message to the specified Ntfy topic.
99+
* @param message The text content of the message.
100+
* @param topic The Ntfy topic to send the message to.
86101
*/
87102
@Override
88103
public boolean send(String message, String topic) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public void connect(String topic, Consumer<NtfyMessageDto> messageHandler) {
4141

4242
@Override
4343
public void receive(Consumer<NtfyMessageDto> messageHandler) {
44-
// Denna metod används inte längre i HelloModel men behålls för kompatibilitet.
44+
4545
}
4646

4747

4848
@Override
49-
public boolean send(String message, String topic) { // RETURTYPEN ÄNDRAD TILL BOOLEAN
49+
public boolean send(String message, String topic) {
5050
// SPIONLOGIK: Spara det skickade meddelandet och ämnet
5151
this.lastSentMessage = message;
5252
this.lastSentTopic = topic;
@@ -74,7 +74,7 @@ public void simulateMessageReceived(NtfyMessageDto messageDto) {
7474
}
7575
}
7676

77-
// --- Getters för testverifieringar ---
77+
// --- Getters för testverifieringar Topic behålls för flexibilitet att skapa fler chattrum---
7878

7979
public String getLastSentMessage() {
8080
return lastSentMessage;

0 commit comments

Comments
 (0)