Skip to content

Commit

Permalink
fixup! TW-888 Synchronize Contact refactor interactors
Browse files Browse the repository at this point in the history
  • Loading branch information
drminh2807 committed Nov 15, 2023
1 parent e2f81ec commit f884522
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
18 changes: 16 additions & 2 deletions lib/domain/contact_manager/contacts_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:fluffychat/domain/model/contact/contact.dart';
import 'package:fluffychat/domain/model/contact/contacts_manager_agruments.dart';
import 'package:fluffychat/domain/usecase/get_all_contacts_interactor.dart';
import 'package:fluffychat/domain/usecase/lookup_mxid_interactor.dart';
import 'package:fluffychat/domain/usecase/phonebook_contact_interactor.dart';
import 'package:fluffychat/utils/permission_service.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter_contacts/flutter_contacts.dart' hide Contact;
Expand All @@ -25,7 +26,7 @@ enum DisplayWaringContactsBannerState {
}

class ContactsManager {
static const int _lookupChunkSize = 10;
static const int _lookupChunkSize = 50;

SyncContactsState _syncContactsState = SyncContactsState.initial;

Expand All @@ -40,6 +41,8 @@ class ContactsManager {

final _lookupMxidInteractor = getIt.get<LookupMxidInteractor>();

final _phonebookContactInteractor = getIt.get<PhonebookContactInteractor>();

final PermissionHandlerService permissionHandlerService =
PermissionHandlerService();

Expand Down Expand Up @@ -112,8 +115,19 @@ class ContactsManager {
}

void _fetchPhonebookContacts() async {
_phonebookContactInteractor.execute().listen((contacts) {
_phonebookContacts = contacts.toSet();
_handleContactsHasDataChange();
_lookupContacts(contacts);
});
}

void _lookupContacts(List<Contact> contacts) {
_lookupMxidInteractor
.execute(lookupChunkSize: _lookupChunkSize)
.execute(
phonebookContacts: contacts,
lookupChunkSize: _lookupChunkSize,
)
.listen((contacts) {
_phonebookContacts = contacts.toSet();
_handleContactsHasDataChange();
Expand Down
17 changes: 7 additions & 10 deletions lib/domain/usecase/lookup_mxid_interactor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import 'package:fluffychat/domain/model/contact/contact.dart';
import 'package:fluffychat/domain/model/contact/contact_status.dart';
import 'package:fluffychat/domain/model/contact/lookup_list_mxid_request.dart';
import 'package:fluffychat/domain/repository/lookup_repository.dart';
import 'package:fluffychat/domain/repository/phonebook_contact_repository.dart';
import 'package:matrix/matrix.dart';

class LookupMxidInteractor {
final LookupRepository _lookupRepository = getIt.get<LookupRepository>();

final PhonebookContactRepository _phonebookContactRepository =
getIt.get<PhonebookContactRepository>();

Stream<List<Contact>> execute({int lookupChunkSize = 50}) async* {
Stream<List<Contact>> execute({
required List<Contact> phonebookContacts,
int lookupChunkSize = 50,
}) async* {
try {
var contacts = await _phonebookContactRepository.fetchContacts();
yield contacts;
final hashDetails = await _lookupRepository.getHashDetails();
final thirdPartyIdToHashMap = {
for (final contact in contacts)
for (final contact in phonebookContacts)
contact.thirdPartyId:
contact.calLookupAddress(hashDetails: hashDetails),
};
Expand All @@ -33,7 +30,7 @@ class LookupMxidInteractor {
pepper: hashDetails.lookupPepper,
),
);
contacts = contacts.map((contact) {
phonebookContacts = phonebookContacts.map((contact) {
if (contact.thirdPartyId != null) {
final hash = thirdPartyIdToHashMap[contact.thirdPartyId];
if (hash != null && response.mappings?[hash] != null) {
Expand All @@ -51,7 +48,7 @@ class LookupMxidInteractor {
}
return contact;
}).toList();
yield contacts;
yield phonebookContacts;
}
} catch (e) {
Logs().e('LookupMxidInteractor::error', e);
Expand Down
19 changes: 19 additions & 0 deletions lib/domain/usecase/phonebook_contact_interactor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/model/contact/contact.dart';
import 'package:fluffychat/domain/repository/phonebook_contact_repository.dart';
import 'package:matrix/matrix.dart';

class PhonebookContactInteractor {
final PhonebookContactRepository _phonebookContactRepository =
getIt.get<PhonebookContactRepository>();

Stream<List<Contact>> execute() async* {
try {
final contacts = await _phonebookContactRepository.fetchContacts();
yield contacts;
} catch (e) {
Logs().e('PhonebookContactInteractor::error', e);
yield [];
}
}
}

0 comments on commit f884522

Please sign in to comment.