mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Handles messages update events
This commit is contained in:
parent
2746623b8d
commit
88ba2d303e
@ -342,9 +342,7 @@ class ConversationsHelper {
|
||||
|
||||
if (response.code != 200) return false;
|
||||
|
||||
// Update the message content locally
|
||||
return await _conversationMessagesDatabaseHelper.updateMessageContent(
|
||||
id: id, newContent: newContent);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Delete permanently a message specified by its [id]
|
||||
|
@ -2,7 +2,6 @@ import 'package:comunic/helpers/database/database_contract.dart';
|
||||
import 'package:comunic/helpers/database/model_database_helper.dart';
|
||||
import 'package:comunic/lists/conversation_messages_list.dart';
|
||||
import 'package:comunic/models/conversation_message.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Conversation messages database helper
|
||||
///
|
||||
@ -35,26 +34,4 @@ class ConversationMessagesDatabaseHelper
|
||||
finalList.addAll(list);
|
||||
return finalList;
|
||||
}
|
||||
|
||||
/// Update the content of a message
|
||||
Future<bool> updateMessageContent({
|
||||
@required int id,
|
||||
@required String newContent,
|
||||
}) async {
|
||||
assert(id != null);
|
||||
assert(newContent != null);
|
||||
|
||||
final message = await get(id);
|
||||
|
||||
if(message == null)
|
||||
return false;
|
||||
|
||||
// Update the conversation message using the map
|
||||
final map = message.toMap();
|
||||
map[ConversationsMessagesTableContract.C_MESSAGE] = newContent;
|
||||
|
||||
await insertOrUpdate(ConversationMessage.fromMap(map));
|
||||
|
||||
return true; // Success
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +53,13 @@ class NewConversationMessageEvent {
|
||||
NewConversationMessageEvent(this.msg);
|
||||
}
|
||||
|
||||
/// Updated conversation message
|
||||
class UpdatedConversationMessageEvent {
|
||||
final ConversationMessage msg;
|
||||
|
||||
UpdatedConversationMessageEvent(this.msg);
|
||||
}
|
||||
|
||||
|
||||
class EventsHelper {
|
||||
static EventBus _mgr = EventBus();
|
||||
|
@ -144,6 +144,12 @@ class WebSocketHelper {
|
||||
ConversationsHelper.apiToConversationMessage(msg.data)));
|
||||
break;
|
||||
|
||||
// Update conversation message content
|
||||
case "updated_conv_message":
|
||||
EventsHelper.emit(UpdatedConversationMessageEvent(
|
||||
ConversationsHelper.apiToConversationMessage(msg.data)));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw Exception("Unknown message type: ${msg.title}");
|
||||
}
|
||||
|
@ -48,4 +48,10 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
|
||||
if (message.id < firstMessageID) firstMessageID = message.id;
|
||||
return firstMessageID;
|
||||
}
|
||||
|
||||
/// Replace a message by another one (if any)
|
||||
void replace(ConversationMessage msg) {
|
||||
final index = this.indexWhere((t) => t.id == msg.id);
|
||||
if (index >= 0) this[index] = msg;
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +86,17 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
.registerConversationEvents(widget.conversationID);
|
||||
|
||||
this.listen<NewConversationMessageEvent>((ev) async {
|
||||
await _conversationsHelper.saveMessage(ev.msg);
|
||||
await _applyNewMessages(ConversationMessagesList()..add(ev.msg));
|
||||
if (ev.msg.conversationID == widget.conversationID) {
|
||||
await _conversationsHelper.saveMessage(ev.msg);
|
||||
await _applyNewMessages(ConversationMessagesList()..add(ev.msg));
|
||||
}
|
||||
});
|
||||
|
||||
this.listen<UpdatedConversationMessageEvent>((ev) async {
|
||||
if (ev.msg.conversationID == widget.conversationID) {
|
||||
await _conversationsHelper.saveMessage(ev.msg);
|
||||
setState(() => _messages.replace(ev.msg));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -406,16 +415,6 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
showSimpleSnack(context, tr("Could not update message content!"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the new version of the conversation message
|
||||
final newMessage =
|
||||
await _conversationsHelper.getSingleMessageFromCache(message.id);
|
||||
|
||||
setState(() {
|
||||
final index = _messages.indexOf(message);
|
||||
_messages.insert(index, newMessage);
|
||||
_messages.removeAt(index + 1);
|
||||
});
|
||||
}
|
||||
|
||||
/// Request message deletion
|
||||
|
Loading…
Reference in New Issue
Block a user