2022-03-04 19:30:19 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
2022-01-18 21:26:52 +00:00
|
|
|
import 'message.dart';
|
|
|
|
|
2022-01-18 23:31:10 +00:00
|
|
|
class MessageInfo {
|
2022-01-18 21:26:52 +00:00
|
|
|
final MessageMetadata metadata;
|
|
|
|
final String wrapper;
|
2022-01-18 23:31:10 +00:00
|
|
|
MessageInfo(this.metadata, this.wrapper);
|
2022-01-19 21:58:52 +00:00
|
|
|
}
|
2022-01-18 23:31:10 +00:00
|
|
|
|
2022-03-04 19:30:19 +00:00
|
|
|
class MessageCache extends ChangeNotifier {
|
2022-01-18 23:31:10 +00:00
|
|
|
late Map<int, MessageInfo> cache;
|
|
|
|
late List<int?> cacheByIndex;
|
2022-01-20 14:13:54 +00:00
|
|
|
late Map<String, int> cacheByHash;
|
2022-01-18 23:31:10 +00:00
|
|
|
|
|
|
|
MessageCache() {
|
2022-01-20 14:13:54 +00:00
|
|
|
cache = {};
|
|
|
|
cacheByIndex = List.empty(growable: true);
|
|
|
|
cacheByHash = {};
|
2022-01-18 23:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 14:13:54 +00:00
|
|
|
int get indexedLength => cacheByIndex.length;
|
2022-01-18 23:31:10 +00:00
|
|
|
|
2022-01-20 14:13:54 +00:00
|
|
|
MessageInfo? getById(int id) => cache[id];
|
|
|
|
MessageInfo? getByIndex(int index) {
|
|
|
|
if (index >= cacheByIndex.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return cache[cacheByIndex[index]];
|
|
|
|
}
|
2022-01-20 18:37:09 +00:00
|
|
|
|
2022-01-20 14:13:54 +00:00
|
|
|
MessageInfo? getByContentHash(String contenthash) => cache[cacheByHash[contenthash]];
|
|
|
|
|
|
|
|
void addNew(String profileOnion, int conversation, int messageID, DateTime timestamp, String senderHandle, String senderImage, bool isAuto, String data, String? contenthash) {
|
2022-01-18 23:31:10 +00:00
|
|
|
this.cache[messageID] = MessageInfo(MessageMetadata(profileOnion, conversation, messageID, timestamp, senderHandle, senderImage, "", {}, false, false, isAuto), data);
|
|
|
|
this.cacheByIndex.insert(0, messageID);
|
2022-01-20 14:13:54 +00:00
|
|
|
if (contenthash != null && contenthash != "") {
|
|
|
|
this.cacheByHash[contenthash] = messageID;
|
|
|
|
}
|
2022-03-04 19:30:19 +00:00
|
|
|
notifyListeners();
|
2022-01-20 14:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(MessageInfo messageInfo, int index, String? contenthash) {
|
|
|
|
this.cache[messageInfo.metadata.messageID] = messageInfo;
|
|
|
|
this.cacheByIndex.insert(index, messageInfo.metadata.messageID);
|
|
|
|
if (contenthash != null && contenthash != "") {
|
|
|
|
this.cacheByHash[contenthash] = messageInfo.metadata.messageID;
|
|
|
|
}
|
2022-03-04 19:30:19 +00:00
|
|
|
notifyListeners();
|
2022-01-20 14:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addUnindexed(MessageInfo messageInfo, String? contenthash) {
|
|
|
|
this.cache[messageInfo.metadata.messageID] = messageInfo;
|
|
|
|
if (contenthash != null && contenthash != "") {
|
|
|
|
this.cacheByHash[contenthash] = messageInfo.metadata.messageID;
|
|
|
|
}
|
2022-03-04 19:30:19 +00:00
|
|
|
notifyListeners();
|
2022-01-18 23:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ackCache(int messageID) {
|
|
|
|
cache[messageID]?.metadata.ackd = true;
|
2022-03-04 19:30:19 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void errCache(int messageID) {
|
|
|
|
cache[messageID]?.metadata.error = true;
|
|
|
|
notifyListeners();
|
2022-01-18 23:31:10 +00:00
|
|
|
}
|
2022-01-20 18:37:09 +00:00
|
|
|
}
|