Compare commits

...

2 Commits

3 changed files with 39 additions and 13 deletions

View File

@ -41,19 +41,25 @@ class ContactsService {
final Map<String, String?> _contactDisplayNames = {};
Future<void> initialize() async {
if (await _canUseContactIntegration()) {
enableDatabaseListener();
await enable(shouldScan: false);
}
/// Enable listening to contact database events. If [shouldScan] is true, also
/// performs a scan of the contacts database, if we're allowed.
Future<void> enable({bool shouldScan = true}) async {
FlutterContacts.addListener(_onContactsDatabaseUpdate);
if (shouldScan && await _canUseContactIntegration()) {
unawaited(scanContacts());
}
}
/// Enable listening to contact database events
void enableDatabaseListener() {
FlutterContacts.addListener(_onContactsDatabaseUpdate);
}
/// Disable listening to contact database events
void disableDatabaseListener() {
/// Disable listening to contact database events. Also removes all roster items
/// that are pseudo roster items.
Future<void> disable() async {
FlutterContacts.removeListener(_onContactsDatabaseUpdate);
await GetIt.I.get<RosterService>().removePseudoRosterItems();
}
Future<void> _onContactsDatabaseUpdate() async {
@ -123,7 +129,6 @@ class ContactsService {
Future<Map<String, String>> _getContactIds() async {
if (_contactIds != null) return _contactIds!;
// TODO(Unknown): Can we just .cast<String, String>() here?
_contactIds = Map<String, String>.fromEntries(
(await GetIt.I.get<DatabaseService>().database.query(contactsTable)).map(
(item) => MapEntry(
@ -276,7 +281,7 @@ class ContactsService {
return cs.updateConversation(
contact.jid,
contactId: contact.id,
contactAvatarPath: contactAvatarPath,
contactAvatarPath: contact.thumbnail != null ? contactAvatarPath : null,
contactDisplayName: contact.displayName,
);
},

View File

@ -394,13 +394,13 @@ Future<void> performSetPreferences(
final css = GetIt.I.get<ContactsService>();
if (command.preferences.enableContactIntegration) {
if (!oldPrefs.enableContactIntegration) {
css.enableDatabaseListener();
await css.enable();
}
unawaited(css.scanContacts());
} else {
if (oldPrefs.enableContactIntegration) {
css.disableDatabaseListener();
await css.disable();
}
}

View File

@ -257,4 +257,25 @@ class RosterService {
return false;
}
/// Removes all roster items that are pseudo roster items.
Future<void> removePseudoRosterItems() async {
final items = await getRoster();
final removed = List<String>.empty(growable: true);
for (final item in items) {
if (!item.pseudoRosterItem) continue;
assert(
item.contactId != null,
'Only pseudo roster items that are for the contact integration should ge removed',
);
removed.add(item.jid);
await removeRosterItem(item.id);
}
sendEvent(
RosterDiffEvent(removed: removed),
);
}
}