1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +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(); ConversationMessagesList list = ConversationMessagesList();
response.getArray().forEach((f) { response.getArray().forEach((f) {
list.add( list.add(
_apiToConversationMessage( apiToConversationMessage(f),
conversationID: conversationID,
map: f,
),
); );
}); });
@ -331,6 +328,11 @@ class ConversationsHelper {
return SendMessageResult.SUCCESS; return SendMessageResult.SUCCESS;
} }
/// Save / Update a message into the database
Future<void> saveMessage(ConversationMessage msg) async {
await _conversationMessagesDatabaseHelper.insertOrUpdate(msg);
}
/// Update a message content /// Update a message content
Future<bool> updateMessage(int id, String newContent) async { Future<bool> updateMessage(int id, String newContent) async {
final response = await APIRequest( final response = await APIRequest(
@ -383,13 +385,12 @@ class ConversationsHelper {
} }
/// Turn an API response into a ConversationMessage object /// Turn an API response into a ConversationMessage object
ConversationMessage _apiToConversationMessage({ static ConversationMessage apiToConversationMessage(
@required int conversationID, Map<String, dynamic> map,
@required Map<String, dynamic> map, ) {
}) {
return ConversationMessage( return ConversationMessage(
id: map["ID"], id: map["ID"],
conversationID: conversationID, conversationID: map["convID"],
userID: map["ID_user"], userID: map["ID_user"],
timeInsert: map["time_insert"], timeInsert: map["time_insert"],
message: DisplayedString(map["message"]), message: DisplayedString(map["message"]),

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:comunic/models/comment.dart'; import 'package:comunic/models/comment.dart';
import 'package:comunic/models/conversation_message.dart';
import 'package:event_bus/event_bus.dart'; import 'package:event_bus/event_bus.dart';
/// Events helper /// Events helper
@ -45,6 +46,13 @@ class DeletedCommentEvent {
DeletedCommentEvent(this.commentID); DeletedCommentEvent(this.commentID);
} }
/// New conversation message
class NewConversationMessageEvent {
final ConversationMessage msg;
NewConversationMessageEvent(this.msg);
}
class EventsHelper { class EventsHelper {
static EventBus _mgr = EventBus(); static EventBus _mgr = EventBus();

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/config.dart'; import 'package:comunic/models/config.dart';
@ -137,6 +138,12 @@ class WebSocketHelper {
EventsHelper.emit(DeletedCommentEvent(msg.data)); EventsHelper.emit(DeletedCommentEvent(msg.data));
break; break;
// Created new conversation message
case "new_conv_message":
EventsHelper.emit(NewConversationMessageEvent(
ConversationsHelper.apiToConversationMessage(msg.data)));
break;
default: default:
throw Exception("Unknown message type: ${msg.title}"); throw Exception("Unknown message type: ${msg.title}");
} }

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/conversation_messages_list.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
@ -83,6 +84,11 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
await _conversationsHelper await _conversationsHelper
.registerConversationEvents(widget.conversationID); .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 /// Free resources when this conversation widget is no longer required