2021-03-10 16:54:41 +00:00
|
|
|
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
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
HashMap<int?, ConversationsMessagesSerializationHelper>? _instances;
|
2021-03-10 16:54:41 +00:00
|
|
|
|
|
|
|
class ConversationsMessagesSerializationHelper
|
|
|
|
extends BaseSerializationHelper<ConversationMessage> {
|
|
|
|
final int convID;
|
|
|
|
|
|
|
|
ConversationsMessagesSerializationHelper._(int convID)
|
2022-03-11 15:27:01 +00:00
|
|
|
: convID = convID;
|
2021-03-10 16:54:41 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
factory ConversationsMessagesSerializationHelper(int? convID) {
|
2021-03-10 18:08:18 +00:00
|
|
|
if (_instances == null) _instances = HashMap();
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
if (!_instances!.containsKey(convID))
|
|
|
|
_instances![convID] = ConversationsMessagesSerializationHelper._(convID!);
|
2021-03-10 16:54:41 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
return _instances![convID]!;
|
2021-03-10 16:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConversationMessage parse(Map<String, dynamic> m) =>
|
|
|
|
ConversationMessage.fromJson(m);
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get type => "conv-messages-$convID";
|
|
|
|
|
|
|
|
Future<ConversationMessagesList> getList() async =>
|
|
|
|
ConversationMessagesList()..addAll(await super.getList());
|
|
|
|
|
|
|
|
Future<void> insertOrReplace(ConversationMessage msg) async =>
|
|
|
|
await insertOrReplaceElement((t) => t.id == msg.id, msg);
|
|
|
|
|
|
|
|
Future<void> remove(ConversationMessage msg) async =>
|
|
|
|
await removeElement((t) => t.id == msg.id);
|
|
|
|
|
|
|
|
/// Insert or replace a list of messages
|
|
|
|
Future<void> insertOrReplaceAll(List<ConversationMessage> list) async {
|
|
|
|
for (var message in list)
|
|
|
|
await insertOrReplaceElement((t) => t.id == message.id, message);
|
|
|
|
}
|
|
|
|
}
|