🐛 Fix events applying issue

This commit is contained in:
LittleSheep 2024-06-28 14:56:57 +08:00
parent 793ad156e3
commit a7035581e9
2 changed files with 38 additions and 66 deletions

View File

@ -56,14 +56,9 @@ class ChatEventController {
); );
totalEvents.value = result?.$2 ?? 0; totalEvents.value = result?.$2 ?? 0;
if (result != null) { if (result != null) {
currentEvents.addAll(result.$1.map( for (final x in result.$1.reversed) {
(x) => LocalEvent( applyEvent(LocalEvent(x.id, x, x.channelId, x.createdAt));
x.id, }
x,
x.channelId,
x.createdAt,
),
));
} }
} else { } else {
final result = await database.syncEvents( final result = await database.syncEvents(
@ -71,16 +66,7 @@ class ChatEventController {
scope: scope, scope: scope,
); );
totalEvents.value = result?.$2 ?? 0; totalEvents.value = result?.$2 ?? 0;
if (!await syncLocal(channel) && result != null) { await syncLocal(channel);
currentEvents.addAll(result.$1.map(
(x) => LocalEvent(
x.id,
x,
x.channelId,
x.createdAt,
),
));
}
} }
isLoading.value = false; isLoading.value = false;
} }
@ -96,14 +82,9 @@ class ChatEventController {
); );
totalEvents.value = result?.$2 ?? 0; totalEvents.value = result?.$2 ?? 0;
if (result != null) { if (result != null) {
currentEvents.addAll(result.$1.map( for (final x in result.$1.reversed) {
(x) => LocalEvent( applyEvent(LocalEvent(x.id, x, x.channelId, x.createdAt));
x.id, }
x,
x.channelId,
x.createdAt,
),
));
} }
} else { } else {
final result = await database.syncEvents( final result = await database.syncEvents(
@ -113,27 +94,17 @@ class ChatEventController {
offset: currentEvents.length, offset: currentEvents.length,
); );
totalEvents.value = result?.$2 ?? 0; totalEvents.value = result?.$2 ?? 0;
if (!await syncLocal(channel) && result != null) { await syncLocal(channel);
currentEvents.addAll(result.$1.map(
(x) => LocalEvent(
x.id,
x,
x.channelId,
x.createdAt,
),
));
}
} }
isLoading.value = false; isLoading.value = false;
} }
Future<bool> syncLocal(Channel channel) async { Future<bool> syncLocal(Channel channel) async {
if (PlatformInfo.isWeb) return false; if (PlatformInfo.isWeb) return false;
currentEvents.replaceRange( final data = await database.localEvents.findAllByChannel(channel.id);
0, for (final x in data.reversed) {
currentEvents.length, applyEvent(x);
await database.localEvents.findAllByChannel(channel.id), }
);
return true; return true;
} }
@ -150,28 +121,32 @@ class ChatEventController {
entry = await database.receiveEvent(remote); entry = await database.receiveEvent(remote);
} }
if (remote.channelId != channel?.id) return; applyEvent(entry);
}
final idx = currentEvents.indexWhere((x) => x.data.uuid == remote.uuid); applyEvent(LocalEvent entry) {
if (entry.channelId != channel?.id) return;
final idx = currentEvents.indexWhere((x) => x.data.uuid == entry.data.uuid);
if (idx != -1) { if (idx != -1) {
currentEvents[idx] = entry; currentEvents[idx] = entry;
} else { } else {
currentEvents.insert(0, entry); currentEvents.insert(0, entry);
} }
switch (remote.type) { switch (entry.data.type) {
case 'messages.edit': case 'messages.edit':
final body = EventMessageBody.fromJson(remote.body); final body = EventMessageBody.fromJson(entry.data.body);
if (body.relatedEvent != null) { if (body.relatedEvent != null) {
final idx = final idx =
currentEvents.indexWhere((x) => x.data.id == body.relatedEvent); currentEvents.indexWhere((x) => x.data.id == body.relatedEvent);
if (idx != -1) { if (idx != -1) {
currentEvents[idx].data.body = remote.body; currentEvents[idx].data.body = entry.data.body;
currentEvents[idx].data.updatedAt = remote.updatedAt; currentEvents[idx].data.updatedAt = entry.data.updatedAt;
} }
} }
case 'messages.delete': case 'messages.delete':
final body = EventMessageBody.fromJson(remote.body); final body = EventMessageBody.fromJson(entry.data.body);
if (body.relatedEvent != null) { if (body.relatedEvent != null) {
currentEvents.removeWhere((x) => x.id == body.relatedEvent); currentEvents.removeWhere((x) => x.id == body.relatedEvent);
} }

View File

@ -273,24 +273,21 @@ class _PostItemState extends State<PostItem> {
attachmentsId: item.attachments ?? List.empty(), attachmentsId: item.attachments ?? List.empty(),
divided: true, divided: true,
), ),
SizedBox( PostQuickAction(
width: MediaQuery.of(context).size.width * 0.9, isShowReply: widget.isShowReply,
child: PostQuickAction( isReactable: widget.isReactable,
isShowReply: widget.isShowReply, item: widget.item,
isReactable: widget.isReactable, onReact: (symbol, changes) {
item: widget.item, setState(() {
onReact: (symbol, changes) { item.reactionList[symbol] =
setState(() { (item.reactionList[symbol] ?? 0) + changes;
item.reactionList[symbol] = });
(item.reactionList[symbol] ?? 0) + changes; },
}); ).paddingOnly(
}, top: hasAttachment ? 10 : 6,
).paddingOnly( left: hasAttachment ? 24 : 60,
top: hasAttachment ? 10 : 6, right: 16,
left: hasAttachment ? 24 : 60, bottom: 10,
right: 16,
bottom: 10,
),
), ),
], ],
); );