From 79039520a6c7f21aa6bf862218c21a620e561d90 Mon Sep 17 00:00:00 2001 From: bwaresiak Date: Wed, 15 May 2024 18:40:56 +0200 Subject: [PATCH] Remove run loop use, use DI for Bookmarks Caching Search (#2854) Task/Issue URL: https://app.asana.com/0/856498667320406/1207322485119026/f Tech Design URL: CC: Description: Remove run loop usage from bookmarks caching search. --- Core/BookmarksCachingSearch.swift | 14 ++------------ DuckDuckGo/AutocompleteViewController.swift | 8 ++++---- DuckDuckGo/MainViewController.swift | 3 ++- DuckDuckGo/SuggestionTrayViewController.swift | 8 ++++++-- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Core/BookmarksCachingSearch.swift b/Core/BookmarksCachingSearch.swift index a874a05284..089eec2699 100644 --- a/Core/BookmarksCachingSearch.swift +++ b/Core/BookmarksCachingSearch.swift @@ -171,7 +171,7 @@ public class BookmarksCachingSearch: BookmarksStringSearch { self?.refreshCache() } - loadCache() + refreshCache() } public var hasData: Bool { @@ -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() diff --git a/DuckDuckGo/AutocompleteViewController.swift b/DuckDuckGo/AutocompleteViewController.swift index a96525830a..b6dc5abe8e 100644 --- a/DuckDuckGo/AutocompleteViewController.swift +++ b/DuckDuckGo/AutocompleteViewController.swift @@ -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 ? @@ -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 { @@ -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 @@ -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) } } diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 7634f219fc..bb5a3ea560 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -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 diff --git a/DuckDuckGo/SuggestionTrayViewController.swift b/DuckDuckGo/SuggestionTrayViewController.swift index fb47070327..c10bc32fe1 100644 --- a/DuckDuckGo/SuggestionTrayViewController.swift +++ b/DuckDuckGo/SuggestionTrayViewController.swift @@ -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 @@ -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) } @@ -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