From 19441b3168e826f92b7cde3bed02497d2bbe07f8 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 11 Apr 2024 11:36:47 +0100 Subject: [PATCH] revert to previous logic for existing users --- Core/DefaultVariantManager.swift | 4 +-- DuckDuckGo/AutocompleteViewController.swift | 38 ++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Core/DefaultVariantManager.swift b/Core/DefaultVariantManager.swift index 98cbf59382..1b20f2829a 100644 --- a/Core/DefaultVariantManager.swift +++ b/Core/DefaultVariantManager.swift @@ -26,8 +26,8 @@ extension FeatureName { // Define your feature e.g.: // public static let experimentalFeature = FeatureName(rawValue: "experimentalFeature") - // Experiment coming soon public static let history = FeatureName(rawValue: "history") + public static let newSuggestionLogic = FeatureName(rawValue: "newSuggestionLogic") } public struct VariantIOS: Variant { @@ -63,7 +63,7 @@ public struct VariantIOS: Variant { VariantIOS(name: "sd", weight: doNotAllocate, isIncluded: When.always, features: []), VariantIOS(name: "se", weight: doNotAllocate, isIncluded: When.always, features: []), - VariantIOS(name: "mc", weight: 1, isIncluded: When.inEnglish, features: []), + VariantIOS(name: "mc", weight: 1, isIncluded: When.inEnglish, features: [.newSuggestionLogic]), VariantIOS(name: "md", weight: 1, isIncluded: When.inEnglish, features: [.history]), returningUser diff --git a/DuckDuckGo/AutocompleteViewController.swift b/DuckDuckGo/AutocompleteViewController.swift index a2fc59be5f..a94516a273 100644 --- a/DuckDuckGo/AutocompleteViewController.swift +++ b/DuckDuckGo/AutocompleteViewController.swift @@ -27,6 +27,7 @@ import CoreData import Persistence import History import Combine +import BrowserServicesKit class AutocompleteViewController: UIViewController { @@ -54,10 +55,16 @@ class AutocompleteViewController: UIViewController { private var historyCoordinator: HistoryCoordinating! private var bookmarksDatabase: CoreDataDatabase! private var appSettings: AppSettings! + private var variantManager: VariantManager! + private lazy var cachedBookmarks: CachedBookmarks = { CachedBookmarks(bookmarksDatabase) }() + private lazy var cachedBookmarksSearch: BookmarksStringSearch = { + BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) + }() + var backgroundColor: UIColor { appSettings.currentAddressBarPosition.isBottom ? UIColor(designSystemColor: .background) : @@ -82,7 +89,8 @@ class AutocompleteViewController: UIViewController { static func loadFromStoryboard(bookmarksDatabase: CoreDataDatabase, historyCoordinator: HistoryCoordinating, - appSettings: AppSettings = AppDependencyProvider.shared.appSettings) -> AutocompleteViewController { + appSettings: AppSettings = AppDependencyProvider.shared.appSettings, + variantManager: VariantManager = DefaultVariantManager()) -> AutocompleteViewController { let storyboard = UIStoryboard(name: "Autocomplete", bundle: nil) guard let controller = storyboard.instantiateInitialViewController() as? AutocompleteViewController else { fatalError("Failed to instatiate correct Autocomplete view controller") @@ -90,6 +98,7 @@ class AutocompleteViewController: UIViewController { controller.bookmarksDatabase = bookmarksDatabase controller.historyCoordinator = historyCoordinator controller.appSettings = appSettings + controller.variantManager = variantManager return controller } @@ -178,6 +187,16 @@ class AutocompleteViewController: UIViewController { selectedItem = -1 tableView.reloadData() + let bookmarks: [Suggestion] + + if variantManager.inSuggestionExperiment { + bookmarks = [] // We'll supply bookmarks elsewhere + } else { + bookmarks = cachedBookmarksSearch.search(query: query).prefix(2).map { + .bookmark(title: $0.title, url: $0.url, isFavorite: $0.isFavorite, allowedInTopHits: true) + } + } + loader = SuggestionLoader(dataSource: self, urlFactory: { phrase in guard let url = URL(trimmedAddressBarString: phrase), let scheme = url.scheme, @@ -195,7 +214,10 @@ class AutocompleteViewController: UIViewController { self?.pendingRequest = false } guard error == nil else { return } - self?.updateSuggestions(result?.all ?? []) + + let remoteResults = result?.all ?? [] + + self?.updateSuggestions(bookmarks + remoteResults) } } @@ -333,11 +355,11 @@ extension AutocompleteViewController { extension AutocompleteViewController: SuggestionLoadingDataSource { func history(for suggestionLoading: Suggestions.SuggestionLoading) -> [HistorySuggestion] { - return historyCoordinator.history ?? [] + return variantManager.inSuggestionExperiment ? (historyCoordinator.history ?? []) : [] } func bookmarks(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.Bookmark] { - return cachedBookmarks.all + return variantManager.inSuggestionExperiment ? cachedBookmarks.all : [] } func suggestionLoading(_ suggestionLoading: Suggestions.SuggestionLoading, suggestionDataFromUrl url: URL, withParameters parameters: [String: String], completion: @escaping (Data?, Error?) -> Void) { @@ -363,3 +385,11 @@ extension HistoryEntry: HistorySuggestion { } } + +extension VariantManager { + + var inSuggestionExperiment: Bool { + isSupported(feature: .newSuggestionLogic) || isSupported(feature: .history) + } + +}