From 3386a76e5cc0efb8cdd778a90703d3c0ca94596a Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Thu, 12 Oct 2023 19:56:41 -0700 Subject: [PATCH] =?UTF-8?q?Avoid=20AppTP=20database=20initialization=20whe?= =?UTF-8?q?n=20it=E2=80=99s=20not=20enabled.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DuckDuckGo/AppDelegate.swift | 14 +++++++++++++ DuckDuckGo/HomeViewController.swift | 26 ++++++++++++++++++----- DuckDuckGo/MainViewController.swift | 32 +++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 604b6db7f2..0b7b6ac1ce 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -54,7 +54,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? @@ -163,6 +167,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 { @@ -175,6 +180,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { fatalError("Could not create AppTP database stack: \(error?.localizedDescription ?? "err")") } } +#endif Favicons.shared.migrateFavicons(to: Favicons.Constants.maxFaviconSize) { WidgetCenter.shared.reloadAllTimelines() @@ -210,11 +216,19 @@ 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) +#else + let main = MainViewController(bookmarksDatabase: bookmarksDatabase, + bookmarksDatabaseCleaner: syncDataProviders.bookmarksAdapter.databaseCleaner, + syncService: syncService, + syncDataProviders: syncDataProviders) +#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 ceda17d760..48238dd5ad 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -80,8 +80,11 @@ class MainViewController: UIViewController { let previewsSource = TabPreviewsSource() fileprivate lazy var appSettings: AppSettings = AppUserDefaults() 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 @@ -125,6 +128,7 @@ class MainViewController: UIViewController { var viewCoordinator: MainViewCoordinator! +#if APP_TRACKING_PROTECTION init( bookmarksDatabase: CoreDataDatabase, bookmarksDatabaseCleaner: BookmarkDatabaseCleaner, @@ -144,6 +148,25 @@ class MainViewController: UIViewController { bindSyncService() } +#else + init( + bookmarksDatabase: CoreDataDatabase, + bookmarksDatabaseCleaner: BookmarkDatabaseCleaner, + syncService: DDGSyncing, + syncDataProviders: SyncDataProviders + ) { + self.bookmarksDatabase = bookmarksDatabase + self.bookmarksDatabaseCleaner = bookmarksDatabaseCleaner + self.syncService = syncService + self.syncDataProviders = syncDataProviders + self.favoritesViewModel = FavoritesListViewModel(bookmarksDatabase: bookmarksDatabase) + self.bookmarksCachingSearch = BookmarksCachingSearch(bookmarksStore: CoreDataBookmarksSearchStore(bookmarksStore: bookmarksDatabase)) + + super.init(nibName: nil, bundle: nil) + + bindSyncService() + } +#endif fileprivate var tabCountInfo: TabCountInfo? @@ -496,10 +519,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