findDeviceContactMatches method

Future<Iterable<TermSearchResult<Contact>>> findDeviceContactMatches(
  1. ISunnyContact sunnyContact, {
  2. int? limit,
})

Implementation

Future<Iterable<TermSearchResult<Contact>>> findDeviceContactMatches(ISunnyContact sunnyContact, {int? limit}) async {
  final allDeviceContacts = Contacts.streamContacts(withThumbnails: false, withHiResPhoto: false);
  final tokens = [
    for (final t in sunnyContactTokenizer(sunnyContact).orEmpty())
      if (t is Token) t.value else "$t",
  ].whereNotBlank();
  final initial = [
    ...(await FullTextSearch.ofStream(
                term: tokens.join(" "),
                items: allDeviceContacts,
                isMatchAll: false,
                isStartsWith: true,
                limit: limit ?? 2,
                tokenize: deviceContactTokenizer)
            .execute())
        .where((r) => r.matchedTerms.isNotEmpty)
  ];
  initial.sort();

  final reloaded = await initial.map((r) async {
    final loaded = await Contacts.getContact(r.result.identifier!, withThumbnails: true);
    return TermSearchResult(loaded!, r.matchedTerms, r.matchedTokens, r.matchAll);
  }).awaitAll();

  return reloaded;
}