1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Handles messages update events

This commit is contained in:
Pierre HUBERT 2020-04-19 14:16:35 +02:00
parent 2746623b8d
commit 88ba2d303e
6 changed files with 31 additions and 38 deletions

View File

@ -342,9 +342,7 @@ class ConversationsHelper {
if (response.code != 200) return false; if (response.code != 200) return false;
// Update the message content locally return true;
return await _conversationMessagesDatabaseHelper.updateMessageContent(
id: id, newContent: newContent);
} }
/// Delete permanently a message specified by its [id] /// Delete permanently a message specified by its [id]

View File

@ -2,7 +2,6 @@ import 'package:comunic/helpers/database/database_contract.dart';
import 'package:comunic/helpers/database/model_database_helper.dart'; import 'package:comunic/helpers/database/model_database_helper.dart';
import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/conversation_messages_list.dart';
import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/conversation_message.dart';
import 'package:meta/meta.dart';
/// Conversation messages database helper /// Conversation messages database helper
/// ///
@ -35,26 +34,4 @@ class ConversationMessagesDatabaseHelper
finalList.addAll(list); finalList.addAll(list);
return finalList; 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
}
} }

View File

@ -53,6 +53,13 @@ class NewConversationMessageEvent {
NewConversationMessageEvent(this.msg); NewConversationMessageEvent(this.msg);
} }
/// Updated conversation message
class UpdatedConversationMessageEvent {
final ConversationMessage msg;
UpdatedConversationMessageEvent(this.msg);
}
class EventsHelper { class EventsHelper {
static EventBus _mgr = EventBus(); static EventBus _mgr = EventBus();

View File

@ -144,6 +144,12 @@ class WebSocketHelper {
ConversationsHelper.apiToConversationMessage(msg.data))); ConversationsHelper.apiToConversationMessage(msg.data)));
break; break;
// Update conversation message content
case "updated_conv_message":
EventsHelper.emit(UpdatedConversationMessageEvent(
ConversationsHelper.apiToConversationMessage(msg.data)));
break;
default: default:
throw Exception("Unknown message type: ${msg.title}"); throw Exception("Unknown message type: ${msg.title}");
} }

View File

@ -48,4 +48,10 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
if (message.id < firstMessageID) firstMessageID = message.id; if (message.id < firstMessageID) firstMessageID = message.id;
return firstMessageID; 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;
}
} }

View File

@ -86,8 +86,17 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
.registerConversationEvents(widget.conversationID); .registerConversationEvents(widget.conversationID);
this.listen<NewConversationMessageEvent>((ev) async { this.listen<NewConversationMessageEvent>((ev) async {
if (ev.msg.conversationID == widget.conversationID) {
await _conversationsHelper.saveMessage(ev.msg); await _conversationsHelper.saveMessage(ev.msg);
await _applyNewMessages(ConversationMessagesList()..add(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!")); showSimpleSnack(context, tr("Could not update message content!"));
return; 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 /// Request message deletion