From 58d3ece14244ceae66eb0e707e89364e4a8419d4 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 09:42:22 +0100 Subject: [PATCH 01/10] inject app settings and make test set up async --- DuckDuckGo/AutoClear.swift | 7 ++++--- DuckDuckGoTests/AutoClearTests.swift | 17 +++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/DuckDuckGo/AutoClear.swift b/DuckDuckGo/AutoClear.swift index eca57dce95..134b23d683 100644 --- a/DuckDuckGo/AutoClear.swift +++ b/DuckDuckGo/AutoClear.swift @@ -33,14 +33,15 @@ class AutoClear { private let worker: AutoClearWorker private var timestamp: TimeInterval? - private lazy var appSettings = AppDependencyProvider.shared.appSettings - + private let appSettings: AppSettings + var isClearingEnabled: Bool { return AutoClearSettingsModel(settings: appSettings) != nil } - init(worker: AutoClearWorker) { + init(worker: AutoClearWorker, appSettings: AppSettings = AppDependencyProvider.shared.appSettings) { self.worker = worker + self.appSettings = appSettings } @MainActor diff --git a/DuckDuckGoTests/AutoClearTests.swift b/DuckDuckGoTests/AutoClearTests.swift index 2edb1e52ee..45e66212ba 100644 --- a/DuckDuckGoTests/AutoClearTests.swift +++ b/DuckDuckGoTests/AutoClearTests.swift @@ -49,19 +49,20 @@ class AutoClearTests: XCTestCase { private var worker: MockWorker! private var logic: AutoClear! + private var appSettings: AppSettingsMock! + + override func setUp() async throws { + try await super.setUp() - override func setUp() { - super.setUp() - worker = MockWorker() - logic = AutoClear(worker: worker) + appSettings = AppSettingsMock() + logic = AutoClear(worker: worker, appSettings: appSettings) } // Note: applicationDidLaunch based clearing has moved to "configureTabManager" function of // MainViewController to ensure that tabs are removed before the data is cleared. func testWhenTimingIsSetToTerminationThenOnlyRestartClearsData() async { - let appSettings = AppUserDefaults() appSettings.autoClearAction = .clearData appSettings.autoClearTiming = .termination @@ -70,10 +71,14 @@ class AutoClearTests: XCTestCase { XCTAssertEqual(worker.clearNavigationStackInvocationCount, 0) XCTAssertEqual(worker.forgetDataInvocationCount, 0) + + await logic.applicationWillMoveToForeground() + + XCTAssertEqual(worker.clearNavigationStackInvocationCount, 0) + XCTAssertEqual(worker.forgetDataInvocationCount, 0) } func testWhenDesiredTimingIsSetThenDataIsClearedOnceTimeHasElapsed() async { - let appSettings = AppUserDefaults() appSettings.autoClearAction = .clearData let cases: [AutoClearSettingsModel.Timing: TimeInterval] = [.delay5min: 5 * 60, From 826d234baddb6c97d1c55bc3988b1e4f3a2e5fb2 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 10:11:04 +0100 Subject: [PATCH 02/10] ensure clear data runs on main actor as required by webkit --- DuckDuckGo/MainViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 92c0277371..147d357f82 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -2320,6 +2320,7 @@ extension MainViewController: AutoClearWorker { refreshUIAfterClear() } + @MainActor func forgetData() async { guard !clearInProgress else { assertionFailure("Shouldn't get called multiple times") From aa47182f654473ccd3237f10e8258eb0fa5ff057 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 10:49:21 +0100 Subject: [PATCH 03/10] inject tabs model --- DuckDuckGo/AppDelegate.swift | 26 ++++++++++- DuckDuckGo/MainViewController.swift | 69 +++++++++++------------------ DuckDuckGo/TabManager.swift | 4 +- 3 files changed, 51 insertions(+), 48 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 42def30fc4..70054f97c8 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -267,6 +267,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } let historyManager = makeHistoryManager() + let tabsModel = prepareTabsModel() #if APP_TRACKING_PROTECTION let main = MainViewController(bookmarksDatabase: bookmarksDatabase, @@ -275,14 +276,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate { historyManager: historyManager, syncService: syncService, syncDataProviders: syncDataProviders, - appSettings: AppDependencyProvider.shared.appSettings) + appSettings: AppDependencyProvider.shared.appSettings, + tabsModel: tabsModel) #else let main = MainViewController(bookmarksDatabase: bookmarksDatabase, bookmarksDatabaseCleaner: syncDataProviders.bookmarksAdapter.databaseCleaner, historyManager: historyManager, syncService: syncService, syncDataProviders: syncDataProviders, - appSettings: AppDependencyProvider.shared.appSettings) + appSettings: AppDependencyProvider.shared.appSettings, + tabsModel: tabsModel) #endif main.loadViewIfNeeded() @@ -345,6 +348,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } + private func prepareTabsModel(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, + isDesktop: Bool = UIDevice.current.userInterfaceIdiom == .pad) -> TabsModel { + let isPadDevice = UIDevice.current.userInterfaceIdiom == .pad + let tabsModel: TabsModel + if let settings = AutoClearSettingsModel(settings: appSettings) { + tabsModel = TabsModel(desktop: isPadDevice) + tabsModel.save() + } else { + if let storedModel = TabsModel.get() { + // Save new model in case of migration + storedModel.save() + tabsModel = storedModel + } else { + tabsModel = TabsModel(desktop: isPadDevice) + } + } + return tabsModel + } + private func makeHistoryManager() -> HistoryManager { let historyManager = HistoryManager(privacyConfigManager: ContentBlocking.shared.privacyConfigurationManager, variantManager: DefaultVariantManager(), diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 147d357f82..4f25492fe5 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -84,7 +84,7 @@ class MainViewController: UIViewController { var tabsBarController: TabsBarViewController? var suggestionTrayController: SuggestionTrayViewController? - var tabManager: TabManager! + let tabManager: TabManager let previewsSource = TabPreviewsSource() let appSettings: AppSettings private var launchTabObserver: LaunchTabNotification.Observer? @@ -138,7 +138,7 @@ class MainViewController: UIViewController { lazy var tabSwitcherTransition = TabSwitcherTransitionDelegate() var currentTab: TabViewController? { - return tabManager?.current(createIfNeeded: false) + return tabManager.current(createIfNeeded: false) } var searchBarRect: CGRect { @@ -173,7 +173,8 @@ class MainViewController: UIViewController { historyManager: HistoryManager, syncService: DDGSyncing, syncDataProviders: SyncDataProviders, - appSettings: AppSettings = AppUserDefaults() + appSettings: AppSettings = AppUserDefaults(), + tabsModel: TabsModel ) { self.appTrackingProtectionDatabase = appTrackingProtectionDatabase self.bookmarksDatabase = bookmarksDatabase @@ -184,9 +185,16 @@ class MainViewController: UIViewController { self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase, favoritesDisplayMode: appSettings.favoritesDisplayMode) self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) self.appSettings = appSettings - + + self.tabManager = TabManager(model: tabsModel, + previewsSource: previewsSource, + bookmarksDatabase: bookmarksDatabase, + historyManager: historyManager, + syncService: syncService) + super.init(nibName: nil, bundle: nil) + tabManager.delegate = self bindFavoritesDisplayMode() bindSyncService() } @@ -197,7 +205,8 @@ class MainViewController: UIViewController { historyManager: HistoryManager, syncService: DDGSyncing, syncDataProviders: SyncDataProviders, - appSettings: AppSettings + appSettings: AppSettings, + tabsModel: TabsModel ) { self.bookmarksDatabase = bookmarksDatabase self.bookmarksDatabaseCleaner = bookmarksDatabaseCleaner @@ -207,9 +216,18 @@ class MainViewController: UIViewController { self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase, favoritesDisplayMode: appSettings.favoritesDisplayMode) self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) self.appSettings = appSettings - + + self.tabManager = TabManager(model: tabsModel, + previewsSource: previewsSource, + bookmarksDatabase: bookmarksDatabase, + historyManager: historyManager, + syncService: syncService) + + super.init(nibName: nil, bundle: nil) + self.tabManager.delegate = self + bindSyncService() } #endif @@ -263,7 +281,6 @@ class MainViewController: UIViewController { initTabButton() initMenuButton() initBookmarksButton() - configureTabManager() loadInitialView() previewsSource.prepare() addLaunchTabNotificationObserver() @@ -692,40 +709,6 @@ class MainViewController: UIViewController { dismissOmniBar() } - private func configureTabManager() { - - let isPadDevice = UIDevice.current.userInterfaceIdiom == .pad - - let tabsModel: TabsModel - if let settings = AutoClearSettingsModel(settings: appSettings) { - // This needs to be refactored so that tabs model is injected and cleared before view did load, - // but for now, ensure this happens in the right order by clearing data here too, if needed. - tabsModel = TabsModel(desktop: isPadDevice) - tabsModel.save() - previewsSource.removeAllPreviews() - - if settings.action.contains(.clearData) { - Task { @MainActor in - await forgetData() - } - } - } else { - if let storedModel = TabsModel.get() { - // Save new model in case of migration - storedModel.save() - tabsModel = storedModel - } else { - tabsModel = TabsModel(desktop: isPadDevice) - } - } - tabManager = TabManager(model: tabsModel, - previewsSource: previewsSource, - bookmarksDatabase: bookmarksDatabase, - historyManager: historyManager, - syncService: syncService, - delegate: self) - } - private func addLaunchTabNotificationObserver() { launchTabObserver = LaunchTabNotification.addObserver(handler: { [weak self] urlString in guard let self = self else { return } @@ -1999,7 +1982,7 @@ extension MainViewController: TabDelegate { if currentTab == tab { refreshControls() } - tabManager?.save() + tabManager.save() tabsBarController?.refresh(tabsModel: tabManager.model) // note: model in swipeTabsCoordinator doesn't need to be updated here // https://app.asana.com/0/414235014887631/1206847376910045/f @@ -2246,7 +2229,7 @@ extension MainViewController: TabSwitcherButtonDelegate { } func showTabSwitcher() { - guard let currentTab = currentTab ?? tabManager?.current(createIfNeeded: true) else { + guard let currentTab = currentTab ?? tabManager.current(createIfNeeded: true) else { fatalError("Unable to get current tab") } diff --git a/DuckDuckGo/TabManager.swift b/DuckDuckGo/TabManager.swift index a6bf166cf2..33e446b5cf 100644 --- a/DuckDuckGo/TabManager.swift +++ b/DuckDuckGo/TabManager.swift @@ -45,14 +45,12 @@ class TabManager { previewsSource: TabPreviewsSource, bookmarksDatabase: CoreDataDatabase, historyManager: HistoryManager, - syncService: DDGSyncing, - delegate: TabDelegate) { + syncService: DDGSyncing) { self.model = model self.previewsSource = previewsSource self.bookmarksDatabase = bookmarksDatabase self.historyManager = historyManager self.syncService = syncService - self.delegate = delegate let index = model.currentIndex let tab = model.tabs[index] if tab.link != nil { From 835d9b7a4f1d03392223bcad07f90abd5a05be6f Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 14:24:30 +0100 Subject: [PATCH 04/10] handle auto clear on load url --- DuckDuckGo/AppDelegate.swift | 2 +- DuckDuckGo/AutoClear.swift | 2 +- DuckDuckGo/MainViewController.swift | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 70054f97c8..af928fb050 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -753,7 +753,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } Task { @MainActor in - await autoClear?.applicationWillMoveToForeground() + // Autoclear should have happened by now showKeyboardIfSettingOn = false if !handleAppDeepLink(app, mainViewController, url) { diff --git a/DuckDuckGo/AutoClear.swift b/DuckDuckGo/AutoClear.swift index 134b23d683..bda1c46614 100644 --- a/DuckDuckGo/AutoClear.swift +++ b/DuckDuckGo/AutoClear.swift @@ -87,8 +87,8 @@ class AutoClear { let timestamp = timestamp, shouldClearData(elapsedTime: Date().timeIntervalSince1970 - timestamp) else { return } + self.timestamp = nil worker.clearNavigationStack() await clearData() - self.timestamp = nil } } diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 4f25492fe5..db0a80f093 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -89,6 +89,8 @@ class MainViewController: UIViewController { let appSettings: AppSettings private var launchTabObserver: LaunchTabNotification.Observer? + var doRefreshAfterClear = true + #if APP_TRACKING_PROTECTION private let appTrackingProtectionDatabase: CoreDataDatabase #endif @@ -866,6 +868,7 @@ class MainViewController: UIViewController { selectTab(existing) return } else if reuseExisting, let existing = tabManager.firstHomeTab() { + doRefreshAfterClear = false tabManager.selectTab(existing) loadUrl(url, fromExternalLink: fromExternalLink) } else { @@ -2291,6 +2294,10 @@ extension MainViewController: AutoClearWorker { } func refreshUIAfterClear() { + guard doRefreshAfterClear else { + doRefreshAfterClear = true + return + } showBars() attachHomeScreen() tabsBarController?.refresh(tabsModel: tabManager.model) From 8c9c6d646624e4a8137759dc9cc904c53a4c47e6 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 15:41:41 +0100 Subject: [PATCH 05/10] assign tab delegate when attaching --- DuckDuckGo/MainViewController.swift | 3 +-- DuckDuckGo/TabManager.swift | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index db0a80f093..14e97f3c0c 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -196,7 +196,6 @@ class MainViewController: UIViewController { super.init(nibName: nil, bundle: nil) - tabManager.delegate = self bindFavoritesDisplayMode() bindSyncService() } @@ -228,7 +227,6 @@ class MainViewController: UIViewController { super.init(nibName: nil, bundle: nil) - self.tabManager.delegate = self bindSyncService() } @@ -984,6 +982,7 @@ class MainViewController: UIViewController { currentTab?.progressWorker.progressBar = nil currentTab?.chromeDelegate = nil + tab.delegate = self addToContentContainer(controller: tab) viewCoordinator.logoContainer.isHidden = true diff --git a/DuckDuckGo/TabManager.swift b/DuckDuckGo/TabManager.swift index 33e446b5cf..7ea01cf1cb 100644 --- a/DuckDuckGo/TabManager.swift +++ b/DuckDuckGo/TabManager.swift @@ -35,7 +35,6 @@ class TabManager { private let historyManager: HistoryManager private let syncService: DDGSyncing private var previewsSource: TabPreviewsSource - weak var delegate: TabDelegate? @UserDefaultsWrapper(key: .faviconTabsCacheNeedsCleanup, defaultValue: true) var tabsCacheNeedsCleanup: Bool @@ -78,7 +77,6 @@ class TabManager { controller.attachWebView(configuration: configuration, andLoadRequest: url == nil ? nil : URLRequest.userInitiated(url!), consumeCookies: !model.hasActiveTabs) - controller.delegate = delegate controller.loadViewIfNeeded() return controller } @@ -150,7 +148,6 @@ class TabManager { andLoadRequest: request, consumeCookies: !model.hasActiveTabs, loadingInitiatedByParentTab: true) - controller.delegate = delegate controller.loadViewIfNeeded() controller.applyInheritedAttribution(inheritedAttribution) tabControllerCache.append(controller) From d578211a21be7548d3cee06d6f0fc390fe08ba7c Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Wed, 3 Apr 2024 16:41:10 +0100 Subject: [PATCH 06/10] swiftlint --- DuckDuckGo/AppDelegate.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index af928fb050..4d9a8f863d 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -349,10 +349,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } private func prepareTabsModel(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, - isDesktop: Bool = UIDevice.current.userInterfaceIdiom == .pad) -> TabsModel { + isDesktop: Bool = UIDevice.current.userInterfaceIdiom == .pad) -> TabsModel { let isPadDevice = UIDevice.current.userInterfaceIdiom == .pad let tabsModel: TabsModel - if let settings = AutoClearSettingsModel(settings: appSettings) { + if AutoClearSettingsModel(settings: appSettings) != nil { tabsModel = TabsModel(desktop: isPadDevice) tabsModel.save() } else { From f5b7475e8d3ecb944e847e907780cb44af479415 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 4 Apr 2024 11:21:08 +0100 Subject: [PATCH 07/10] inject previews source and remove them when tabs are removed --- DuckDuckGo/AppDelegate.swift | 9 +++++++-- DuckDuckGo/MainViewController.swift | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 4d9a8f863d..fb1e82dd9b 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -266,8 +266,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { }) } + let previewsSource = TabPreviewsSource() let historyManager = makeHistoryManager() - let tabsModel = prepareTabsModel() + let tabsModel = prepareTabsModel(previewsSource: previewsSource) #if APP_TRACKING_PROTECTION let main = MainViewController(bookmarksDatabase: bookmarksDatabase, @@ -277,6 +278,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { syncService: syncService, syncDataProviders: syncDataProviders, appSettings: AppDependencyProvider.shared.appSettings, + previewsSource: previewsSource, tabsModel: tabsModel) #else let main = MainViewController(bookmarksDatabase: bookmarksDatabase, @@ -285,6 +287,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { syncService: syncService, syncDataProviders: syncDataProviders, appSettings: AppDependencyProvider.shared.appSettings, + previewsSource: previewsSource, tabsModel: tabsModel) #endif @@ -348,13 +351,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } - private func prepareTabsModel(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, + private func prepareTabsModel(previewsSource: TabPreviewsSource = TabPreviewsSource(), + appSettings: AppSettings = AppDependencyProvider.shared.appSettings, isDesktop: Bool = UIDevice.current.userInterfaceIdiom == .pad) -> TabsModel { let isPadDevice = UIDevice.current.userInterfaceIdiom == .pad let tabsModel: TabsModel if AutoClearSettingsModel(settings: appSettings) != nil { tabsModel = TabsModel(desktop: isPadDevice) tabsModel.save() + previewsSource.removeAllPreviews() } else { if let storedModel = TabsModel.get() { // Save new model in case of migration diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 14e97f3c0c..4991254263 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -85,7 +85,7 @@ class MainViewController: UIViewController { var suggestionTrayController: SuggestionTrayViewController? let tabManager: TabManager - let previewsSource = TabPreviewsSource() + let previewsSource: TabPreviewsSource let appSettings: AppSettings private var launchTabObserver: LaunchTabNotification.Observer? @@ -176,6 +176,7 @@ class MainViewController: UIViewController { syncService: DDGSyncing, syncDataProviders: SyncDataProviders, appSettings: AppSettings = AppUserDefaults(), + previewsSource: TabPreviewsSource, tabsModel: TabsModel ) { self.appTrackingProtectionDatabase = appTrackingProtectionDatabase @@ -187,6 +188,7 @@ class MainViewController: UIViewController { self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase, favoritesDisplayMode: appSettings.favoritesDisplayMode) self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) self.appSettings = appSettings + self.previewsSource = previewsSource self.tabManager = TabManager(model: tabsModel, previewsSource: previewsSource, @@ -207,6 +209,7 @@ class MainViewController: UIViewController { syncService: DDGSyncing, syncDataProviders: SyncDataProviders, appSettings: AppSettings, + previewsSource: TabPreviewsSource, tabsModel: TabsModel ) { self.bookmarksDatabase = bookmarksDatabase @@ -217,6 +220,7 @@ class MainViewController: UIViewController { self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase, favoritesDisplayMode: appSettings.favoritesDisplayMode) self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) self.appSettings = appSettings + self.previewsSource = previewsSource self.tabManager = TabManager(model: tabsModel, previewsSource: previewsSource, From db3f6bb5c87c73b94ba7bdc512453a0a20d23f45 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 4 Apr 2024 12:18:02 +0100 Subject: [PATCH 08/10] assign delegate when controller is created --- DuckDuckGo/TabManager.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DuckDuckGo/TabManager.swift b/DuckDuckGo/TabManager.swift index 7ea01cf1cb..63cc6b3f48 100644 --- a/DuckDuckGo/TabManager.swift +++ b/DuckDuckGo/TabManager.swift @@ -36,6 +36,8 @@ class TabManager { private let syncService: DDGSyncing private var previewsSource: TabPreviewsSource + weak var delegate: TabDelegate? + @UserDefaultsWrapper(key: .faviconTabsCacheNeedsCleanup, defaultValue: true) var tabsCacheNeedsCleanup: Bool @@ -77,6 +79,7 @@ class TabManager { controller.attachWebView(configuration: configuration, andLoadRequest: url == nil ? nil : URLRequest.userInitiated(url!), consumeCookies: !model.hasActiveTabs) + controller.delegate = delegate controller.loadViewIfNeeded() return controller } @@ -148,6 +151,7 @@ class TabManager { andLoadRequest: request, consumeCookies: !model.hasActiveTabs, loadingInitiatedByParentTab: true) + controller.delegate = delegate controller.loadViewIfNeeded() controller.applyInheritedAttribution(inheritedAttribution) tabControllerCache.append(controller) From 7bd1908572fe1b4197b4d3341d4e458e87ddee95 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 4 Apr 2024 12:45:49 +0100 Subject: [PATCH 09/10] assign delegate when tab is created --- DuckDuckGo/MainViewController.swift | 6 +++--- DuckDuckGo/TabViewController.swift | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 4991254263..efe8920d17 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -197,7 +197,8 @@ class MainViewController: UIViewController { syncService: syncService) super.init(nibName: nil, bundle: nil) - + + tabManager.delegate = self bindFavoritesDisplayMode() bindSyncService() } @@ -231,7 +232,7 @@ class MainViewController: UIViewController { super.init(nibName: nil, bundle: nil) - + tabManager.delegate = self bindSyncService() } #endif @@ -986,7 +987,6 @@ class MainViewController: UIViewController { currentTab?.progressWorker.progressBar = nil currentTab?.chromeDelegate = nil - tab.delegate = self addToContentContainer(controller: tab) viewCoordinator.logoContainer.isHidden = true diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index 5429c52948..9ed0683ee3 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -74,7 +74,11 @@ class TabViewController: UIViewController { } } - weak var delegate: TabDelegate? + weak var delegate: TabDelegate? { + didSet { + assert(delegate != nil) + } + } weak var chromeDelegate: BrowserChromeDelegate? var findInPage: FindInPage? { From ef538d9f7f489390dec10d642a2a00bf52ce4b63 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 4 Apr 2024 17:37:56 +0100 Subject: [PATCH 10/10] remove errant assert --- DuckDuckGo/TabViewController.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index 9ed0683ee3..5429c52948 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -74,11 +74,7 @@ class TabViewController: UIViewController { } } - weak var delegate: TabDelegate? { - didSet { - assert(delegate != nil) - } - } + weak var delegate: TabDelegate? weak var chromeDelegate: BrowserChromeDelegate? var findInPage: FindInPage? {