Skip to content

Commit c12ede9

Browse files
authored
Refactor FXIOS-12995 [Swift 6 Migration] Update NotificationCenter usage to use Notifiable pattern (part 1) (#30250
* Update ToolbarButton to use Notifiable. * Update HistoryCoordinator to use Notifiable. * Update CreditCardTableViewController to use Notifiable. * Update CreditCardTableViewController to use Notifiable. * Update ReaderPanel to use Notifiable. * Update DownloadsPanel to use Notifiable. * Update SyncNowSetting to use Notifiable. * Update PasswordManagerListViewController to use Notifiable. * Update PasswordDetailViewController to use Notifiable. * Update AccountStatusSetting to use Notifiable. * Update SyncNowSetting to use Notifiable.
1 parent 4d95600 commit c12ede9

File tree

10 files changed

+177
-143
lines changed

10 files changed

+177
-143
lines changed

BrowserKit/Sources/ToolbarKit/ToolbarButton.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public enum ToolbarButtonGesture: Sendable {
1010
case longPress
1111
}
1212

13-
class ToolbarButton: UIButton, ThemeApplicable, UIGestureRecognizerDelegate {
13+
class ToolbarButton: UIButton,
14+
ThemeApplicable,
15+
UIGestureRecognizerDelegate,
16+
Notifiable {
1417
private struct UX {
1518
static let verticalInset: CGFloat = 10
1619
static let horizontalInset: CGFloat = 10
@@ -200,12 +203,10 @@ class ToolbarButton: UIButton, ThemeApplicable, UIGestureRecognizerDelegate {
200203
self.largeContentViewerInteraction = largeContentViewerInteraction
201204
addInteraction(largeContentViewerInteraction)
202205

203-
// FIXME: FXIOS-12995 Use Notifiable
204-
notificationCenter.addObserver(
205-
self,
206-
selector: #selector(largeContentViewerInteractionDidChange),
207-
name: UILargeContentViewerInteraction.enabledStatusDidChangeNotification,
208-
object: nil
206+
startObservingNotifications(
207+
withNotificationCenter: notificationCenter,
208+
forObserver: self,
209+
observing: [UILargeContentViewerInteraction.enabledStatusDidChangeNotification]
209210
)
210211
}
211212

@@ -295,9 +296,13 @@ class ToolbarButton: UIButton, ThemeApplicable, UIGestureRecognizerDelegate {
295296
}
296297
}
297298

298-
@objc
299-
private func largeContentViewerInteractionDidChange() {
300-
setMinimumPressDuration()
299+
// MARK: Notifiable
300+
func handleNotifications(_ notification: Notification) {
301+
guard notification.name == UILargeContentViewerInteraction.enabledStatusDidChangeNotification else { return }
302+
303+
ensureMainThread {
304+
self.setMinimumPressDuration()
305+
}
301306
}
302307

303308
// MARK: - ThemeApplicable

firefox-ios/Client/Coordinators/Library/HistoryCoordinator.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ protocol HistoryCoordinatorDelegate: AnyObject, LibraryPanelCoordinatorDelegate
1111
func showRecentlyClosedTab()
1212
}
1313

14-
class HistoryCoordinator: BaseCoordinator, HistoryCoordinatorDelegate {
14+
class HistoryCoordinator: BaseCoordinator,
15+
HistoryCoordinatorDelegate,
16+
Notifiable {
1517
// MARK: - Properties
1618

1719
private let profile: Profile
@@ -37,17 +39,17 @@ class HistoryCoordinator: BaseCoordinator, HistoryCoordinatorDelegate {
3739
self.navigationHandler = navigationHandler
3840
super.init(router: router)
3941

40-
// FIXME: FXIOS-12995 Use Notifiable
41-
self.notificationCenter.addObserver(
42-
self,
43-
selector: #selector(openClearHistory),
44-
name: .OpenClearRecentHistory,
45-
object: nil
42+
startObservingNotifications(
43+
withNotificationCenter: notificationCenter,
44+
forObserver: self,
45+
observing: [.OpenClearRecentHistory]
4646
)
4747
}
4848

49-
@objc
50-
private nonisolated func openClearHistory() {
49+
// MARK: Notifiable
50+
func handleNotifications(_ notification: Notification) {
51+
guard notification.name == .OpenClearRecentHistory else { return }
52+
5153
ensureMainThread {
5254
guard let historyPanel = self.router.rootViewController as? HistoryPanel else { return }
5355
historyPanel.showClearRecentHistory()

firefox-ios/Client/Frontend/Autofill/CreditCard/CreditCardSettingsView/CreditCardTableViewController.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import UIKit
88

99
import struct MozillaAppServices.CreditCard
1010

11-
class CreditCardTableViewController: UIViewController, Themeable {
11+
class CreditCardTableViewController: UIViewController,
12+
Themeable,
13+
Notifiable {
1214
// MARK: UX constants
1315
struct UX {
1416
static let toggleSwitchContainerHeight: CGFloat = 40
@@ -85,12 +87,11 @@ class CreditCardTableViewController: UIViewController, Themeable {
8587
listenForThemeChanges(withNotificationCenter: notificationCenter)
8688
applyTheme()
8789

88-
// FIXME: FXIOS-12995 Use Notifiable
89-
NotificationCenter.default.addObserver(
90-
self,
91-
selector: #selector(didFinishAnnouncement),
92-
name: UIAccessibility.announcementDidFinishNotification,
93-
object: nil)
90+
startObservingNotifications(
91+
withNotificationCenter: NotificationCenter.default,
92+
forObserver: self,
93+
observing: [UIAccessibility.announcementDidFinishNotification]
94+
)
9495
}
9596

9697
private func viewSetup() {
@@ -120,8 +121,10 @@ class CreditCardTableViewController: UIViewController, Themeable {
120121
tableView.reloadData()
121122
}
122123

123-
@objc
124-
func didFinishAnnouncement(notification: Notification) {
124+
// MARK: Notifiable
125+
func handleNotifications(_ notification: Notification) {
126+
guard notification.name == UIAccessibility.announcementDidFinishNotification else { return }
127+
125128
if let userInfo = notification.userInfo,
126129
let announcementText = userInfo[UIAccessibility.announcementStringValueUserInfoKey] as? String {
127130
let saveSuccessMessage: String = .CreditCard.SnackBar.SavedCardLabel
@@ -130,8 +133,11 @@ class CreditCardTableViewController: UIViewController, Themeable {
130133
if announcementText == saveSuccessMessage
131134
|| announcementText == updateSuccessMessage
132135
|| announcementText == removeCardMessage {
133-
if let lastIndex = lastSelectedIndex, let lastSelectedCell = tableView.cellForRow(at: lastIndex) {
134-
UIAccessibility.post(notification: .layoutChanged, argument: lastSelectedCell)
136+
ensureMainThread {
137+
if let lastIndex = self.lastSelectedIndex,
138+
let lastSelectedCell = self.tableView.cellForRow(at: lastIndex) {
139+
UIAccessibility.post(notification: .layoutChanged, argument: lastSelectedCell)
140+
}
135141
}
136142
}
137143
}

firefox-ios/Client/Frontend/Library/Bookmarks/BookmarksViewController.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import SiteImageView
1111
import MozillaAppServices
1212

1313
final class BookmarksViewController: SiteTableViewController,
14-
LibraryPanel,
15-
CanRemoveQuickActionBookmark,
16-
UITableViewDropDelegate {
14+
LibraryPanel,
15+
CanRemoveQuickActionBookmark,
16+
UITableViewDropDelegate,
17+
Notifiable {
1718
struct UX {
1819
static let FolderIconSize = CGSize(width: 24, height: 24)
1920
static let RowFlashDelay: TimeInterval = 0.4
@@ -122,9 +123,6 @@ final class BookmarksViewController: SiteTableViewController,
122123
}
123124

124125
deinit {
125-
// FIXME: FXIOS-12995 Use Notifiable
126-
notificationCenter.removeObserver(self)
127-
128126
// FXIOS-11315: Necessary to prevent BookmarksFolderEmptyStateView from being retained in memory
129127
a11yEmptyStateScrollView.removeFromSuperview()
130128
}
@@ -640,6 +638,18 @@ final class BookmarksViewController: SiteTableViewController,
640638
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
641639
updateEmptyState(animated: false)
642640
}
641+
642+
// MARK: - Notifiable
643+
func handleNotifications(_ notification: Notification) {
644+
switch notification.name {
645+
case .FirefoxAccountChanged, .ProfileDidFinishSyncing:
646+
ensureMainThread {
647+
self.reloadData()
648+
}
649+
default:
650+
break
651+
}
652+
}
643653
}
644654

645655
// MARK: - LibraryPanelContextMenu
@@ -741,20 +751,6 @@ extension BookmarksViewController: LibraryPanelContextMenu {
741751
}
742752
}
743753

744-
// MARK: - Notifiable
745-
extension BookmarksViewController: Notifiable {
746-
func handleNotifications(_ notification: Notification) {
747-
switch notification.name {
748-
case .FirefoxAccountChanged, .ProfileDidFinishSyncing:
749-
ensureMainThread {
750-
self.reloadData()
751-
}
752-
default:
753-
break
754-
}
755-
}
756-
}
757-
758754
// MARK: - Toolbar button actions
759755
extension BookmarksViewController {
760756
func bottomLeftButtonAction() {

firefox-ios/Client/Frontend/Library/Downloads/DownloadsPanel.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class DownloadsPanel: UIViewController,
1010
UITableViewDelegate,
1111
UITableViewDataSource,
1212
LibraryPanel,
13-
Themeable {
13+
Themeable,
14+
Notifiable {
1415
private struct UX {
1516
static let welcomeScreenTopPadding: CGFloat = 120
1617
static let welcomeScreenPadding: CGFloat = 15
@@ -26,10 +27,6 @@ class DownloadsPanel: UIViewController,
2627
var notificationCenter: NotificationProtocol
2728
private var viewModel = DownloadsPanelViewModel()
2829
private let logger: Logger
29-
private let events: [Notification.Name] = [.FileDidDownload,
30-
.PrivateDataClearedDownloadedFiles,
31-
UIContentSizeCategory.didChangeNotification,
32-
.DownloadPanelFileWasDeleted]
3330
let windowUUID: WindowUUID
3431
var currentWindowUUID: UUID? { windowUUID }
3532

@@ -62,15 +59,16 @@ class DownloadsPanel: UIViewController,
6259
self.windowUUID = windowUUID
6360
super.init(nibName: nil, bundle: nil)
6461

65-
// FIXME: FXIOS-12995 Use Notifiable
66-
events.forEach {
67-
NotificationCenter.default.addObserver(
68-
self,
69-
selector: #selector(notificationReceived),
70-
name: $0,
71-
object: nil
72-
)
73-
}
62+
startObservingNotifications(
63+
withNotificationCenter: NotificationCenter.default,
64+
forObserver: self,
65+
observing: [
66+
.FileDidDownload,
67+
.PrivateDataClearedDownloadedFiles,
68+
UIContentSizeCategory.didChangeNotification,
69+
.DownloadPanelFileWasDeleted
70+
]
71+
)
7472
}
7573

7674
required init?(coder aDecoder: NSCoder) {
@@ -108,26 +106,28 @@ class DownloadsPanel: UIViewController,
108106
tableView.delegate = nil
109107
}
110108

111-
@objc
112-
func notificationReceived(_ notification: Notification) {
113-
DispatchQueue.main.async { [weak self] in
114-
guard let self else { return }
109+
func handleNotifications(_ notification: Notification) {
110+
let notificationName = notification.name
111+
let notificationWindowUUID = notification.windowUUID
115112

116-
switch notification.name {
113+
ensureMainThread {
114+
switch notificationName {
117115
case .FileDidDownload, .PrivateDataClearedDownloadedFiles:
118116
self.reloadData()
117+
119118
case UIContentSizeCategory.didChangeNotification:
120119
self.reloadData()
121120
if self.emptyStateOverlayView.superview != nil {
122121
self.emptyStateOverlayView.removeFromSuperview()
123122
}
124123
self.emptyStateOverlayView = self.createEmptyStateOverlayView()
125-
break
124+
126125
case .DownloadPanelFileWasDeleted:
127-
guard let uuid = notification.windowUUID,
126+
guard let uuid = notificationWindowUUID,
128127
uuid != self.windowUUID else { return }
129128
// If another window's download panel has deleted a file, ensure we refresh our table
130129
self.reloadData()
130+
131131
default:
132132
break
133133
}

firefox-ios/Client/Frontend/Library/Reader/ReaderPanel.swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ class ReadingListTableViewCell: UITableViewCell, ThemeApplicable {
167167
class ReadingListPanel: UITableViewController,
168168
LibraryPanel,
169169
LibraryPanelContextMenu,
170-
Themeable {
170+
Themeable,
171+
Notifiable {
171172
weak var libraryPanelDelegate: LibraryPanelDelegate?
172173
weak var navigationHandler: ReadingListNavigationHandler?
173174
let profile: Profile
@@ -201,17 +202,15 @@ class ReadingListPanel: UITableViewController,
201202
self.state = .readingList
202203
super.init(nibName: nil, bundle: nil)
203204

204-
// FIXME: FXIOS-12995 Use Notifiable
205-
[ Notification.Name.FirefoxAccountChanged,
206-
UIContentSizeCategory.didChangeNotification,
207-
Notification.Name.DatabaseWasReopened ].forEach {
208-
NotificationCenter.default.addObserver(
209-
self,
210-
selector: #selector(notificationReceived),
211-
name: $0,
212-
object: nil
213-
)
214-
}
205+
startObservingNotifications(
206+
withNotificationCenter: NotificationCenter.default,
207+
forObserver: self,
208+
observing: [
209+
Notification.Name.FirefoxAccountChanged,
210+
UIContentSizeCategory.didChangeNotification,
211+
Notification.Name.DatabaseWasReopened
212+
]
213+
)
215214
}
216215

217216
required init!(coder aDecoder: NSCoder) {
@@ -258,14 +257,18 @@ class ReadingListPanel: UITableViewController,
258257
return themeManager.getCurrentTheme(for: windowUUID)
259258
}
260259

261-
@objc
262-
func notificationReceived(_ notification: Notification) {
260+
// MARK: Notifiable
261+
func handleNotifications(_ notification: Notification) {
263262
switch notification.name {
264263
case .FirefoxAccountChanged, UIContentSizeCategory.didChangeNotification:
265-
refreshReadingList()
264+
ensureMainThread {
265+
self.refreshReadingList()
266+
}
266267
case .DatabaseWasReopened:
267268
if let dbName = notification.object as? String, dbName == "ReadingList.db" {
268-
refreshReadingList()
269+
ensureMainThread {
270+
self.refreshReadingList()
271+
}
269272
}
270273
default:
271274
// no need to do anything at all

0 commit comments

Comments
 (0)