Skip to content

Commit

Permalink
Remove run loop use, use DI for Bookmarks Caching Search (#2854)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/856498667320406/1207322485119026/f
Tech Design URL:
CC:

Description:

Remove run loop usage from bookmarks caching search.
  • Loading branch information
bwaresiak authored May 15, 2024
1 parent 5579070 commit 7903952
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
14 changes: 2 additions & 12 deletions Core/BookmarksCachingSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public class BookmarksCachingSearch: BookmarksStringSearch {
self?.refreshCache()
}

loadCache()
refreshCache()
}

public var hasData: Bool {
Expand All @@ -181,26 +181,16 @@ public class BookmarksCachingSearch: BookmarksStringSearch {
private var cachedBookmarksAndFavorites = [ScoredBookmark]()
private var cacheLoadedCondition = RunLoop.ResumeCondition()

private func loadCache() {
private func refreshCache() {
bookmarksStore.bookmarksAndFavorites { result in
self.cachedBookmarksAndFavorites = result
if !self.cacheLoadedCondition.isResolved {
self.cacheLoadedCondition.resolve()
}
}
}

private var bookmarksAndFavorites: [ScoredBookmark] {
RunLoop.current.run(until: cacheLoadedCondition)
return cachedBookmarksAndFavorites
}

public func refreshCache() {
// setting cacheLoadedCondition back to initialized state
cacheLoadedCondition = RunLoop.ResumeCondition()
loadCache()
}

// swiftlint:disable cyclomatic_complexity
private func score(query: String, input: [ScoredBookmark]) -> [ScoredBookmark] {
let query = query.lowercased()
Expand Down
8 changes: 4 additions & 4 deletions DuckDuckGo/AutocompleteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class AutocompleteViewController: UIViewController {
CachedBookmarks(bookmarksDatabase)
}()

private lazy var cachedBookmarksSearch: BookmarksStringSearch = {
BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase))
}()
private var bookmarksStringSearch: BookmarksStringSearch!

var backgroundColor: UIColor {
appSettings.currentAddressBarPosition.isBottom ?
Expand All @@ -88,6 +86,7 @@ class AutocompleteViewController: UIViewController {
var shouldOffsetY = false

static func loadFromStoryboard(bookmarksDatabase: CoreDataDatabase,
bookmarksStringSearch: BookmarksStringSearch,
historyCoordinator: HistoryCoordinating,
appSettings: AppSettings = AppDependencyProvider.shared.appSettings,
variantManager: VariantManager = DefaultVariantManager()) -> AutocompleteViewController {
Expand All @@ -96,6 +95,7 @@ class AutocompleteViewController: UIViewController {
fatalError("Failed to instatiate correct Autocomplete view controller")
}
controller.bookmarksDatabase = bookmarksDatabase
controller.bookmarksStringSearch = bookmarksStringSearch
controller.historyCoordinator = historyCoordinator
controller.appSettings = appSettings
controller.variantManager = variantManager
Expand Down Expand Up @@ -192,7 +192,7 @@ class AutocompleteViewController: UIViewController {
if variantManager.inSuggestionExperiment {
bookmarks = [] // We'll supply bookmarks elsewhere
} else {
bookmarks = cachedBookmarksSearch.search(query: query).prefix(2).map {
bookmarks = bookmarksStringSearch.search(query: query).prefix(2).map {
.bookmark(title: $0.title, url: $0.url, isFavorite: $0.isFavorite, allowedInTopHits: true)
}
}
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ class MainViewController: UIViewController {
SuggestionTrayViewController(coder: coder,
favoritesViewModel: self.favoritesViewModel,
bookmarksDatabase: self.bookmarksDatabase,
historyCoordinator: self.historyManager.historyCoordinator)
historyCoordinator: self.historyManager.historyCoordinator,
bookmarksStringSearch: self.bookmarksCachingSearch)
}) else {
assertionFailure()
return
Expand Down
8 changes: 6 additions & 2 deletions DuckDuckGo/SuggestionTrayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SuggestionTrayViewController: UIViewController {
private let bookmarksDatabase: CoreDataDatabase
private let favoritesModel: FavoritesListInteracting
private let historyCoordinator: HistoryCoordinating
private let bookmarksStringSearch: BookmarksStringSearch

var selectedSuggestion: Suggestion? {
autocompleteController?.selectedSuggestion
Expand Down Expand Up @@ -76,10 +77,11 @@ class SuggestionTrayViewController: UIViewController {
}
}

required init?(coder: NSCoder, favoritesViewModel: FavoritesListInteracting, bookmarksDatabase: CoreDataDatabase, historyCoordinator: HistoryCoordinating) {
required init?(coder: NSCoder, favoritesViewModel: FavoritesListInteracting, bookmarksDatabase: CoreDataDatabase, historyCoordinator: HistoryCoordinating, bookmarksStringSearch: BookmarksStringSearch) {
self.favoritesModel = favoritesViewModel
self.bookmarksDatabase = bookmarksDatabase
self.historyCoordinator = historyCoordinator
self.bookmarksStringSearch = bookmarksStringSearch
super.init(coder: coder)
}

Expand Down Expand Up @@ -242,7 +244,9 @@ class SuggestionTrayViewController: UIViewController {
}

private func installAutocompleteSuggestions() {
let controller = AutocompleteViewController.loadFromStoryboard(bookmarksDatabase: bookmarksDatabase, historyCoordinator: historyCoordinator)
let controller = AutocompleteViewController.loadFromStoryboard(bookmarksDatabase: bookmarksDatabase,
bookmarksStringSearch: bookmarksStringSearch,
historyCoordinator: historyCoordinator)
install(controller: controller)
controller.delegate = autocompleteDelegate
controller.presentationDelegate = self
Expand Down

0 comments on commit 7903952

Please sign in to comment.