Skip to content

Commit

Permalink
Properly refresh home page favorites view when updating favorites dis…
Browse files Browse the repository at this point in the history
…play mode
  • Loading branch information
ayoy committed Nov 10, 2023
1 parent 02329f8 commit 6fac824
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
44 changes: 38 additions & 6 deletions DuckDuckGo/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6fac824

Please sign in to comment.