From 0f73f8a1e055d8a80eb4fd73875b82330a075b76 Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Tue, 7 Nov 2023 19:38:22 -0800 Subject: [PATCH] Avoid AppTP DB initialization when disabled (#2090) Task/Issue URL: https://app.asana.com/0/1199333091098016/1205714053673490/f Tech Design URL: CC: Description: This PR disables AppTP database initialization when the feature is disabled. --- DuckDuckGo/AppDelegate.swift | 14 ++++++++++++ DuckDuckGo/HomeViewController.swift | 26 +++++++++++++++++----- DuckDuckGo/MainViewController.swift | 34 +++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index cc6917da35..d49f873e3d 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -62,7 +62,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { private lazy var privacyStore = PrivacyUserDefaults() private var bookmarksDatabase: CoreDataDatabase = BookmarksDatabase.make() + +#if APP_TRACKING_PROTECTION private var appTrackingProtectionDatabase: CoreDataDatabase = AppTrackingProtectionDatabase.make() +#endif + private var autoClear: AutoClear? private var showKeyboardIfSettingOn = true private var lastBackgroundDate: Date? @@ -183,6 +187,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { WidgetCenter.shared.reloadAllTimelines() } +#if APP_TRACKING_PROTECTION appTrackingProtectionDatabase.loadStore { context, error in guard context != nil else { if let error = error { @@ -199,6 +204,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } } +#endif Favicons.shared.migrateFavicons(to: Favicons.Constants.maxFaviconSize) { WidgetCenter.shared.reloadAllTimelines() @@ -234,12 +240,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate { syncService.initializeIfNeeded() self.syncService = syncService +#if APP_TRACKING_PROTECTION let main = MainViewController(bookmarksDatabase: bookmarksDatabase, bookmarksDatabaseCleaner: syncDataProviders.bookmarksAdapter.databaseCleaner, appTrackingProtectionDatabase: appTrackingProtectionDatabase, syncService: syncService, syncDataProviders: syncDataProviders, appSettings: AppDependencyProvider.shared.appSettings) +#else + let main = MainViewController(bookmarksDatabase: bookmarksDatabase, + bookmarksDatabaseCleaner: syncDataProviders.bookmarksAdapter.databaseCleaner, + syncService: syncService, + syncDataProviders: syncDataProviders, + appSettings: AppDependencyProvider.shared.appSettings) +#endif main.loadViewIfNeeded() window = UIWindow(frame: UIScreen.main.bounds) diff --git a/DuckDuckGo/HomeViewController.swift b/DuckDuckGo/HomeViewController.swift index f938dca3c1..4f97f8c46b 100644 --- a/DuckDuckGo/HomeViewController.swift +++ b/DuckDuckGo/HomeViewController.swift @@ -69,6 +69,7 @@ class HomeViewController: UIViewController { private let appTPHomeViewModel: AppTPHomeViewModel #endif +#if APP_TRACKING_PROTECTION static func loadFromStoryboard(model: Tab, favoritesViewModel: FavoritesListInteracting, appTPDatabase: CoreDataDatabase) -> HomeViewController { let storyboard = UIStoryboard(name: "Home", bundle: nil) let controller = storyboard.instantiateViewController(identifier: "HomeViewController", creator: { coder in @@ -76,18 +77,33 @@ class HomeViewController: UIViewController { }) return controller } - +#else + static func loadFromStoryboard(model: Tab, favoritesViewModel: FavoritesListInteracting) -> HomeViewController { + let storyboard = UIStoryboard(name: "Home", bundle: nil) + let controller = storyboard.instantiateViewController(identifier: "HomeViewController", creator: { coder in + HomeViewController(coder: coder, tabModel: model, favoritesViewModel: favoritesViewModel) + }) + return controller + } +#endif + +#if APP_TRACKING_PROTECTION required init?(coder: NSCoder, tabModel: Tab, favoritesViewModel: FavoritesListInteracting, appTPDatabase: CoreDataDatabase) { self.tabModel = tabModel self.favoritesViewModel = favoritesViewModel - -#if APP_TRACKING_PROTECTION self.appTPHomeViewModel = AppTPHomeViewModel(appTrackingProtectionDatabase: appTPDatabase) -#endif super.init(coder: coder) } - +#else + required init?(coder: NSCoder, tabModel: Tab, favoritesViewModel: FavoritesListInteracting) { + self.tabModel = tabModel + self.favoritesViewModel = favoritesViewModel + + super.init(coder: coder) + } +#endif + required init?(coder: NSCoder) { fatalError("Not implemented") } diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 6dd1d6bb44..df77c8acc5 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -78,8 +78,11 @@ class MainViewController: UIViewController { let previewsSource = TabPreviewsSource() let appSettings: AppSettings private var launchTabObserver: LaunchTabNotification.Observer? - + +#if APP_TRACKING_PROTECTION private let appTrackingProtectionDatabase: CoreDataDatabase +#endif + let bookmarksDatabase: CoreDataDatabase private weak var bookmarksDatabaseCleaner: BookmarkDatabaseCleaner? private let favoritesViewModel: FavoritesListInteracting @@ -128,6 +131,7 @@ class MainViewController: UIViewController { var viewCoordinator: MainViewCoordinator! +#if APP_TRACKING_PROTECTION init( bookmarksDatabase: CoreDataDatabase, bookmarksDatabaseCleaner: BookmarkDatabaseCleaner, @@ -149,6 +153,27 @@ class MainViewController: UIViewController { bindSyncService() } +#else + init( + bookmarksDatabase: CoreDataDatabase, + bookmarksDatabaseCleaner: BookmarkDatabaseCleaner, + syncService: DDGSyncing, + syncDataProviders: SyncDataProviders, + appSettings: AppSettings + ) { + self.bookmarksDatabase = bookmarksDatabase + self.bookmarksDatabaseCleaner = bookmarksDatabaseCleaner + self.syncService = syncService + self.syncDataProviders = syncDataProviders + self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase) + self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) + self.appSettings = appSettings + + super.init(nibName: nil, bundle: nil) + + bindSyncService() + } +#endif fileprivate var tabCountInfo: TabCountInfo? @@ -554,10 +579,15 @@ class MainViewController: UIViewController { AppDependencyProvider.shared.homePageConfiguration.refresh() let tabModel = currentTab?.tabModel + +#if APP_TRACKING_PROTECTION let controller = HomeViewController.loadFromStoryboard(model: tabModel!, favoritesViewModel: favoritesViewModel, appTPDatabase: appTrackingProtectionDatabase) - +#else + let controller = HomeViewController.loadFromStoryboard(model: tabModel!, favoritesViewModel: favoritesViewModel) +#endif + homeController = controller controller.chromeDelegate = self