mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-25 22:39:22 +00:00
Handles remove message events
This commit is contained in:
parent
88ba2d303e
commit
909e68e7bb
@ -333,6 +333,11 @@ class ConversationsHelper {
|
||||
await _conversationMessagesDatabaseHelper.insertOrUpdate(msg);
|
||||
}
|
||||
|
||||
/// Remove a message from the database
|
||||
Future<void> removeMessage(int msgID) async {
|
||||
await _conversationMessagesDatabaseHelper.delete(msgID);
|
||||
}
|
||||
|
||||
/// Update a message content
|
||||
Future<bool> updateMessage(int id, String newContent) async {
|
||||
final response = await APIRequest(
|
||||
@ -355,8 +360,7 @@ class ConversationsHelper {
|
||||
|
||||
if (response.code != 200) return false;
|
||||
|
||||
// Delete the message locally
|
||||
return await _conversationMessagesDatabaseHelper.delete(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Register a conversation : ask the server to notify about updates to the
|
||||
|
@ -60,6 +60,12 @@ class UpdatedConversationMessageEvent {
|
||||
UpdatedConversationMessageEvent(this.msg);
|
||||
}
|
||||
|
||||
/// Deleted conversation message
|
||||
class DeletedConversationMessageEvent {
|
||||
final ConversationMessage msg;
|
||||
|
||||
DeletedConversationMessageEvent(this.msg);
|
||||
}
|
||||
|
||||
class EventsHelper {
|
||||
static EventBus _mgr = EventBus();
|
||||
|
@ -150,6 +150,12 @@ class WebSocketHelper {
|
||||
ConversationsHelper.apiToConversationMessage(msg.data)));
|
||||
break;
|
||||
|
||||
// Deleted a conversation message
|
||||
case "deleted_conv_message":
|
||||
EventsHelper.emit(DeletedConversationMessageEvent(
|
||||
ConversationsHelper.apiToConversationMessage(msg.data)));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw Exception("Unknown message type: ${msg.title}");
|
||||
}
|
||||
|
@ -54,4 +54,7 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
|
||||
final index = this.indexWhere((t) => t.id == msg.id);
|
||||
if (index >= 0) this[index] = msg;
|
||||
}
|
||||
|
||||
/// Remove a message from this list
|
||||
void removeMsg(int id) => removeWhere((f) => f.id == id);
|
||||
}
|
||||
|
@ -98,6 +98,13 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
setState(() => _messages.replace(ev.msg));
|
||||
}
|
||||
});
|
||||
|
||||
this.listen<DeletedConversationMessageEvent>((ev) async {
|
||||
if (ev.msg.conversationID == widget.conversationID) {
|
||||
await _conversationsHelper.removeMessage(ev.msg.id);
|
||||
setState(() => _messages.removeMsg(ev.msg.id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Free resources when this conversation widget is no longer required
|
||||
@ -450,10 +457,5 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
// Execute the request
|
||||
if (!await _conversationsHelper.deleteMessage(message.id))
|
||||
showSimpleSnack(context, tr("Could not delete conversation message!"));
|
||||
|
||||
// Remove the message from the list
|
||||
setState(() {
|
||||
_messages.remove(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user