11package com .example ;
22
3- import com .fasterxml .jackson .databind .ObjectMapper ;
4- import io .github .cdimascio .dotenv .Dotenv ;
3+
54import javafx .application .Platform ;
6- import javafx . beans . InvalidationListener ;
5+
76import javafx .beans .property .ObjectProperty ;
87import javafx .beans .property .SimpleObjectProperty ;
98import javafx .beans .property .SimpleStringProperty ;
109import javafx .beans .property .StringProperty ;
1110import javafx .collections .FXCollections ;
12- import javafx . collections . ListChangeListener ;
11+
1312import javafx .collections .ObservableList ;
1413
1514import 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+
2416import 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 */
3223public 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 }
0 commit comments