stabilize desktop chat
This commit is contained in:
@@ -5,7 +5,6 @@ import javafx.collections.FXCollections;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.Hyperlink;
|
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.ListCell;
|
import javafx.scene.control.ListCell;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
@@ -124,20 +123,28 @@ public class ChatController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void btnSendClicked() {
|
void btnSendClicked() {
|
||||||
if (selectedConversation == null) {
|
try {
|
||||||
lblChatStatus.setText("Select a conversation");
|
if (selectedConversation == null) {
|
||||||
return;
|
lblChatStatus.setText("Select a conversation");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
String content = txtMessage.getText() == null ? "" : txtMessage.getText().trim();
|
String content = txtMessage.getText() == null ? "" : txtMessage.getText().trim();
|
||||||
if (content.isEmpty()) {
|
if (content.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
txtMessage.clear();
|
txtMessage.clear();
|
||||||
boolean sent = realtimeClient.sendMessage(selectedConversation.getId(), content);
|
boolean sent = realtimeClient.sendMessage(selectedConversation.getId(), content);
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
sendMessageFallback(selectedConversation.getId(), content);
|
sendMessageFallback(selectedConversation.getId(), content);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
ActivityLogger.getInstance().logException(
|
||||||
|
"ChatController.btnSendClicked",
|
||||||
|
e,
|
||||||
|
"Sending chat message");
|
||||||
|
lblChatStatus.setText("Chat send failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,16 +231,30 @@ public class ChatController {
|
|||||||
private void renderMessages(List<MessageResponse> messages) {
|
private void renderMessages(List<MessageResponse> messages) {
|
||||||
vbMessages.getChildren().clear();
|
vbMessages.getChildren().clear();
|
||||||
for (MessageResponse message : messages) {
|
for (MessageResponse message : messages) {
|
||||||
vbMessages.getChildren().add(createMessageBubble(message));
|
try {
|
||||||
|
vbMessages.getChildren().add(createMessageBubble(message));
|
||||||
|
} catch (Exception e) {
|
||||||
|
ActivityLogger.getInstance().logException(
|
||||||
|
"ChatController.renderMessages",
|
||||||
|
e,
|
||||||
|
"Rendering chat message");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
scrollMessagesToBottom();
|
scrollMessagesToBottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendMessageIfSelected(MessageResponse message) {
|
private void appendMessageIfSelected(MessageResponse message) {
|
||||||
upsertConversationForMessage(message);
|
try {
|
||||||
if (selectedConversation != null && selectedConversation.getId().equals(message.getConversationId())) {
|
upsertConversationForMessage(message);
|
||||||
vbMessages.getChildren().add(createMessageBubble(message));
|
if (selectedConversation != null && selectedConversation.getId().equals(message.getConversationId())) {
|
||||||
scrollMessagesToBottom();
|
vbMessages.getChildren().add(createMessageBubble(message));
|
||||||
|
scrollMessagesToBottom();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
ActivityLogger.getInstance().logException(
|
||||||
|
"ChatController.appendMessageIfSelected",
|
||||||
|
e,
|
||||||
|
"Appending chat message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,10 +327,9 @@ public class ChatController {
|
|||||||
if (message.getAttachmentSizeBytes() != null && message.getAttachmentSizeBytes() > 0) {
|
if (message.getAttachmentSizeBytes() != null && message.getAttachmentSizeBytes() > 0) {
|
||||||
attachmentLabel = attachmentLabel + " (" + formatSize(message.getAttachmentSizeBytes()) + ")";
|
attachmentLabel = attachmentLabel + " (" + formatSize(message.getAttachmentSizeBytes()) + ")";
|
||||||
}
|
}
|
||||||
Hyperlink attachment = new Hyperlink(attachmentLabel);
|
Label attachment = new Label(attachmentLabel);
|
||||||
attachment.setWrapText(true);
|
attachment.setWrapText(true);
|
||||||
attachment.setOnAction(event -> openAttachment(message.getAttachmentUrl()));
|
attachment.setStyle("-fx-text-fill: " + (mine ? "#dbeafe" : "#0f766e") + "; -fx-underline: true;");
|
||||||
attachment.setStyle("-fx-text-fill: " + (mine ? "#dbeafe" : "#0f766e") + ";");
|
|
||||||
bubble.getChildren().add(attachment);
|
bubble.getChildren().add(attachment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,18 +388,6 @@ public class ChatController {
|
|||||||
private void scrollMessagesToBottom() {
|
private void scrollMessagesToBottom() {
|
||||||
Platform.runLater(() -> spMessages.setVvalue(1.0));
|
Platform.runLater(() -> spMessages.setVvalue(1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openAttachment(String url) {
|
|
||||||
try {
|
|
||||||
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
|
|
||||||
} catch (Exception e) {
|
|
||||||
ActivityLogger.getInstance().logException(
|
|
||||||
"ChatController.openAttachment",
|
|
||||||
e,
|
|
||||||
"Opening chat attachment");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String formatSize(Long bytes) {
|
private String formatSize(Long bytes) {
|
||||||
if (bytes == null || bytes <= 0) {
|
if (bytes == null || bytes <= 0) {
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
Reference in New Issue
Block a user