Skip to content

Commit 460edfc

Browse files
LnamwMatthieu-dgl
authored andcommitted
feat: Create func to get the groupContact
1 parent 29505a8 commit 460edfc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

MailCore/Cache/ContactManager/ContactManager+DB.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public protocol ContactFetchable {
3131
func frozenContactsAsync(matching string: String, fetchLimit: Int?, sorted: ((MergedContact, MergedContact) -> Bool)?) async
3232
-> any Collection<MergedContact>
3333

34+
/// Case and diacritic insensitive search for a `GroupContact`
35+
/// - Parameters:
36+
/// - string: input string to match against email and name
37+
/// - fetchLimit: limit the query by default to limit memory footprint
38+
/// - Returns: The collection of matching contacts.
39+
func frozenGroupContacts(matching string: String, fetchLimit: Int?) -> any Collection<GroupContact>
40+
3441
/// Get a contact from a given transactionable
3542
func getContact(for correspondent: any Correspondent, transactionable: Transactionable) -> MergedContact?
3643

@@ -43,6 +50,7 @@ public protocol ContactFetchable {
4350
public extension ContactManager {
4451
/// Both *case* insensitive __and__ *diacritic* (accents) insensitive
4552
static let searchContactInsensitivePredicate = "name contains[cd] %@ OR email contains[cd] %@"
53+
static let searchGroupContactInsensitivePredicate = "name contains[cd] %@"
4654

4755
/// Making sure, that by default, we do not overflow memory with too much contacts
4856
private static let contactFetchLimit = 120
@@ -76,6 +84,25 @@ public extension ContactManager {
7684
return frozenContacts(matching: string, fetchLimit: fetchLimit, sorted: sorted)
7785
}
7886

87+
/// Case and diacritic insensitive search for a `GroupContact`
88+
/// - Parameters:
89+
/// - string: input string to match against email and name
90+
/// - fetchLimit: limit the query by default to limit memory footprint
91+
/// - Returns: The collection of matching contacts. Frozen.
92+
func frozenGroupContacts(matching string: String, fetchLimit: Int?) -> any Collection<GroupContact> {
93+
var lazyResults = fetchResults(ofType: GroupContact.self) { partial in
94+
partial
95+
}
96+
lazyResults = lazyResults
97+
.filter(Self.searchGroupContactInsensitivePredicate, string, string)
98+
.freeze()
99+
100+
let fetchLimit = min(lazyResults.count, fetchLimit ?? Self.contactFetchLimit)
101+
102+
let limitedResults = lazyResults[0 ..< fetchLimit]
103+
return limitedResults
104+
}
105+
79106
func getContact(for correspondent: any Correspondent) -> MergedContact? {
80107
getContact(for: correspondent, transactionable: self)
81108
}

0 commit comments

Comments
 (0)