feat: use launch history to break search ranking ties
This commit is contained in:
@@ -89,8 +89,15 @@ public enum ApplicationCatalog {
|
||||
|
||||
public enum ApplicationSearch {
|
||||
public static func results(for query: String, in applications: [Application]) -> [Application] {
|
||||
results(for: query, in: applications, breakingTiesWith: alphabetical)
|
||||
}
|
||||
|
||||
static func results(
|
||||
for query: String, in applications: [Application],
|
||||
breakingTiesWith preferred: (Application, Application) -> Bool
|
||||
) -> [Application] {
|
||||
let query = folded(query.trimmingCharacters(in: .whitespacesAndNewlines))
|
||||
guard !query.isEmpty else { return applications.sorted(by: alphabetical) }
|
||||
guard !query.isEmpty else { return applications.sorted(by: preferred) }
|
||||
let characters = Array(query)
|
||||
var ranked = applications.compactMap { application -> (Application, Rank)? in
|
||||
let name = application.searchableName
|
||||
@@ -110,7 +117,7 @@ public enum ApplicationSearch {
|
||||
}
|
||||
ranked.sort { lhs, rhs in
|
||||
if lhs.1 != rhs.1 { return lhs.1 < rhs.1 }
|
||||
return alphabetical(lhs.0, rhs.0)
|
||||
return preferred(lhs.0, rhs.0)
|
||||
}
|
||||
return ranked.map(\.0)
|
||||
}
|
||||
|
||||
@@ -40,16 +40,22 @@ public final class LaunchHistory {
|
||||
}
|
||||
|
||||
public func suggestions(in applications: [Application]) -> [Application] {
|
||||
applications.sorted { lhs, rhs in
|
||||
let left = usage[lhs.id]
|
||||
let right = usage[rhs.id]
|
||||
let leftCount = left?.count ?? 0
|
||||
let rightCount = right?.count ?? 0
|
||||
if leftCount != rightCount { return leftCount > rightCount }
|
||||
if let left, let right, left.lastLaunch != right.lastLaunch {
|
||||
return left.lastLaunch > right.lastLaunch
|
||||
}
|
||||
return alphabetical(lhs, rhs)
|
||||
applications.sorted(by: preferred)
|
||||
}
|
||||
|
||||
public func results(for query: String, in applications: [Application]) -> [Application] {
|
||||
ApplicationSearch.results(for: query, in: applications, breakingTiesWith: preferred)
|
||||
}
|
||||
|
||||
private func preferred(_ lhs: Application, _ rhs: Application) -> Bool {
|
||||
let left = usage[lhs.id]
|
||||
let right = usage[rhs.id]
|
||||
let leftCount = left?.count ?? 0
|
||||
let rightCount = right?.count ?? 0
|
||||
if leftCount != rightCount { return leftCount > rightCount }
|
||||
if let left, let right, left.lastLaunch != right.lastLaunch {
|
||||
return left.lastLaunch > right.lastLaunch
|
||||
}
|
||||
return alphabetical(lhs, rhs)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user