💄 Optimize the ability to search
This commit is contained in:
@@ -124,6 +124,7 @@ class MessagesNotifier extends _$MessagesNotifier {
|
|||||||
}) async {
|
}) async {
|
||||||
talker.log('Getting cached messages from offset $offset, take $take');
|
talker.log('Getting cached messages from offset $offset, take $take');
|
||||||
final List<LocalChatMessage> dbMessages;
|
final List<LocalChatMessage> dbMessages;
|
||||||
|
|
||||||
if (searchQuery != null && searchQuery.isNotEmpty) {
|
if (searchQuery != null && searchQuery.isNotEmpty) {
|
||||||
dbMessages = await _database.searchMessages(
|
dbMessages = await _database.searchMessages(
|
||||||
_roomId,
|
_roomId,
|
||||||
@@ -147,6 +148,13 @@ class MessagesNotifier extends _$MessagesNotifier {
|
|||||||
filteredMessages.where((msg) => _hasLink(msg)).toList();
|
filteredMessages.where((msg) => _hasLink(msg)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (withAttachments == true) {
|
||||||
|
filteredMessages =
|
||||||
|
filteredMessages
|
||||||
|
.where((msg) => msg.toRemoteMessage().attachments.isNotEmpty)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
final dbLocalMessages = filteredMessages;
|
final dbLocalMessages = filteredMessages;
|
||||||
|
|
||||||
// Always ensure unique messages to prevent duplicate keys
|
// Always ensure unique messages to prevent duplicate keys
|
||||||
@@ -778,18 +786,23 @@ class MessagesNotifier extends _$MessagesNotifier {
|
|||||||
bool? withAttachments,
|
bool? withAttachments,
|
||||||
}) async {
|
}) async {
|
||||||
final trimmedQuery = query.trim();
|
final trimmedQuery = query.trim();
|
||||||
|
final hasFilters = [withLinks, withAttachments].any((e) => e == true);
|
||||||
|
|
||||||
if (trimmedQuery.isEmpty) {
|
if (trimmedQuery.isEmpty && !hasFilters) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
talker.log('Getting search results for query: $trimmedQuery');
|
talker.log(
|
||||||
|
'Getting search results for query: $trimmedQuery, filters: links=$withLinks, attachments=$withAttachments',
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// When filtering without query, get more messages to ensure we find all matches
|
||||||
|
final take = (trimmedQuery.isEmpty && hasFilters) ? 1000 : 50;
|
||||||
final messages = await _getCachedMessages(
|
final messages = await _getCachedMessages(
|
||||||
offset: 0,
|
offset: 0,
|
||||||
take: 50,
|
take: take,
|
||||||
searchQuery: trimmedQuery,
|
searchQuery: trimmedQuery.isNotEmpty ? trimmedQuery : null,
|
||||||
withLinks: withLinks,
|
withLinks: withLinks,
|
||||||
withAttachments: withAttachments,
|
withAttachments: withAttachments,
|
||||||
); // Limit initial search results
|
); // Limit initial search results
|
||||||
|
@@ -130,7 +130,10 @@ class SearchMessagesScreen extends HookConsumerWidget {
|
|||||||
|
|
||||||
// Optimized search function with debouncing
|
// Optimized search function with debouncing
|
||||||
void performSearch(String query) async {
|
void performSearch(String query) async {
|
||||||
if (query.trim().isEmpty) {
|
final trimmedQuery = query.trim();
|
||||||
|
final hasFilters = withLinks.value || withAttachments.value;
|
||||||
|
|
||||||
|
if (trimmedQuery.isEmpty && !hasFilters) {
|
||||||
searchState.value = SearchState.idle;
|
searchState.value = SearchState.idle;
|
||||||
searchResultCount.value = null;
|
searchResultCount.value = null;
|
||||||
searchResults.value = const AsyncValue.data([]);
|
searchResults.value = const AsyncValue.data([]);
|
||||||
|
Reference in New Issue
Block a user