feat: add favorites with match-quality-aware ranking
This commit is contained in:
@@ -8,9 +8,11 @@ public final class LaunchHistory {
|
||||
}
|
||||
|
||||
private static let storageKey = "applicationLaunchHistory"
|
||||
private static let favoritesStorageKey = "applicationFavorites"
|
||||
private let defaults: UserDefaults
|
||||
private let now: () -> Date
|
||||
private var usage: [URL: Usage] = [:]
|
||||
private var favorites = Set<URL>()
|
||||
|
||||
public init(defaults: UserDefaults = .standard, now: @escaping () -> Date = Date.init) {
|
||||
self.defaults = defaults
|
||||
@@ -23,6 +25,21 @@ public final class LaunchHistory {
|
||||
lastLaunch.timeIntervalSinceReferenceDate.isFinite else { continue }
|
||||
usage[url] = Usage(count: count, lastLaunch: lastLaunch)
|
||||
}
|
||||
for identifier in defaults.stringArray(forKey: Self.favoritesStorageKey) ?? [] {
|
||||
guard let url = URL(string: identifier), url.isFileURL else { continue }
|
||||
favorites.insert(url)
|
||||
}
|
||||
}
|
||||
|
||||
public func isFavorite(_ application: Application) -> Bool {
|
||||
favorites.contains(application.id)
|
||||
}
|
||||
|
||||
public func toggleFavorite(_ application: Application) {
|
||||
if favorites.remove(application.id) == nil {
|
||||
favorites.insert(application.id)
|
||||
}
|
||||
defaults.set(favorites.map(\.absoluteString), forKey: Self.favoritesStorageKey)
|
||||
}
|
||||
|
||||
public func recordLaunch(of application: Application) {
|
||||
@@ -57,6 +74,9 @@ public final class LaunchHistory {
|
||||
}
|
||||
|
||||
private func preferred(_ lhs: Application, _ rhs: Application) -> Bool {
|
||||
let leftFavorite = isFavorite(lhs)
|
||||
let rightFavorite = isFavorite(rhs)
|
||||
if leftFavorite != rightFavorite { return leftFavorite }
|
||||
let left = usage[lhs.id]
|
||||
let right = usage[rhs.id]
|
||||
let leftCount = left?.count ?? 0
|
||||
|
||||
Reference in New Issue
Block a user