Skip to content

Commit d2cb898

Browse files
committed
NotificationsViewController: Reload Failsafe
1 parent d107e3f commit d2cb898

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
4+
// MARK: - WooCommerce Notifications
5+
//
6+
extension NSNotification.Name {
7+
static let StoresManagerDidUpdateDefaultSite = NSNotification.Name(rawValue: "StoresManagerDidUpdateDefaultSite")
8+
}

WooCommerce/Classes/ViewRelated/Notifications/NotificationsViewController.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class NotificationsViewController: UIViewController {
7878

7979
// MARK: - View Lifecycle
8080

81+
deinit {
82+
stopListeningToNotifications()
83+
}
84+
8185
required init?(coder aDecoder: NSCoder) {
8286
super.init(coder: aDecoder)
8387
configureTabBarItem()
@@ -92,6 +96,8 @@ class NotificationsViewController: UIViewController {
9296
configureTableView()
9397
configureTableViewCells()
9498
configureResultsController()
99+
100+
startListeningToNotifications()
95101
}
96102

97103
override func viewWillAppear(_ animated: Bool) {
@@ -221,6 +227,19 @@ private extension NotificationsViewController {
221227
}
222228

223229

230+
// MARK: - ResultsController
231+
//
232+
extension NotificationsViewController {
233+
234+
/// Refreshes the Results Controller Predicate, and ensures the UI is in Sync.
235+
///
236+
func reloadResultsController() {
237+
resultsController.predicate = filter
238+
tableView.reloadData()
239+
}
240+
}
241+
242+
224243
// MARK: - UITableViewDataSource Conformance
225244
//
226245
extension NotificationsViewController: UITableViewDataSource {
@@ -403,6 +422,31 @@ private extension NotificationsViewController {
403422
}
404423

405424

425+
// MARK: - Notifications
426+
//
427+
extension NotificationsViewController {
428+
429+
/// Setup: Notification Hooks
430+
///
431+
func startListeningToNotifications() {
432+
let nc = NotificationCenter.default
433+
nc.addObserver(self, selector: #selector(defaultSiteWasUpdated), name: .StoresManagerDidUpdateDefaultSite, object: nil)
434+
}
435+
436+
/// Tear down the Notifications Hooks
437+
///
438+
func stopListeningToNotifications() {
439+
NotificationCenter.default.removeObserver(self)
440+
}
441+
442+
/// Default Site Updated Handler
443+
///
444+
@objc func defaultSiteWasUpdated() {
445+
reloadResultsController()
446+
}
447+
}
448+
449+
406450
// MARK: - Finite State Machine Management
407451
//
408452
private extension NotificationsViewController {

WooCommerce/Classes/Yosemite/StoresManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class StoresManager {
104104
func updateDefaultStore(storeID: Int) {
105105
sessionManager.defaultStoreID = storeID
106106
restoreSessionSiteIfPossible()
107+
108+
NotificationCenter.default.post(name: .StoresManagerDidUpdateDefaultSite, object: nil)
107109
}
108110
}
109111

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
B59D1EEC2190B08B009D1978 /* Age.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59D1EEB2190B08B009D1978 /* Age.swift */; };
126126
B59D49CD219B587E006BF0AD /* UILabel+OrderStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */; };
127127
B5A0369B214C0E8500774E2C /* CocoaLumberjack.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A0369A214C0E8500774E2C /* CocoaLumberjack.swift */; };
128+
B5A56BF5219F5AB20065A902 /* NSNotificationName+Woo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */; };
128129
B5A82EE221025C450053ADC8 /* FulfillViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A82EE121025C450053ADC8 /* FulfillViewController.swift */; };
129130
B5A82EE521025E550053ADC8 /* FulfillViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5A82EE421025E550053ADC8 /* FulfillViewController.xib */; };
130131
B5A82EE7210263460053ADC8 /* UIViewController+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A82EE6210263460053ADC8 /* UIViewController+Helpers.swift */; };
@@ -389,6 +390,7 @@
389390
B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+OrderStatus.swift"; sourceTree = "<group>"; };
390391
B59F38E020D40A24008C1829 /* WooCommerce.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WooCommerce.entitlements; sourceTree = "<group>"; };
391392
B5A0369A214C0E8500774E2C /* CocoaLumberjack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CocoaLumberjack.swift; sourceTree = "<group>"; };
393+
B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSNotificationName+Woo.swift"; sourceTree = "<group>"; };
392394
B5A82EE121025C450053ADC8 /* FulfillViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FulfillViewController.swift; sourceTree = "<group>"; };
393395
B5A82EE421025E550053ADC8 /* FulfillViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = FulfillViewController.xib; sourceTree = "<group>"; };
394396
B5A82EE6210263460053ADC8 /* UIViewController+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Helpers.swift"; sourceTree = "<group>"; };
@@ -948,6 +950,7 @@
948950
CE021534215BE3AB00C19555 /* LoginNavigationController+Woo.swift */,
949951
B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */,
950952
B53B3F38219C817800DF1EB6 /* UIStoryboard+Woo.swift */,
953+
B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */,
951954
);
952955
path = Extensions;
953956
sourceTree = "<group>";
@@ -1537,6 +1540,7 @@
15371540
CE263DE8206ACE3E0015A693 /* MainTabBarController.swift in Sources */,
15381541
CE14452E2188C11700A991D8 /* ZendeskManager.swift in Sources */,
15391542
B5BE75DB213F1D1E00909A14 /* OverlayMessageView.swift in Sources */,
1543+
B5A56BF5219F5AB20065A902 /* NSNotificationName+Woo.swift in Sources */,
15401544
B541B2152189EEA1008FE7C1 /* Scanner+Helpers.swift in Sources */,
15411545
743EDD9F214B05350039071B /* TopEarnerStatsItem+Woo.swift in Sources */,
15421546
CE1EC8EC20B8A3FF009762BF /* LeftImageTableViewCell.swift in Sources */,

0 commit comments

Comments
 (0)