From 8dc9c3f455aa51e17a1a9ee2bbac6d5d11f0620a Mon Sep 17 00:00:00 2001 From: Alexey Martemyanov Date: Mon, 16 Dec 2024 20:22:21 +0600 Subject: [PATCH] separate tab search for fire windows --- .../Model/HomePageAddressBarModel.swift | 2 +- .../MainWindow/MainViewController.swift | 1 - .../View/AddressBarViewController.swift | 8 +++--- .../View/NavigationBarViewController.swift | 12 ++++----- .../Model/SuggestionContainer.swift | 27 +++++++++++-------- .../View/WindowControllersManager.swift | 12 ++++++++- .../Tab/SearchNonexistentDomainTests.swift | 5 +++- .../Model/SuggestionContainerTests.swift | 9 ++++--- .../SuggestionContainerViewModelTests.swift | 7 ++--- 9 files changed, 51 insertions(+), 32 deletions(-) diff --git a/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift b/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift index 9d5f2ed60d..763a33d514 100644 --- a/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift +++ b/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift @@ -146,7 +146,7 @@ extension HomePage.Models { return AddressBarViewController( coder: coder, tabCollectionViewModel: tabCollectionViewModel, - isBurner: tabCollectionViewModel.isBurner, + burnerMode: tabCollectionViewModel.burnerMode, popovers: nil, isSearchBox: true ) diff --git a/DuckDuckGo/MainWindow/MainViewController.swift b/DuckDuckGo/MainWindow/MainViewController.swift index d0674088c2..c58cc019d9 100644 --- a/DuckDuckGo/MainWindow/MainViewController.swift +++ b/DuckDuckGo/MainWindow/MainViewController.swift @@ -119,7 +119,6 @@ final class MainViewController: NSViewController { }() navigationBarViewController = NavigationBarViewController.create(tabCollectionViewModel: tabCollectionViewModel, - isBurner: isBurner, networkProtectionPopoverManager: networkProtectionPopoverManager, networkProtectionStatusReporter: networkProtectionStatusReporter, autofillPopoverPresenter: autofillPopoverPresenter, diff --git a/DuckDuckGo/NavigationBar/View/AddressBarViewController.swift b/DuckDuckGo/NavigationBar/View/AddressBarViewController.swift index 2d98d5f052..8b7db93af4 100644 --- a/DuckDuckGo/NavigationBar/View/AddressBarViewController.swift +++ b/DuckDuckGo/NavigationBar/View/AddressBarViewController.swift @@ -88,7 +88,7 @@ final class AddressBarViewController: NSViewController, ObservableObject { init?(coder: NSCoder, tabCollectionViewModel: TabCollectionViewModel, - isBurner: Bool, + burnerMode: BurnerMode, popovers: NavigationBarPopovers?, isSearchBox: Bool = false, onboardingPixelReporter: OnboardingAddressBarReporting = OnboardingPixelReporter()) { @@ -96,9 +96,9 @@ final class AddressBarViewController: NSViewController, ObservableObject { self.popovers = popovers self.suggestionContainerViewModel = SuggestionContainerViewModel( isHomePage: tabViewModel?.tab.content == .newtab, - isBurner: isBurner, - suggestionContainer: SuggestionContainer()) - self.isBurner = isBurner + isBurner: burnerMode.isBurner, + suggestionContainer: SuggestionContainer(burnerMode: burnerMode)) + self.isBurner = burnerMode.isBurner self.onboardingPixelReporter = onboardingPixelReporter self.isSearchBox = isSearchBox diff --git a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift index ef178b6eed..c3c2e0605d 100644 --- a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift +++ b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift @@ -85,7 +85,7 @@ final class NavigationBarViewController: NSViewController { var addressBarViewController: AddressBarViewController? private var tabCollectionViewModel: TabCollectionViewModel - private let isBurner: Bool + private var burnerMode: BurnerMode { tabCollectionViewModel.burnerMode } // swiftlint:disable weak_delegate private let goBackButtonMenuDelegate: NavigationButtonMenuDelegate @@ -124,7 +124,6 @@ final class NavigationBarViewController: NSViewController { private let networkProtectionFeatureActivation: NetworkProtectionFeatureActivation static func create(tabCollectionViewModel: TabCollectionViewModel, - isBurner: Bool, networkProtectionFeatureActivation: NetworkProtectionFeatureActivation = NetworkProtectionKeychainTokenStore(), downloadListCoordinator: DownloadListCoordinator = .shared, dragDropManager: BookmarkDragDropManager = .shared, @@ -134,17 +133,16 @@ final class NavigationBarViewController: NSViewController { aiChatMenuConfig: AIChatMenuVisibilityConfigurable, brokenSitePromptLimiter: BrokenSitePromptLimiter) -> NavigationBarViewController { NSStoryboard(name: "NavigationBar", bundle: nil).instantiateInitialController { coder in - self.init(coder: coder, tabCollectionViewModel: tabCollectionViewModel, isBurner: isBurner, networkProtectionFeatureActivation: networkProtectionFeatureActivation, downloadListCoordinator: downloadListCoordinator, dragDropManager: dragDropManager, networkProtectionPopoverManager: networkProtectionPopoverManager, networkProtectionStatusReporter: networkProtectionStatusReporter, autofillPopoverPresenter: autofillPopoverPresenter, aiChatMenuConfig: aiChatMenuConfig, brokenSitePromptLimiter: brokenSitePromptLimiter) + self.init(coder: coder, tabCollectionViewModel: tabCollectionViewModel, networkProtectionFeatureActivation: networkProtectionFeatureActivation, downloadListCoordinator: downloadListCoordinator, dragDropManager: dragDropManager, networkProtectionPopoverManager: networkProtectionPopoverManager, networkProtectionStatusReporter: networkProtectionStatusReporter, autofillPopoverPresenter: autofillPopoverPresenter, aiChatMenuConfig: aiChatMenuConfig, brokenSitePromptLimiter: brokenSitePromptLimiter) }! } - init?(coder: NSCoder, tabCollectionViewModel: TabCollectionViewModel, isBurner: Bool, networkProtectionFeatureActivation: NetworkProtectionFeatureActivation, downloadListCoordinator: DownloadListCoordinator, dragDropManager: BookmarkDragDropManager, networkProtectionPopoverManager: NetPPopoverManager, networkProtectionStatusReporter: NetworkProtectionStatusReporter, autofillPopoverPresenter: AutofillPopoverPresenter, + init?(coder: NSCoder, tabCollectionViewModel: TabCollectionViewModel, networkProtectionFeatureActivation: NetworkProtectionFeatureActivation, downloadListCoordinator: DownloadListCoordinator, dragDropManager: BookmarkDragDropManager, networkProtectionPopoverManager: NetPPopoverManager, networkProtectionStatusReporter: NetworkProtectionStatusReporter, autofillPopoverPresenter: AutofillPopoverPresenter, aiChatMenuConfig: AIChatMenuVisibilityConfigurable, brokenSitePromptLimiter: BrokenSitePromptLimiter) { - self.popovers = NavigationBarPopovers(networkProtectionPopoverManager: networkProtectionPopoverManager, autofillPopoverPresenter: autofillPopoverPresenter, isBurner: isBurner) + self.popovers = NavigationBarPopovers(networkProtectionPopoverManager: networkProtectionPopoverManager, autofillPopoverPresenter: autofillPopoverPresenter, isBurner: tabCollectionViewModel.isBurner) self.tabCollectionViewModel = tabCollectionViewModel self.networkProtectionButtonModel = NetworkProtectionNavBarButtonModel(popoverManager: networkProtectionPopoverManager, statusReporter: networkProtectionStatusReporter) - self.isBurner = isBurner self.networkProtectionFeatureActivation = networkProtectionFeatureActivation self.downloadListCoordinator = downloadListCoordinator self.dragDropManager = dragDropManager @@ -234,7 +232,7 @@ final class NavigationBarViewController: NSViewController { let onboardingPixelReporter = OnboardingPixelReporter() guard let addressBarViewController = AddressBarViewController(coder: coder, tabCollectionViewModel: tabCollectionViewModel, - isBurner: isBurner, + burnerMode: burnerMode, popovers: popovers, onboardingPixelReporter: onboardingPixelReporter) else { fatalError("NavigationBarViewController: Failed to init AddressBarViewController") diff --git a/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift b/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift index add2a4d266..99ccdfd576 100644 --- a/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift +++ b/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift @@ -52,20 +52,12 @@ final class SuggestionContainer { self.loading = suggestionLoading } - convenience init () { + convenience init (burnerMode: BurnerMode) { let urlFactory = { urlString in return URL.makeURL(fromSuggestionPhrase: urlString) } - let openTabsProvider: OpenTabsProvider = { @MainActor in - let selectedTab = WindowControllersManager.shared.selectedTab - return WindowControllersManager.shared.allTabViewModels.compactMap { model in - guard model.tab !== selectedTab, model.tab.content.isUrl else { return nil } - return model.tab.content.userEditableUrl.map { url in - OpenTab(title: model.title, url: url) - } - } - } - self.init(openTabsProvider: openTabsProvider, suggestionLoading: SuggestionLoader(urlFactory: urlFactory), + self.init(openTabsProvider: Self.defaultOpenTabsProvider(burnerMode: burnerMode), + suggestionLoading: SuggestionLoader(urlFactory: urlFactory), historyCoordinating: HistoryCoordinator.shared, bookmarkManager: LocalBookmarkManager.shared) } @@ -102,6 +94,19 @@ final class SuggestionContainer { latestQuery = nil } + private static func defaultOpenTabsProvider(burnerMode: BurnerMode) -> OpenTabsProvider { + { @MainActor in + let selectedTab = WindowControllersManager.shared.selectedTab + let openTabViewModels = WindowControllersManager.shared.allTabViewModels(for: burnerMode) + return openTabViewModels.compactMap { model in + guard model.tab !== selectedTab, model.tab.content.isUrl else { return nil } + return model.tab.content.userEditableUrl.map { url in + OpenTab(title: model.title, url: url) + } + } + } + } + } struct OpenTab: BrowserTab { diff --git a/DuckDuckGo/Windows/View/WindowControllersManager.swift b/DuckDuckGo/Windows/View/WindowControllersManager.swift index d971ab720f..b6eb66d113 100644 --- a/DuckDuckGo/Windows/View/WindowControllersManager.swift +++ b/DuckDuckGo/Windows/View/WindowControllersManager.swift @@ -356,10 +356,20 @@ extension WindowControllersManager { var allTabViewModels: [TabViewModel] { return allTabCollectionViewModels.flatMap { - Array($0.tabViewModels.values) + $0.tabViewModels.values } } + func allTabViewModels(for burnerMode: BurnerMode) -> [TabViewModel] { + allTabCollectionViewModels + .filter { tabCollectionViewModel in + tabCollectionViewModel.burnerMode == burnerMode + } + .flatMap { + $0.tabViewModels.values + } + } + func windowController(for tabCollectionViewModel: TabCollectionViewModel) -> MainWindowController? { return mainWindowControllers.first(where: { tabCollectionViewModel === $0.mainViewController.tabCollectionViewModel diff --git a/IntegrationTests/Tab/SearchNonexistentDomainTests.swift b/IntegrationTests/Tab/SearchNonexistentDomainTests.swift index 9daff96090..ace26d6ac5 100644 --- a/IntegrationTests/Tab/SearchNonexistentDomainTests.swift +++ b/IntegrationTests/Tab/SearchNonexistentDomainTests.swift @@ -238,7 +238,10 @@ final class SearchNonexistentDomainTests: XCTestCase { addressBar.stringValue = enteredString let suggestionLoadingMock = SuggestionLoadingMock() - let suggestionContainer = SuggestionContainer(suggestionLoading: suggestionLoadingMock, historyCoordinating: HistoryCoordinator.shared, bookmarkManager: LocalBookmarkManager.shared) + let suggestionContainer = SuggestionContainer(openTabsProvider: { [] }, + suggestionLoading: suggestionLoadingMock, + historyCoordinating: HistoryCoordinator.shared, + bookmarkManager: LocalBookmarkManager.shared) addressBar.suggestionContainerViewModel = SuggestionContainerViewModel(isHomePage: true, isBurner: false, suggestionContainer: suggestionContainer) suggestionContainer.getSuggestions(for: enteredString) diff --git a/UnitTests/Suggestions/Model/SuggestionContainerTests.swift b/UnitTests/Suggestions/Model/SuggestionContainerTests.swift index c2f35053c2..faa8e308ec 100644 --- a/UnitTests/Suggestions/Model/SuggestionContainerTests.swift +++ b/UnitTests/Suggestions/Model/SuggestionContainerTests.swift @@ -25,7 +25,8 @@ final class SuggestionContainerTests: XCTestCase { func testWhenGetSuggestionsIsCalled_ThenContainerAsksAndHoldsSuggestionsFromLoader() { let suggestionLoadingMock = SuggestionLoadingMock() let historyCoordinatingMock = HistoryCoordinatingMock() - let suggestionContainer = SuggestionContainer(suggestionLoading: suggestionLoadingMock, + let suggestionContainer = SuggestionContainer(openTabsProvider: { [] }, + suggestionLoading: suggestionLoadingMock, historyCoordinating: historyCoordinatingMock, bookmarkManager: LocalBookmarkManager.shared) @@ -50,7 +51,8 @@ final class SuggestionContainerTests: XCTestCase { func testWhenStopGettingSuggestionsIsCalled_ThenNoSuggestionsArePublished() { let suggestionLoadingMock = SuggestionLoadingMock() let historyCoordinatingMock = HistoryCoordinatingMock() - let suggestionContainer = SuggestionContainer(suggestionLoading: suggestionLoadingMock, + let suggestionContainer = SuggestionContainer(openTabsProvider: { [] }, + suggestionLoading: suggestionLoadingMock, historyCoordinating: historyCoordinatingMock, bookmarkManager: LocalBookmarkManager.shared) @@ -65,7 +67,8 @@ final class SuggestionContainerTests: XCTestCase { func testSuggestionLoadingCacheClearing() { let suggestionLoadingMock = SuggestionLoadingMock() let historyCoordinatingMock = HistoryCoordinatingMock() - let suggestionContainer = SuggestionContainer(suggestionLoading: suggestionLoadingMock, + let suggestionContainer = SuggestionContainer(openTabsProvider: { [] }, + suggestionLoading: suggestionLoadingMock, historyCoordinating: historyCoordinatingMock, bookmarkManager: LocalBookmarkManager.shared) diff --git a/UnitTests/Suggestions/ViewModel/SuggestionContainerViewModelTests.swift b/UnitTests/Suggestions/ViewModel/SuggestionContainerViewModelTests.swift index 50ac3dce1a..9bf1d116bd 100644 --- a/UnitTests/Suggestions/ViewModel/SuggestionContainerViewModelTests.swift +++ b/UnitTests/Suggestions/ViewModel/SuggestionContainerViewModelTests.swift @@ -34,7 +34,8 @@ final class SuggestionContainerViewModelTests: XCTestCase { SearchPreferences.shared.showAutocompleteSuggestions = true suggestionLoadingMock = SuggestionLoadingMock() historyCoordinatingMock = HistoryCoordinatingMock() - suggestionContainer = SuggestionContainer(suggestionLoading: suggestionLoadingMock, + suggestionContainer = SuggestionContainer(openTabsProvider: { [] }, + suggestionLoading: suggestionLoadingMock, historyCoordinating: historyCoordinatingMock, bookmarkManager: LocalBookmarkManager.shared) suggestionContainerViewModel = SuggestionContainerViewModel(suggestionContainer: suggestionContainer) @@ -59,7 +60,7 @@ final class SuggestionContainerViewModelTests: XCTestCase { // MARK: - Tests func testWhenSelectionIndexIsNilThenSelectedSuggestionViewModelIsNil() { - let suggestionContainer = SuggestionContainer() + let suggestionContainer = SuggestionContainer(burnerMode: .regular) let suggestionContainerViewModel = SuggestionContainerViewModel(suggestionContainer: suggestionContainer) XCTAssertNil(suggestionContainerViewModel.selectionIndex) @@ -87,7 +88,7 @@ final class SuggestionContainerViewModelTests: XCTestCase { } func testWhenSelectCalledWithIndexOutOfBoundsThenSelectedSuggestionViewModelIsNil() { - let suggestionContainer = SuggestionContainer() + let suggestionContainer = SuggestionContainer(burnerMode: .regular) let suggestionListViewModel = SuggestionContainerViewModel(suggestionContainer: suggestionContainer) suggestionListViewModel.select(at: 0)