Skip to content

Commit 74b1ffb

Browse files
Merge pull request #444 from woocommerce/issue/437-reloading-results-controller
NotificationsViewController: Reload Failsafe
2 parents dd80f1a + d2cb898 commit 74b1ffb

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
@@ -83,6 +83,10 @@ class NotificationsViewController: UIViewController {
8383

8484
// MARK: - View Lifecycle
8585

86+
deinit {
87+
stopListeningToNotifications()
88+
}
89+
8690
required init?(coder aDecoder: NSCoder) {
8791
super.init(coder: aDecoder)
8892
configureTabBarItem()
@@ -97,6 +101,8 @@ class NotificationsViewController: UIViewController {
97101
configureTableView()
98102
configureTableViewCells()
99103
configureResultsController()
104+
105+
startListeningToNotifications()
100106
}
101107

102108
override func viewWillAppear(_ animated: Bool) {
@@ -233,6 +239,19 @@ private extension NotificationsViewController {
233239
}
234240

235241

242+
// MARK: - ResultsController
243+
//
244+
extension NotificationsViewController {
245+
246+
/// Refreshes the Results Controller Predicate, and ensures the UI is in Sync.
247+
///
248+
func reloadResultsController() {
249+
resultsController.predicate = filter
250+
tableView.reloadData()
251+
}
252+
}
253+
254+
236255
// MARK: - UITableViewDataSource Conformance
237256
//
238257
extension NotificationsViewController: UITableViewDataSource {
@@ -415,6 +434,31 @@ private extension NotificationsViewController {
415434
}
416435

417436

437+
// MARK: - Notifications
438+
//
439+
extension NotificationsViewController {
440+
441+
/// Setup: Notification Hooks
442+
///
443+
func startListeningToNotifications() {
444+
let nc = NotificationCenter.default
445+
nc.addObserver(self, selector: #selector(defaultSiteWasUpdated), name: .StoresManagerDidUpdateDefaultSite, object: nil)
446+
}
447+
448+
/// Tear down the Notifications Hooks
449+
///
450+
func stopListeningToNotifications() {
451+
NotificationCenter.default.removeObserver(self)
452+
}
453+
454+
/// Default Site Updated Handler
455+
///
456+
@objc func defaultSiteWasUpdated() {
457+
reloadResultsController()
458+
}
459+
}
460+
461+
418462
// MARK: - Finite State Machine Management
419463
//
420464
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
@@ -126,6 +126,7 @@
126126
B59D1EEC2190B08B009D1978 /* Age.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59D1EEB2190B08B009D1978 /* Age.swift */; };
127127
B59D49CD219B587E006BF0AD /* UILabel+OrderStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */; };
128128
B5A0369B214C0E8500774E2C /* CocoaLumberjack.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A0369A214C0E8500774E2C /* CocoaLumberjack.swift */; };
129+
B5A56BF5219F5AB20065A902 /* NSNotificationName+Woo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */; };
129130
B5A82EE221025C450053ADC8 /* FulfillViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A82EE121025C450053ADC8 /* FulfillViewController.swift */; };
130131
B5A82EE521025E550053ADC8 /* FulfillViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5A82EE421025E550053ADC8 /* FulfillViewController.xib */; };
131132
B5A82EE7210263460053ADC8 /* UIViewController+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A82EE6210263460053ADC8 /* UIViewController+Helpers.swift */; };
@@ -391,6 +392,7 @@
391392
B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+OrderStatus.swift"; sourceTree = "<group>"; };
392393
B59F38E020D40A24008C1829 /* WooCommerce.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WooCommerce.entitlements; sourceTree = "<group>"; };
393394
B5A0369A214C0E8500774E2C /* CocoaLumberjack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CocoaLumberjack.swift; sourceTree = "<group>"; };
395+
B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSNotificationName+Woo.swift"; sourceTree = "<group>"; };
394396
B5A82EE121025C450053ADC8 /* FulfillViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FulfillViewController.swift; sourceTree = "<group>"; };
395397
B5A82EE421025E550053ADC8 /* FulfillViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = FulfillViewController.xib; sourceTree = "<group>"; };
396398
B5A82EE6210263460053ADC8 /* UIViewController+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Helpers.swift"; sourceTree = "<group>"; };
@@ -951,6 +953,7 @@
951953
CE021534215BE3AB00C19555 /* LoginNavigationController+Woo.swift */,
952954
B59D49CC219B587E006BF0AD /* UILabel+OrderStatus.swift */,
953955
B53B3F38219C817800DF1EB6 /* UIStoryboard+Woo.swift */,
956+
B5A56BF4219F5AB20065A902 /* NSNotificationName+Woo.swift */,
954957
);
955958
path = Extensions;
956959
sourceTree = "<group>";
@@ -1540,6 +1543,7 @@
15401543
CE263DE8206ACE3E0015A693 /* MainTabBarController.swift in Sources */,
15411544
CE14452E2188C11700A991D8 /* ZendeskManager.swift in Sources */,
15421545
B5BE75DB213F1D1E00909A14 /* OverlayMessageView.swift in Sources */,
1546+
B5A56BF5219F5AB20065A902 /* NSNotificationName+Woo.swift in Sources */,
15431547
B541B2152189EEA1008FE7C1 /* Scanner+Helpers.swift in Sources */,
15441548
743EDD9F214B05350039071B /* TopEarnerStatsItem+Woo.swift in Sources */,
15451549
CE1EC8EC20B8A3FF009762BF /* LeftImageTableViewCell.swift in Sources */,

0 commit comments

Comments
 (0)