♻️ Refactor room message sorting

This commit is contained in:
2025-09-23 14:21:37 +08:00
parent b2ac5fbef2
commit 8baf77bcf7
5 changed files with 33 additions and 25 deletions

View File

@@ -348,7 +348,11 @@ class MessagesNotifier extends _$MessagesNotifier {
);
final List<LocalChatMessage> dbMessages;
if (_searchQuery != null && _searchQuery!.isNotEmpty) {
dbMessages = await _database.searchMessages(_roomId, _searchQuery ?? '');
dbMessages = await _database.searchMessages(
_roomId,
_searchQuery ?? '',
withAttachments: _withAttachments,
);
} else {
final chatMessagesFromDb = await _database.getMessagesForRoom(
_roomId,
@@ -366,11 +370,6 @@ class MessagesNotifier extends _$MessagesNotifier {
filteredMessages.where((msg) => _hasLink(msg)).toList();
}
if (_withAttachments == true) {
filteredMessages =
filteredMessages.where((msg) => _hasAttachment(msg)).toList();
}
final dbLocalMessages = filteredMessages;
if (offset == 0) {
@@ -852,9 +851,11 @@ class MessagesNotifier extends _$MessagesNotifier {
if (existingIndex >= 0) {
final newList = [...currentMessages];
newList[existingIndex] = localMessage;
state = AsyncValue.data(newList);
state = AsyncValue.data(_sortMessages(newList));
} else {
state = AsyncValue.data([localMessage, ...currentMessages]);
state = AsyncValue.data(
_sortMessages([localMessage, ...currentMessages]),
);
}
}
@@ -987,11 +988,6 @@ class MessagesNotifier extends _$MessagesNotifier {
final urlRegex = RegExp(r'https?://[^\s/$.?#].[^\s]*');
return urlRegex.hasMatch(content);
}
bool _hasAttachment(LocalChatMessage message) {
final remoteMessage = message.toRemoteMessage();
return remoteMessage.attachments.isNotEmpty;
}
}
class ChatRoomScreen extends HookConsumerWidget {