Skip to content

Commit

Permalink
TW-1963: update search keyword matching (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z authored Sep 13, 2024
1 parent 01527b8 commit 2c905f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
22 changes: 17 additions & 5 deletions lib/domain/model/room/room_list_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import 'package:fluffychat/domain/model/search/recent_chat_model.dart';
import 'package:matrix/matrix.dart';

extension RoomListExtension on List<Room> {
bool _matchedMatrixId(RecentChatSearchModel model, String keyword) {
return model.directChatMatrixID
?.toLowerCase()
.contains(keyword.toLowerCase()) ??
false;
}

bool _matchedName(RecentChatSearchModel model, String keyword) {
return model.displayName?.toLowerCase().contains(keyword.toLowerCase()) ??
false;
}

bool _matchedNameOrMatrixId(RecentChatSearchModel model, String keyword) {
return _matchedName(model, keyword) || _matchedMatrixId(model, keyword);
}

List<RecentChatSearchModel> searchRecentChat({
required MatrixLocalizations matrixLocalizations,
required String keyword,
Expand All @@ -13,11 +29,7 @@ extension RoomListExtension on List<Room> {
)
.map((room) => room.toRecentChatSearchModel(matrixLocalizations))
.where(
(model) =>
model.displayName != null &&
model.displayName!.toLowerCase().contains(
keyword.toLowerCase(),
),
(model) => _matchedNameOrMatrixId(model, keyword),
)
.take(limit ?? length)
.toList();
Expand Down
62 changes: 41 additions & 21 deletions lib/pages/search/search_contacts_and_chats_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,8 @@ class SearchContactsAndChatsController with SearchDebouncerMixin, SearchMixin {
.toList();
final tomContactPresentationSearchMatched = tomPresentationSearchContacts
.expand((contact) => contact.toPresentationSearch())
.where((contact) {
if (contact is! ContactPresentationSearch) {
return false;
}

if (contact.displayName == null) {
return false;
}

if (contact.email == null) {
return false;
}

final matchedName =
contact.displayName!.toLowerCase().contains(keyword.toLowerCase());

final matchedEmail =
contact.email!.toLowerCase().contains(keyword.toLowerCase());

return matchedName || matchedEmail;
}).toList();
.where((contact) => _doesMatchKeyword(contact, keyword))
.toList();
_searchRecentChatInteractor
.execute(
keyword: keyword,
Expand All @@ -126,6 +107,45 @@ class SearchContactsAndChatsController with SearchDebouncerMixin, SearchMixin {
);
}

bool _matchedMatrixId(PresentationSearch contact, String keyword) {
return contact.directChatMatrixID
?.toLowerCase()
.contains(keyword.toLowerCase()) ??
false;
}

bool _matchedName(PresentationSearch contact, String keyword) {
return contact.displayName?.toLowerCase().contains(keyword.toLowerCase()) ??
false;
}

bool _matchedEmail(PresentationSearch contact, String keyword) {
return contact.email?.toLowerCase().contains(keyword.toLowerCase()) ??
false;
}

bool _matchedContactInfo(PresentationSearch contact, String keyword) {
return _matchedName(contact, keyword) ||
_matchedEmail(contact, keyword) ||
_matchedMatrixId(contact, keyword);
}

bool _doesMatchKeyword(PresentationSearch contact, String keyword) {
if (contact is! ContactPresentationSearch) {
return false;
}

if (contact.displayName == null) {
return false;
}

if (contact.email == null) {
return false;
}

return _matchedContactInfo(contact, keyword);
}

void onSearchBarChanged(String keyword) {
setDebouncerValue(keyword);
}
Expand Down

0 comments on commit 2c905f8

Please sign in to comment.