diff --git a/DuckDuckGo/HomeViewController.swift b/DuckDuckGo/HomeViewController.swift index 4f97f8c46b..fd67f7965c 100644 --- a/DuckDuckGo/HomeViewController.swift +++ b/DuckDuckGo/HomeViewController.swift @@ -63,42 +63,64 @@ class HomeViewController: UIViewController { private let tabModel: Tab private let favoritesViewModel: FavoritesListInteracting + private let appSettings: AppSettings private var viewModelCancellable: AnyCancellable? + private var favoritesDisplayModeCancellable: AnyCancellable? #if APP_TRACKING_PROTECTION private let appTPHomeViewModel: AppTPHomeViewModel #endif #if APP_TRACKING_PROTECTION - static func loadFromStoryboard(model: Tab, favoritesViewModel: FavoritesListInteracting, appTPDatabase: CoreDataDatabase) -> HomeViewController { + static func loadFromStoryboard( + model: Tab, + favoritesViewModel: FavoritesListInteracting, + appSettings: AppSettings, + 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) + HomeViewController( + coder: coder, + tabModel: model, + favoritesViewModel: favoritesViewModel, + appSettings: appSettings, + appTPDatabase: appTPDatabase + ) }) return controller } #else - static func loadFromStoryboard(model: Tab, favoritesViewModel: FavoritesListInteracting) -> HomeViewController { + static func loadFromStoryboard(model: Tab, favoritesViewModel: FavoritesListInteracting, appSettings: AppSettings) -> HomeViewController { let storyboard = UIStoryboard(name: "Home", bundle: nil) let controller = storyboard.instantiateViewController(identifier: "HomeViewController", creator: { coder in - HomeViewController(coder: coder, tabModel: model, favoritesViewModel: favoritesViewModel) + HomeViewController(coder: coder, tabModel: model, favoritesViewModel: favoritesViewModel, appSettings: AppSettings) }) return controller } #endif #if APP_TRACKING_PROTECTION - required init?(coder: NSCoder, tabModel: Tab, favoritesViewModel: FavoritesListInteracting, appTPDatabase: CoreDataDatabase) { + required init?( + coder: NSCoder, + tabModel: Tab, + favoritesViewModel: FavoritesListInteracting, + appSettings: AppSettings, + appTPDatabase: CoreDataDatabase + ) { self.tabModel = tabModel self.favoritesViewModel = favoritesViewModel + self.appSettings = appSettings self.appTPHomeViewModel = AppTPHomeViewModel(appTrackingProtectionDatabase: appTPDatabase) super.init(coder: coder) } #else - required init?(coder: NSCoder, tabModel: Tab, favoritesViewModel: FavoritesListInteracting) { + required init?(coder: NSCoder, tabModel: Tab, favoritesViewModel: FavoritesListInteracting, appSettings: AppSettings) { self.tabModel = tabModel self.favoritesViewModel = favoritesViewModel + self.appSettings = appSettings super.init(coder: coder) } @@ -133,6 +155,16 @@ class HomeViewController: UIViewController { self.delegate?.home(self, didRequestHideLogo: false) } } + + favoritesDisplayModeCancellable = NotificationCenter.default.publisher(for: AppUserDefaults.Notifications.favoritesDisplayModeChange) + .receive(on: DispatchQueue.main) + .sink { [weak self] _ in + guard let self else { + return + } + self.favoritesViewModel.favoritesDisplayMode = self.appSettings.favoritesDisplayMode + self.collectionView.reloadData() + } } @objc func bookmarksDidChange() { diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index ebd8cfd37a..bbc4f9f9d3 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -645,9 +645,10 @@ class MainViewController: UIViewController { #if APP_TRACKING_PROTECTION let controller = HomeViewController.loadFromStoryboard(model: tabModel!, favoritesViewModel: favoritesViewModel, + appSettings: appSettings, appTPDatabase: appTrackingProtectionDatabase) #else - let controller = HomeViewController.loadFromStoryboard(model: tabModel!, favoritesViewModel: favoritesViewModel) + let controller = HomeViewController.loadFromStoryboard(model: tabModel!, favoritesViewModel: favoritesViewModel, appSettings: appSettings) #endif homeController = controller