Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 7abaa99

Browse files
committed
Remove usage of Obj-C dynamic runtime features to remove observers
1 parent e0f0101 commit 7abaa99

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

WordPressAuthenticator/NUX/NUXTableViewController.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,29 @@ open class NUXTableViewController: UITableViewController, NUXViewControllerBase,
1818
return UIDevice.isPad() ? .all : .portrait
1919
}
2020

21+
// MARK: - Private
22+
private var notificationObservers: [NSObjectProtocol] = []
23+
2124
override open func viewDidLoad() {
2225
super.viewDidLoad()
2326
setupHelpButtonIfNeeded()
2427
setupCancelButtonIfNeeded()
2528
}
2629

27-
deinit {
28-
removeNotificationObservers()
29-
}
30-
3130
public func shouldShowCancelButton() -> Bool {
3231
return shouldShowCancelButtonBase()
3332
}
33+
34+
// MARK: - Notification Observers
35+
36+
public func addNotificationObserver(_ observer: NSObjectProtocol) {
37+
notificationObservers.append(observer)
38+
}
39+
40+
deinit {
41+
for observer in notificationObservers {
42+
NotificationCenter.default.removeObserver(observer)
43+
}
44+
notificationObservers.removeAll()
45+
}
3446
}

WordPressAuthenticator/NUX/NUXViewController.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ open class NUXViewController: UIViewController, NUXViewControllerBase, UIViewCon
1717
}
1818
}
1919

20+
// MARK: - Private
21+
private var notificationObservers: [NSObjectProtocol] = []
22+
2023
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
2124
return UIDevice.isPad() ? .all : .portrait
2225
}
@@ -28,10 +31,6 @@ open class NUXViewController: UIViewController, NUXViewControllerBase, UIViewCon
2831
setupBackgroundTapGestureRecognizer()
2932
}
3033

31-
deinit {
32-
removeNotificationObservers()
33-
}
34-
3534
// properties specific to NUXViewController
3635
@IBOutlet var submitButton: NUXButton?
3736
@IBOutlet var errorLabel: UILabel?
@@ -57,6 +56,19 @@ open class NUXViewController: UIViewController, NUXViewControllerBase, UIViewCon
5756
public func shouldShowCancelButton() -> Bool {
5857
return shouldShowCancelButtonBase()
5958
}
59+
60+
// MARK: - Notification Observers
61+
62+
public func addNotificationObserver(_ observer: NSObjectProtocol) {
63+
notificationObservers.append(observer)
64+
}
65+
66+
deinit {
67+
for observer in notificationObservers {
68+
NotificationCenter.default.removeObserver(observer)
69+
}
70+
notificationObservers.removeAll()
71+
}
6072
}
6173

6274
extension NUXViewController {

WordPressAuthenticator/NUX/NUXViewControllerBase.swift

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public protocol NUXViewControllerBase {
2828
///
2929
func shouldShowCancelButton() -> Bool
3030
func setupCancelButtonIfNeeded()
31+
32+
/// Notification observers that can be tied to the lifecycle of the entities implementing the protocol
33+
func addNotificationObserver(_ observer: NSObjectProtocol)
3134
}
3235

3336
/// extension for NUXViewControllerBase where the base class is UIViewController (and thus also NUXTableViewController)
@@ -286,44 +289,17 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro
286289
private func setupNotificationsIndicator() {
287290
helpNotificationIndicator.isHidden = true
288291

289-
wordpressSupportNotificationReceivedObserver = NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in
290-
self?.refreshSupportNotificationIndicator()
291-
}
292-
wordpressSupportNotificationClearedObserver = NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in
293-
self?.refreshSupportNotificationIndicator()
294-
}
295-
}
296-
297-
private var wordpressSupportNotificationReceivedObserver: NSObjectProtocol? {
298-
get {
299-
300-
objc_getAssociatedObject(self, &wordpressSupportNotificationReceivedKey) as? NSObjectProtocol
301-
}
302-
set {
303-
objc_setAssociatedObject(self, &wordpressSupportNotificationReceivedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
304-
}
305-
}
306-
307-
private var wordpressSupportNotificationClearedObserver: NSObjectProtocol? {
308-
get {
309-
objc_getAssociatedObject(self, &wordpressSupportNotificationClearedKey) as? NSObjectProtocol
310-
}
311-
set {
312-
objc_setAssociatedObject(self, &wordpressSupportNotificationClearedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
313-
}
314-
}
315-
316-
func removeNotificationObservers() {
317-
if let wordpressSupportNotificationReceivedObserver {
318-
NotificationCenter.default.removeObserver(wordpressSupportNotificationReceivedObserver)
319-
}
320-
321-
if let wordpressSupportNotificationClearedObserver {
322-
NotificationCenter.default.removeObserver(wordpressSupportNotificationClearedObserver)
323-
}
292+
addNotificationObserver(
293+
NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in
294+
self?.refreshSupportNotificationIndicator()
295+
}
296+
)
324297

325-
wordpressSupportNotificationReceivedObserver = nil
326-
wordpressSupportNotificationClearedObserver = nil
298+
addNotificationObserver(
299+
NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in
300+
self?.refreshSupportNotificationIndicator()
301+
}
302+
)
327303
}
328304

329305
private func layoutNotificationIndicatorView(_ view: UIView, to superView: UIView) {
@@ -355,6 +331,3 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro
355331
WordPressAuthenticator.shared.delegate?.presentSupport(from: navigationController, sourceTag: source, lastStep: state.lastStep, lastFlow: state.lastFlow)
356332
}
357333
}
358-
359-
private var wordpressSupportNotificationReceivedKey = 0
360-
private var wordpressSupportNotificationClearedKey = 0

0 commit comments

Comments
 (0)