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