Skip to content

Commit

Permalink
Avoid AppTP database initialization when it’s not enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Oct 13, 2023
1 parent e8e7fc7 commit 3386a76
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
14 changes: 14 additions & 0 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 21 additions & 5 deletions DuckDuckGo/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,41 @@ 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
HomeViewController(coder: coder, tabModel: model, favoritesViewModel: favoritesViewModel, appTPDatabase: appTPDatabase)
})
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")
}
Expand Down
32 changes: 30 additions & 2 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,6 +128,7 @@ class MainViewController: UIViewController {

var viewCoordinator: MainViewCoordinator!

#if APP_TRACKING_PROTECTION
init(
bookmarksDatabase: CoreDataDatabase,
bookmarksDatabaseCleaner: BookmarkDatabaseCleaner,
Expand All @@ -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?

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3386a76

Please sign in to comment.