Add received messages to their chat
This commit is contained in:
		@@ -148,12 +148,22 @@ public abstract class LocalDB {
 | 
			
		||||
	 * Searches for a message by ID.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param id the ID of the message to search for
 | 
			
		||||
	 * @return the message with the corresponding ID, or {@code null} if no message
 | 
			
		||||
	 *         has been found
 | 
			
		||||
	 * @return an optional containing the message
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	public Message getMessage(long id) {
 | 
			
		||||
		return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null);
 | 
			
		||||
	public Optional<Message> getMessage(long id) {
 | 
			
		||||
		return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Searches for a chat by recipient ID.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param recipientID the ID of the chat's recipient
 | 
			
		||||
	 * @return an optional containing the chat
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	public Optional<Chat> getChat(long recipientID) {
 | 
			
		||||
		return chats.stream().filter(c -> c.getRecipient().getID() == recipientID).findAny();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
@@ -49,7 +49,7 @@ public class WriteProxy {
 | 
			
		||||
				client.sendMessage(msg);
 | 
			
		||||
 | 
			
		||||
				// Update message state to SENT in localDB
 | 
			
		||||
				localDB.getMessage(msg.getID()).nextStatus();
 | 
			
		||||
				localDB.getMessage(msg.getID()).ifPresent(Message::nextStatus);
 | 
			
		||||
			} catch (IOException e) {
 | 
			
		||||
				logger.log(Level.SEVERE, "Could not send cached message", e);
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ import javafx.application.Platform;
 | 
			
		||||
import javafx.collections.FXCollections;
 | 
			
		||||
import javafx.fxml.FXML;
 | 
			
		||||
import javafx.scene.control.*;
 | 
			
		||||
import javafx.scene.control.Alert.AlertType;
 | 
			
		||||
import javafx.scene.input.KeyCode;
 | 
			
		||||
import javafx.scene.input.KeyEvent;
 | 
			
		||||
import javafx.scene.paint.Color;
 | 
			
		||||
@@ -86,21 +87,24 @@ public final class ChatScene {
 | 
			
		||||
 | 
			
		||||
		// Listen to received messages
 | 
			
		||||
		eventBus.register(MessageCreationEvent.class, e -> {
 | 
			
		||||
			final var	message	= e.get();
 | 
			
		||||
			final var	chat	= localDB.getChats().stream().filter(c -> c.getRecipient().getID() == message.getSenderID()).findAny().get();
 | 
			
		||||
			final var message = e.get();
 | 
			
		||||
			localDB.getChat(message.getSenderID()).ifPresent(chat -> {
 | 
			
		||||
				chat.getMessages().add(message);
 | 
			
		||||
 | 
			
		||||
			// Update UI if in current chat
 | 
			
		||||
			if (chat == currentChat) Platform.runLater(() -> messageList.getItems().add(message));
 | 
			
		||||
				// Update UI if in current chat
 | 
			
		||||
				if (chat == currentChat)
 | 
			
		||||
					Platform.runLater(messageList::refresh);
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		// Listen to message status changes
 | 
			
		||||
		eventBus.register(MessageStatusChangeEvent.class, e -> {
 | 
			
		||||
			final var message = localDB.getMessage(e.getID());
 | 
			
		||||
		eventBus.register(MessageStatusChangeEvent.class, e ->
 | 
			
		||||
			localDB.getMessage(e.getID()).ifPresent(message -> {
 | 
			
		||||
			message.setStatus(e.get());
 | 
			
		||||
 | 
			
		||||
			// Update UI if in current chat
 | 
			
		||||
			if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) Platform.runLater(messageList::refresh);
 | 
			
		||||
		});
 | 
			
		||||
		}));
 | 
			
		||||
 | 
			
		||||
		// Listen to user status changes
 | 
			
		||||
		eventBus.register(UserStatusChangeEvent.class, e ->
 | 
			
		||||
@@ -165,10 +169,8 @@ public final class ChatScene {
 | 
			
		||||
			// LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes
 | 
			
		||||
 | 
			
		||||
			// Load the chat or create a new one and add it to the LocalDB
 | 
			
		||||
			currentChat = localDB.getChats()
 | 
			
		||||
				.stream()
 | 
			
		||||
				.filter(c -> c.getRecipient().getID() == user.getID())
 | 
			
		||||
				.findAny()
 | 
			
		||||
			currentChat = localDB
 | 
			
		||||
				.getChat(user.getID())
 | 
			
		||||
				.orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; });
 | 
			
		||||
 | 
			
		||||
			messageList.setItems(FXCollections.observableList(currentChat.getMessages()));
 | 
			
		||||
@@ -180,14 +182,6 @@ public final class ChatScene {
 | 
			
		||||
		messageTextArea.setDisable(currentChat == null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Actions to perform when the Post Button has been clicked.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	@FXML
 | 
			
		||||
	private void postButtonClicked() { postMessage(); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Actions to perform when the Settings Button has been clicked.
 | 
			
		||||
	 *
 | 
			
		||||
@@ -252,27 +246,13 @@ public final class ChatScene {
 | 
			
		||||
	 *
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	@FXML
 | 
			
		||||
	private void postMessage() {
 | 
			
		||||
 | 
			
		||||
		// Create and send message
 | 
			
		||||
		sendMessage(new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())
 | 
			
		||||
			.setText(messageTextArea.getText().strip())
 | 
			
		||||
			.build());
 | 
			
		||||
 | 
			
		||||
		// Clear text field and disable post button
 | 
			
		||||
		messageTextArea.setText("");
 | 
			
		||||
		postButton.setDisable(true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Sends a message to the server and appends it to the current chat. If all
 | 
			
		||||
	 * message IDs have been used, a new ID generator is requested.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param message the message to send
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	private void sendMessage(Message message) {
 | 
			
		||||
		try {
 | 
			
		||||
			// Create and send message
 | 
			
		||||
			final var message = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())
 | 
			
		||||
				.setText(messageTextArea.getText().strip())
 | 
			
		||||
				.build();
 | 
			
		||||
 | 
			
		||||
			// Send message
 | 
			
		||||
			writeProxy.writeMessage(message);
 | 
			
		||||
@@ -285,6 +265,11 @@ public final class ChatScene {
 | 
			
		||||
 | 
			
		||||
		} catch (final IOException e) {
 | 
			
		||||
			logger.log(Level.SEVERE, "Error sending message", e);
 | 
			
		||||
			new Alert(AlertType.ERROR, "An error occured while sending the message!").showAndWait();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Clear text field and disable post button
 | 
			
		||||
		messageTextArea.setText("");
 | 
			
		||||
		postButton.setDisable(true);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,7 @@
 | 
			
		||||
		<Label fx:id="contactLabel" prefHeight="16.0" prefWidth="250.0" text="Select a contact to chat with" GridPane.columnSpan="2" />
 | 
			
		||||
		<Button fx:id="settingsButton" mnemonicParsing="true" onAction="#settingsButtonClicked" text="_Settings" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
 | 
			
		||||
		<ListView fx:id="messageList" prefHeight="257.0" prefWidth="155.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1" GridPane.rowSpan="2" />
 | 
			
		||||
		<Button fx:id="postButton" defaultButton="true" disable="true" mnemonicParsing="true" onAction="#postButtonClicked" prefHeight="10.0" prefWidth="65.0" text="_Post" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
 | 
			
		||||
		<Button fx:id="postButton" defaultButton="true" disable="true" mnemonicParsing="true" onAction="#postMessage" prefHeight="10.0" prefWidth="65.0" text="_Post" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
 | 
			
		||||
      	<TextArea fx:id="messageTextArea" disable="true" onInputMethodTextChanged="#messageTextUpdated" onKeyPressed="#checkKeyCombination" onKeyTyped="#messageTextUpdated" prefHeight="200.0" prefWidth="200.0" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="3" />
 | 
			
		||||
      <Button mnemonicParsing="true" onAction="#addContactButtonClicked" text="_Add Contacts" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER">
 | 
			
		||||
         <padding>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user