diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index d7f003d..ac801fd 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -227,10 +227,7 @@ class ConversationsHelper { ConversationMessagesList list = ConversationMessagesList(); response.getArray().forEach((f) { list.add( - _apiToConversationMessage( - conversationID: conversationID, - map: f, - ), + apiToConversationMessage(f), ); }); @@ -331,6 +328,11 @@ class ConversationsHelper { return SendMessageResult.SUCCESS; } + /// Save / Update a message into the database + Future saveMessage(ConversationMessage msg) async { + await _conversationMessagesDatabaseHelper.insertOrUpdate(msg); + } + /// Update a message content Future updateMessage(int id, String newContent) async { final response = await APIRequest( @@ -383,13 +385,12 @@ class ConversationsHelper { } /// Turn an API response into a ConversationMessage object - ConversationMessage _apiToConversationMessage({ - @required int conversationID, - @required Map map, - }) { + static ConversationMessage apiToConversationMessage( + Map map, + ) { return ConversationMessage( id: map["ID"], - conversationID: conversationID, + conversationID: map["convID"], userID: map["ID_user"], timeInsert: map["time_insert"], message: DisplayedString(map["message"]), diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index fd5a7c1..093f3af 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:comunic/models/comment.dart'; +import 'package:comunic/models/conversation_message.dart'; import 'package:event_bus/event_bus.dart'; /// Events helper @@ -45,6 +46,13 @@ class DeletedCommentEvent { DeletedCommentEvent(this.commentID); } +/// New conversation message +class NewConversationMessageEvent { + final ConversationMessage msg; + + NewConversationMessageEvent(this.msg); +} + class EventsHelper { static EventBus _mgr = EventBus(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index a69c9f0..e8c579c 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:convert'; import 'package:comunic/helpers/comments_helper.dart'; +import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/config.dart'; @@ -137,6 +138,12 @@ class WebSocketHelper { EventsHelper.emit(DeletedCommentEvent(msg.data)); break; + // Created new conversation message + case "new_conv_message": + EventsHelper.emit(NewConversationMessageEvent( + ConversationsHelper.apiToConversationMessage(msg.data))); + break; + default: throw Exception("Unknown message type: ${msg.title}"); } diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index 11e83b5..4a75eb5 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:comunic/helpers/conversations_helper.dart'; +import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/users_list.dart'; @@ -83,6 +84,11 @@ class _ConversationScreenState extends SafeState { await _conversationsHelper .registerConversationEvents(widget.conversationID); + + this.listen((ev) async { + await _conversationsHelper.saveMessage(ev.msg); + await _applyNewMessages(ConversationMessagesList()..add(ev.msg)); + }); } /// Free resources when this conversation widget is no longer required