import 'dart:collection'; import 'package:comunic/helpers/serialization/base_serialization_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/models/conversation_message.dart'; /// Conversations serialization helper /// /// @author Pierre Hubert HashMap? _instances; class ConversationsMessagesSerializationHelper extends BaseSerializationHelper { final int convID; ConversationsMessagesSerializationHelper._(int convID) : convID = convID; factory ConversationsMessagesSerializationHelper(int? convID) { if (_instances == null) _instances = HashMap(); if (!_instances!.containsKey(convID)) _instances![convID] = ConversationsMessagesSerializationHelper._(convID!); return _instances![convID]!; } @override ConversationMessage parse(Map m) => ConversationMessage.fromJson(m); @override String get type => "conv-messages-$convID"; Future getList() async => ConversationMessagesList()..addAll(await super.getList()); Future insertOrReplace(ConversationMessage msg) async => await insertOrReplaceElement((t) => t.id == msg.id, msg); Future remove(ConversationMessage msg) async => await removeElement((t) => t.id == msg.id); /// Insert or replace a list of messages Future insertOrReplaceAll(List list) async { for (var message in list) await insertOrReplaceElement((t) => t.id == message.id, message); } }