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

Handles new messages events

This commit is contained in:
Pierre HUBERT 2020-04-19 13:58:24 +02:00
parent b0be889833
commit 2746623b8d
4 changed files with 31 additions and 9 deletions

View File

@ -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<void> saveMessage(ConversationMessage msg) async {
await _conversationMessagesDatabaseHelper.insertOrUpdate(msg);
}
/// Update a message content
Future<bool> 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<String, dynamic> map,
}) {
static ConversationMessage apiToConversationMessage(
Map<String, dynamic> map,
) {
return ConversationMessage(
id: map["ID"],
conversationID: conversationID,
conversationID: map["convID"],
userID: map["ID_user"],
timeInsert: map["time_insert"],
message: DisplayedString(map["message"]),

View File

@ -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();

View File

@ -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}");
}

View File

@ -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<ConversationScreen> {
await _conversationsHelper
.registerConversationEvents(widget.conversationID);
this.listen<NewConversationMessageEvent>((ev) async {
await _conversationsHelper.saveMessage(ev.msg);
await _applyNewMessages(ConversationMessagesList()..add(ev.msg));
});
}
/// Free resources when this conversation widget is no longer required