Skip to content

Commit

Permalink
Avoid AppTP DB initialization when disabled (#2090)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samsymons authored Nov 8, 2023
1 parent 81e555e commit 0f73f8a
Show file tree
Hide file tree
Showing 3 changed files with 67 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 @@ -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?
Expand Down Expand Up @@ -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 {
Expand All @@ -199,6 +204,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}
}
#endif

Favicons.shared.migrateFavicons(to: Favicons.Constants.maxFaviconSize) {
WidgetCenter.shared.reloadAllTimelines()
Expand Down Expand Up @@ -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)
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
34 changes: 32 additions & 2 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,6 +131,7 @@ class MainViewController: UIViewController {

var viewCoordinator: MainViewCoordinator!

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

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

0 comments on commit 0f73f8a

Please sign in to comment.