Skip to content

Commit

Permalink
Finding URL variants refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstrba committed Dec 6, 2024
1 parent 39e099c commit b99ed35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 16 additions & 1 deletion DuckDuckGo/Bookmarks/Model/BookmarkList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ struct BookmarkList {

private(set) var allBookmarkURLsOrdered: [IdentifiableBookmark]
private var favoriteBookmarksOrdered: [IdentifiableBookmark]
private var itemsDict: [String: [Bookmark]]
private var itemsDict: [String: [Bookmark]] {
didSet {
updateLowercasedItemsDict()
}
}

// Copy of itemsDict used for efficient lookups of variant URLs
private(set) var lowercasedItemsDict: [String: [Bookmark]] = [:]

var totalBookmarks: Int {
return allBookmarkURLsOrdered.count
Expand Down Expand Up @@ -76,6 +83,7 @@ struct BookmarkList {
self.allBookmarkURLsOrdered = keysOrdered
self.itemsDict = itemsDict
self.topLevelEntities = topLevelEntities
updateLowercasedItemsDict()
}

mutating func insert(_ bookmark: Bookmark) {
Expand Down Expand Up @@ -149,6 +157,13 @@ struct BookmarkList {
return allBookmarkURLsOrdered
}

private mutating func updateLowercasedItemsDict() {
lowercasedItemsDict = itemsDict.reduce(into: [:]) { result, entry in
let lowercasedKey = entry.key.lowercased()
result[lowercasedKey] = entry.value
}
}

}

private extension BookmarkList {
Expand Down
10 changes: 6 additions & 4 deletions DuckDuckGo/Bookmarks/Model/BookmarkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ final class LocalBookmarkManager: BookmarkManager {

/// Finds a bookmark by checking all possible URL variants (http/https, trailing slash).
private func findBookmark(forVariantUrl url: URL) -> Bookmark? {
let urlVariants = url.bookmarkButtonUrlVariants()
guard let list = list else {
return nil
}

for variant in urlVariants {
if let bookmark = getBookmark(for: variant) {
for variant in url.bookmarkButtonUrlVariants() {
let variantString = variant.absoluteString
if let bookmark = list.lowercasedItemsDict[variantString]?.first {
return bookmark
}
}

return nil
}

Expand Down

0 comments on commit b99ed35

Please sign in to comment.