Skip to content

feat: Sort autocompletion result #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Mail/Views/New Message/AutocompletionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,27 @@ struct AutocompletionView: View {
}

private nonisolated func sortByRemoteAndName(lhs: MergedContact, rhs: MergedContact) -> Bool {
if lhs.isRemote != rhs.isRemote {
return lhs.isRemote && !rhs.isRemote
} else {
return lhs.name.localizedStandardCompare(rhs.name) == .orderedAscending
var lhsWeight = lhs.remoteContactedTimes ?? 0
var rhsWeight = rhs.remoteContactedTimes ?? 0

if rhs.name.isEmpty || rhs.remoteOther {
rhsWeight = -1
}

if lhs.name.isEmpty || lhs.remoteOther {
lhsWeight = -1
}

if lhsWeight > rhsWeight {
return true
} else if lhsWeight == rhsWeight {
if lhs.isRemote != rhs.isRemote {
return lhs.isRemote && !rhs.isRemote
} else {
return lhs.name.localizedStandardCompare(rhs.name) == .orderedAscending
}
}
return false
}
}

Expand Down
2 changes: 1 addition & 1 deletion MailCore/API/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public extension Endpoint {
static var contacts: Endpoint {
return .base.appending(
path: "/pim/contact/all",
queryItems: [URLQueryItem(name: "with", value: "emails,details,others"),
queryItems: [URLQueryItem(name: "with", value: "emails,details,others,contacted_times"),
URLQueryItem(name: "filters", value: "has_email")]
)
}
Expand Down
7 changes: 7 additions & 0 deletions MailCore/Models/Contact/Contact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public struct InfomaniakContact: Codable, Identifiable {
public var nickname: String?
public var organization: String?
public var groupContactId: List<Int>?
public var contactedTimes: Int?

enum CodingKeys: String, CodingKey {
case id
Expand All @@ -51,6 +52,7 @@ public struct InfomaniakContact: Codable, Identifiable {
case nickname
case organization
case groupContactId = "categories"
case contactedTimes
}

public init(from decoder: Decoder) throws {
Expand All @@ -74,5 +76,10 @@ public struct InfomaniakContact: Codable, Identifiable {
nickname = try values.decodeIfPresent(String.self, forKey: .nickname)
organization = try values.decodeIfPresent(String.self, forKey: .organization)
groupContactId = try values.decodeIfPresent(List<Int>.self, forKey: .groupContactId)
if let rawContactedTimes = try? values.decodeIfPresent([String: Int].self, forKey: .contactedTimes) {
contactedTimes = rawContactedTimes.reduce(into: 0) { result, element in
result += element.value
}
}
}
}
4 changes: 4 additions & 0 deletions MailCore/Models/MergedContact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final class MergedContact: Object, Identifiable, Correspondent {
@Persisted public var remoteIdentifier: String?
@Persisted public var remoteAddressBookId: Int?
@Persisted public var remoteGroupContactId: List<Int>
@Persisted public var remoteContactedTimes: Int?
@Persisted public var remoteOther: Bool

/// Local
@Persisted public var localIdentifier: String?
Expand Down Expand Up @@ -130,6 +132,8 @@ public final class MergedContact: Object, Identifiable, Correspondent {
remoteIdentifier = contact.id
remoteAddressBookId = contact.addressbookId
remoteGroupContactId = contact.groupContactId ?? List<Int>()
remoteContactedTimes = contact.contactedTimes
remoteOther = contact.other
}

static func computeId(email: String, name: String?) -> String {
Expand Down
Loading