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

Commit a292ac0

Browse files
committed
Remove notification observers for NUXViewControllerBase
NUXViewControllerBase is implemented as a protocol. In order not to change architecture using Obj-C runtime feature to create observers and deinitialize them on deinit
1 parent 43d7011 commit a292ac0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

WordPressAuthenticator/NUX/NUXTableViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ open class NUXTableViewController: UITableViewController, NUXViewControllerBase,
2323
setupHelpButtonIfNeeded()
2424
setupCancelButtonIfNeeded()
2525
}
26+
27+
deinit {
28+
removeNotificationObservers()
29+
}
2630

2731
public func shouldShowCancelButton() -> Bool {
2832
return shouldShowCancelButtonBase()

WordPressAuthenticator/NUX/NUXViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ open class NUXViewController: UIViewController, NUXViewControllerBase, UIViewCon
2727
setupCancelButtonIfNeeded()
2828
setupBackgroundTapGestureRecognizer()
2929
}
30+
31+
deinit {
32+
removeNotificationObservers()
33+
}
3034

3135
// properties specific to NUXViewController
3236
@IBOutlet var submitButton: NUXButton?

WordPressAuthenticator/NUX/NUXViewControllerBase.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,46 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro
286286
private func setupNotificationsIndicator() {
287287
helpNotificationIndicator.isHidden = true
288288

289-
NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in
289+
wordpressSupportNotificationReceivedObserver = NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in
290290
self?.refreshSupportNotificationIndicator()
291291
}
292-
NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in
292+
wordpressSupportNotificationClearedObserver = NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in
293293
self?.refreshSupportNotificationIndicator()
294294
}
295295
}
296296

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+
}
324+
325+
wordpressSupportNotificationReceivedObserver = nil
326+
wordpressSupportNotificationClearedObserver = nil
327+
}
328+
297329
private func layoutNotificationIndicatorView(_ view: UIView, to superView: UIView) {
298330
superView.addSubview(view)
299331
view.translatesAutoresizingMaskIntoConstraints = false
@@ -323,3 +355,6 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro
323355
WordPressAuthenticator.shared.delegate?.presentSupport(from: navigationController, sourceTag: source, lastStep: state.lastStep, lastFlow: state.lastFlow)
324356
}
325357
}
358+
359+
private var wordpressSupportNotificationReceivedKey = 0
360+
private var wordpressSupportNotificationClearedKey = 0

0 commit comments

Comments
 (0)