message edit/update functionality

This commit is contained in:
Shivering Ocelot committed 2025-07-05 10:44:14 -05:00
1 parent cdba31e9f7
commit cba41f1e7e
6 files changed
+122 -32

No files matched your search

+63 -21
View File
@@ -57,6 +57,7 @@ class _ChatScreenState extends State<ChatScreen> {
// UI State
Set<int> expandedThreads = {};
int? editingMessageId = null;
// Internal Funcionality
Offset? _longPressOffset;
@@ -153,21 +154,39 @@ class _ChatScreenState extends State<ChatScreen> {
});
}
void sendMessage(String content) async {
if (content.trim().isEmpty) return; // Don't send empty messages
void sendMessage() async {
final content = _textController.text;
_textController.clear();
if (content.trim().isEmpty) return; // Don't send empty messages
final newMessage = Message(sender: "user", content: content, threadId: currentThreadId);
Backend.sendAction("send-chat-message", {
'chatId': widget.chatId,
'content': content,
'threadId': currentThreadId,
}).then((response) {
if (response['error'] != null) {
print("Error sending chat message: ${response['error']}");
}
});
addMessage(newMessage);
if (editingMessageId == null) {
Backend.sendAction("send-chat-message", {
'chatId': widget.chatId,
'content': content,
'threadId': currentThreadId,
}).then((response) {
if (response['error'] != null) {
print("Error sending chat message: ${response['error']}");
}
});
final newMessage = Message(sender: "user", content: content, threadId: currentThreadId);
addMessage(newMessage);
} else {
Backend.sendAction("update-chat-message", {
'messageId': editingMessageId,
'content': content,
}).then((response) {
if (response['error'] != null) {
print("Error updating message: ${response['error']}");
}
});
Message message = _messages.firstWhere((element) => element.id == editingMessageId!);
setState(() {
message.content = content;
});
}
}
void createThread () async {
@@ -186,7 +205,7 @@ class _ChatScreenState extends State<ChatScreen> {
return messageColor;
}
void _showMessageContextMenu(BuildContext context, Offset position) {
void _showMessageContextMenu(BuildContext context, Message message, Offset position) {
showMenu(
context: context,
position: RelativeRect.fromLTRB(position.dx, position.dy, position.dx + 1, position.dy + 1),
@@ -201,8 +220,10 @@ class _ChatScreenState extends State<ChatScreen> {
PopupMenuItem(
child: Text('Edit'),
onTap: () {
// TODO: Implement Edit functionality
print('Edit clicked');
_textController.text = message.content;
setState(() {
editingMessageId = message.id;
});
},
),
PopupMenuItem(
@@ -282,11 +303,11 @@ class _ChatScreenState extends State<ChatScreen> {
_longPressOffset = details.globalPosition;
},
onLongPress: () {
_showMessageContextMenu(context, _longPressOffset!);
_showMessageContextMenu(context, message, _longPressOffset!);
_longPressOffset = null;
},
onSecondaryTapUp: (details) {
_showMessageContextMenu(context, details.globalPosition);
_showMessageContextMenu(context, message, details.globalPosition);
},
child: Padding(
padding: isWideScreen ? message.padding : EdgeInsets.zero,
@@ -299,7 +320,14 @@ class _ChatScreenState extends State<ChatScreen> {
color: getMessageColor(context),
borderRadius: BorderRadius.circular(12),
),
child: Text(message.content),
child: SelectableText(
message.content,
toolbarOptions: ToolbarOptions(
copy: true,
selectAll: false,
),
onTap: () {},
),
),
),
),
@@ -315,13 +343,16 @@ class _ChatScreenState extends State<ChatScreen> {
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
if (editingMessageId!= null) ...[
Text('Editing ${editingMessageId}'),
],
Expanded(
child: KeyboardListener(
focusNode: _inputFocusNode,
onKeyEvent: (event) {
// TODO: Probably doing this in the input focus node would be cleaner
if (HardwareKeyboard.instance.isControlPressed && HardwareKeyboard.instance.isLogicalKeyPressed(LogicalKeyboardKey.enter)) {
sendMessage(_textController.text);
sendMessage();
//return KeyEventResult.handled;
}
//return KeyEventResult.ignored;
@@ -338,7 +369,7 @@ class _ChatScreenState extends State<ChatScreen> {
children: [
ElevatedButton(
onPressed: () {
sendMessage(_textController.text);
sendMessage();
},
child: Text('Send'),
),
@@ -349,6 +380,17 @@ class _ChatScreenState extends State<ChatScreen> {
},
child: Text('Start New Topic'),
),
if (editingMessageId!= null) ...[
ElevatedButton(
onPressed: () {
_textController.text = "";
setState(() {
editingMessageId = null;
});
},
child: Text('Cancel Edit'),
)
],
],
),
],