🐛 Bug fixes on loading more messages

This commit is contained in:
LittleSheep 2024-11-19 22:17:17 +08:00
parent fd272ead37
commit 6af695d74e
5 changed files with 42 additions and 11 deletions

View File

@ -163,5 +163,7 @@
"eventResourceTag": "Event {}", "eventResourceTag": "Event {}",
"messageDelete": "Delete message {}", "messageDelete": "Delete message {}",
"messageDeleteDescription": "Are you sure you want to delete this message? This operation is irreversible. You will leave a record of the deleted message.", "messageDeleteDescription": "Are you sure you want to delete this message? This operation is irreversible. You will leave a record of the deleted message.",
"messageDeleted": "Message {} has been deleted" "messageDeleted": "Message {} has been deleted",
"messageEdited": "Message {} has been edited",
"messageEditedHint": "Edited"
} }

View File

@ -163,5 +163,7 @@
"eventResourceTag": "消息 {}", "eventResourceTag": "消息 {}",
"messageDelete": "删除消息 {}", "messageDelete": "删除消息 {}",
"messageDeleteDescription": "你确定要删除这个消息吗?该操作不可撤销。同时您将留下一条删除消息的记录。", "messageDeleteDescription": "你确定要删除这个消息吗?该操作不可撤销。同时您将留下一条删除消息的记录。",
"messageDeleted": "消息 {} 已被删除" "messageDeleted": "消息 {} 已被删除",
"messageEdited": "消息 {} 已被编辑",
"messageEditedHint": "已编辑"
} }

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:collection/collection.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
@ -295,7 +296,7 @@ class ChatMessageController extends ChangeNotifier {
final countToFetch = math.min(resp.data['count'] as int, 100); final countToFetch = math.min(resp.data['count'] as int, 100);
for (int idx = 0; idx < countToFetch; idx += kSingleBatchLoadLimit) { for (int idx = 0; idx < countToFetch; idx += kSingleBatchLoadLimit) {
await getMessages(kSingleBatchLoadLimit, idx); await getMessages(kSingleBatchLoadLimit, idx, forceRemote: true);
} }
} catch (err) { } catch (err) {
rethrow; rethrow;
@ -349,10 +350,20 @@ class ChatMessageController extends ChangeNotifier {
int take, int take,
int offset, { int offset, {
bool forceLocal = false, bool forceLocal = false,
bool forceRemote = false,
}) async { }) async {
late List<SnChatMessage> out; late List<SnChatMessage> out;
if (_box != null && (_box!.length >= take + offset || forceLocal)) { if (_box != null &&
out = _box!.values.skip(offset).take(take).toList().reversed.toList(); (_box!.length >= take + offset || forceLocal) &&
!forceRemote) {
out = _box!.keys
.toList()
.cast<int>()
.sorted((a, b) => b.compareTo(a))
.skip(offset)
.take(take)
.map((key) => _box!.get(key)!)
.toList();
} else { } else {
final resp = await _sn.client.get( final resp = await _sn.client.get(
'/cgi/im/channels/${channel!.keyPath}/events', '/cgi/im/channels/${channel!.keyPath}/events',

View File

@ -98,6 +98,7 @@ class _ChatRoomScreenState extends State<ChatRoomScreen> {
idx > 0 ? _messageController.messages[idx - 1] : null; idx > 0 ? _messageController.messages[idx - 1] : null;
final canMerge = nextMessage != null && final canMerge = nextMessage != null &&
nextMessage.updatedAt == nextMessage.createdAt &&
nextMessage.senderId == message.senderId && nextMessage.senderId == message.senderId &&
message.createdAt message.createdAt
.difference(nextMessage.createdAt) .difference(nextMessage.createdAt)
@ -105,6 +106,7 @@ class _ChatRoomScreenState extends State<ChatRoomScreen> {
.abs() <= .abs() <=
3; 3;
final canMergePrevious = previousMessage != null && final canMergePrevious = previousMessage != null &&
message.updatedAt == message.createdAt &&
previousMessage.senderId == message.senderId && previousMessage.senderId == message.senderId &&
message.createdAt message.createdAt
.difference(previousMessage.createdAt) .difference(previousMessage.createdAt)

View File

@ -111,6 +111,10 @@ class ChatMessage extends StatelessWidget {
? data.sender.nick! ? data.sender.nick!
: user!.nick, : user!.nick,
).bold(), ).bold(),
if (data.updatedAt != data.createdAt)
Text(
'messageEditedHint'.tr(),
).fontSize(14).opacity(0.75).padding(left: 6),
const Gap(6), const Gap(6),
Text( Text(
dateFormatter.format(data.createdAt.toLocal()), dateFormatter.format(data.createdAt.toLocal()),
@ -142,23 +146,33 @@ class ChatMessage extends StatelessWidget {
onDelete: onDelete, onDelete: onDelete,
), ),
)).padding(bottom: 4, top: isMerged ? 4 : 2), )).padding(bottom: 4, top: isMerged ? 4 : 2),
if (data.body['text'] != null) if (data.type == 'messages.edit')
Row(
children: [
const Icon(Symbols.edit, size: 20),
const Gap(4),
Text(
'messageEdited'
.tr(args: ['#${data.relatedEventId}']),
),
],
).opacity(0.75)
else if (data.body['text'] != null)
MarkdownTextContent( MarkdownTextContent(
content: data.body['text'], content: data.body['text'],
isAutoWarp: true, isAutoWarp: true,
), ),
if (data.type == 'messages.delete' && if (data.type == 'messages.delete')
data.relatedEventId != null)
Row( Row(
children: [ children: [
const Icon(Symbols.delete), const Icon(Symbols.delete, size: 20),
const Gap(8), const Gap(4),
Text( Text(
'messageDeleted' 'messageDeleted'
.tr(args: ['#${data.relatedEventId}']), .tr(args: ['#${data.relatedEventId}']),
), ),
], ],
), ).opacity(0.75),
], ],
), ),
) )