Autocomplete: tweaks to allow some top level paths
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user