Skip to content

Commit

Permalink
TW-1551: Add test for combineDuplicateContactAndChat func
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored and hoangdat committed Mar 11, 2024
1 parent a81ef9b commit 1c0afe9
Show file tree
Hide file tree
Showing 4 changed files with 835 additions and 21 deletions.
24 changes: 3 additions & 21 deletions lib/pages/search/search_contacts_and_chats_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:fluffychat/domain/app_state/search/search_state.dart';
import 'package:fluffychat/domain/usecase/search/search_recent_chat_interactor.dart';
import 'package:fluffychat/domain/contact_manager/contacts_manager.dart';
import 'package:fluffychat/pages/search/search_debouncer_mixin.dart';
import 'package:fluffychat/pages/search/search_mixin.dart';
import 'package:fluffychat/presentation/extensions/contact/presentation_contact_extension.dart';
import 'package:fluffychat/presentation/model/search/presentation_search.dart';
import 'package:fluffychat/presentation/model/search/presentation_search_state_extension.dart';
Expand All @@ -15,7 +16,7 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';

class SearchContactsAndChatsController with SearchDebouncerMixin {
class SearchContactsAndChatsController with SearchDebouncerMixin, SearchMixin {
final BuildContext context;

SearchContactsAndChatsController(this.context);
Expand Down Expand Up @@ -114,7 +115,7 @@ class SearchContactsAndChatsController with SearchDebouncerMixin {
event.map(
(success) {
if (success is SearchRecentChatSuccess) {
recentAndContactsNotifier.value = _combineDuplicateContactAndChat(
recentAndContactsNotifier.value = combineDuplicateContactAndChat(
recentChat: success.toPresentation().contacts,
contacts: tomContactPresentationSearchMatched,
);
Expand All @@ -125,25 +126,6 @@ class SearchContactsAndChatsController with SearchDebouncerMixin {
);
}

List<PresentationSearch> _combineDuplicateContactAndChat({
required List<PresentationSearch> contacts,
required List<PresentationSearch> recentChat,
}) {
final contactNames = contacts
.map(
(contact) => contact.displayName?.toLowerCase(),
)
.toSet();

final filteredRecentChat = recentChat
.where(
(chat) => !contactNames.contains(chat.displayName?.toLowerCase()),
)
.toList();

return [...contacts, ...filteredRecentChat];
}

void onSearchBarChanged(String keyword) {
setDebouncerValue(keyword);
}
Expand Down
25 changes: 25 additions & 0 deletions lib/pages/search/search_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:fluffychat/presentation/model/search/presentation_search.dart';

mixin SearchMixin {
List<PresentationSearch> combineDuplicateContactAndChat({
required List<PresentationSearch> contacts,
required List<PresentationSearch> recentChat,
}) {
final Map<String, PresentationSearch> distinctContactsAndChatsMap = {};

for (final contact in contacts) {
distinctContactsAndChatsMap[contact.id] = contact;
}

for (final chat in recentChat) {
final isContactHasChat =
distinctContactsAndChatsMap[chat.directChatMatrixID ?? chat.id] !=
null;
if (!isContactHasChat) {
distinctContactsAndChatsMap[chat.id] = chat;
}
}

return distinctContactsAndChatsMap.values.toList();
}
}
6 changes: 6 additions & 0 deletions lib/presentation/model/search/presentation_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ abstract class PresentationSearch extends Equatable {
this.displayName,
this.directChatMatrixID,
});

@override
List<Object?> get props => [
displayName,
directChatMatrixID,
];
}

class ContactPresentationSearch extends PresentationSearch {
Expand Down
Loading

0 comments on commit 1c0afe9

Please sign in to comment.