Autocomplete: tweaks to allow some top level paths

This commit is contained in:
James Magahern
2022-02-18 18:11:34 -08:00
parent 96de5ff23c
commit f6b90e209d

View File

@@ -53,12 +53,14 @@ class BrowserHistory
let fetchRequest: NSFetchRequest<HistoryItemEntity> = HistoryItemEntity.fetchRequest() let fetchRequest: NSFetchRequest<HistoryItemEntity> = HistoryItemEntity.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "host CONTAINS %@ OR title contains %@", matching, matching) fetchRequest.predicate = NSPredicate(format: "host CONTAINS %@ OR title contains %@", matching, matching)
fetchRequest.fetchLimit = 100
let entities: [HistoryItemEntity] = (try? dataContext.fetch(fetchRequest)) ?? [] let entities: [HistoryItemEntity] = (try? dataContext.fetch(fetchRequest)) ?? []
let allItems: [HistoryItem] = entities.map { HistoryItem(entity: $0) } let allItems: [HistoryItem] = entities.map { HistoryItem(entity: $0) }
var topLevelItems: [URL: (HistoryItem, Int)] = [:] var topLevelItems: [URL: (HistoryItem, Int)] = [:]
allItems.forEach { item in for item in allItems {
if item.url.pathComponents.count <= 2 {
let topLevelURL = item.url.topLevelURL() let topLevelURL = item.url.topLevelURL()
var topLevelItem = topLevelItems[topLevelURL] ?? (item, 0) var topLevelItem = topLevelItems[topLevelURL] ?? (item, 0)
topLevelItem.0.url = topLevelURL topLevelItem.0.url = topLevelURL
@@ -67,6 +69,11 @@ class BrowserHistory
} }
topLevelItem.1 += 1 topLevelItem.1 += 1
topLevelItems[topLevelURL] = topLevelItem topLevelItems[topLevelURL] = topLevelItem
if topLevelItems.count == 20 {
break
}
}
} }
return topLevelItems.values.map { return $0.0 }.sorted { (item1, item2) -> Bool in return topLevelItems.values.map { return $0.0 }.sorted { (item1, item2) -> Bool in
@@ -79,7 +86,6 @@ extension URL
{ {
public func topLevelURL() -> URL { public func topLevelURL() -> URL {
if var components = URLComponents(url: self, resolvingAgainstBaseURL: false) { if var components = URLComponents(url: self, resolvingAgainstBaseURL: false) {
components.path = ""
components.query = nil components.query = nil
components.queryItems = nil components.queryItems = nil
components.fragment = nil components.fragment = nil