Skip to content

Commit d3c955d

Browse files
authored
clean code around NSNotification.Name (#600)
## Summary Some places defined strings that were then meant to be converted to NSNotification.Name. This MR removes that step so the `NSNotification.Name`s can be used directly. ## Validation Could use as before, notifications such as signing out and goals updated were still observed.
1 parent 83f7231 commit d3c955d

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
lines changed

BeeKit/Managers/CurrentUserManager.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ import SwiftyJSON
1717
public actor CurrentUserManager {
1818
let logger = Logger(subsystem: "com.beeminder.beeminder", category: "CurrentUserManager")
1919

20-
public static let signedInNotificationName = "com.beeminder.signedInNotification"
21-
public static let willSignOutNotificationName = "com.beeminder.willSignOutNotification"
22-
public static let failedSignInNotificationName = "com.beeminder.failedSignInNotification"
23-
public static let signedOutNotificationName = "com.beeminder.signedOutNotification"
24-
public static let resetNotificationName = "com.beeminder.resetNotification"
25-
public static let willResetNotificationName = "com.beeminder.willResetNotification"
26-
public static let healthKitMetricRemovedNotificationName = "com.beeminder.healthKitMetricRemovedNotification"
27-
20+
public enum NotificationName {
21+
public static let signedIn = NSNotification.Name(rawValue: "com.beeminder.signedInNotification")
22+
public static let willSignOut = NSNotification.Name(rawValue: "com.beeminder.willSignOutNotification")
23+
public static let failedSignIn = NSNotification.Name(rawValue: "com.beeminder.failedSignInNotification")
24+
public static let signedOut = NSNotification.Name(rawValue: "com.beeminder.signedOutNotification")
25+
public static let reset = NSNotification.Name(rawValue: "com.beeminder.resetNotification")
26+
public static let willReset = NSNotification.Name(rawValue: "com.beeminder.willResetNotification")
27+
public static let healthKitMetricRemoved = NSNotification.Name(rawValue: "com.beeminder.healthKitMetricRemovedNotification")
28+
}
29+
2830
fileprivate let beemiosSecret = "C0QBFPWqDykIgE6RyQ2OJJDxGxGXuVA2CNqcJM185oOOl4EQTjmpiKgcwjki"
2931

3032
internal static let accessTokenKey = "access_token"
@@ -165,28 +167,28 @@ public actor CurrentUserManager {
165167
self.setAccessToken(responseJSON[CurrentUserManager.accessTokenKey].string!)
166168

167169
await Task { @MainActor in
168-
NotificationCenter.default.post(name: Notification.Name(rawValue: CurrentUserManager.signedInNotificationName), object: self)
170+
NotificationCenter.default.post(name: CurrentUserManager.NotificationName.signedIn, object: self)
169171
}.value
170172
}
171173

172174
func handleFailedSignin(_ responseError: Error, errorMessage : String?) async throws {
173175
await Task { @MainActor in
174-
NotificationCenter.default.post(name: Notification.Name(rawValue: CurrentUserManager.failedSignInNotificationName), object: self, userInfo: ["error" : responseError])
176+
NotificationCenter.default.post(name: CurrentUserManager.NotificationName.failedSignIn, object: self, userInfo: ["error" : responseError])
175177
}.value
176178
try await self.signOut()
177179
}
178180

179181
public func signOut() async throws {
180182
await Task { @MainActor in
181-
NotificationCenter.default.post(name: Notification.Name(rawValue: CurrentUserManager.willSignOutNotificationName), object: self)
183+
NotificationCenter.default.post(name: CurrentUserManager.NotificationName.willSignOut, object: self)
182184
}.value
183185

184186
try deleteUser()
185187

186188
keychain.delete(CurrentUserManager.accessTokenKey)
187189

188190
await Task { @MainActor in
189-
NotificationCenter.default.post(name: Notification.Name(rawValue: CurrentUserManager.signedOutNotificationName), object: self)
191+
NotificationCenter.default.post(name: CurrentUserManager.NotificationName.signedOut, object: self)
190192
}.value
191193
}
192-
}
194+
}

BeeKit/Managers/GoalManager.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import OrderedCollections
1818
public actor GoalManager {
1919
private let logger = Logger(subsystem: "com.beeminder.beeminder", category: "GoalManager")
2020

21-
/// A notification that is triggered any time the data for one or more goals is updated
22-
public static let goalsUpdatedNotificationName = "com.beeminder.goalsUpdatedNotification"
23-
21+
public enum NotificationName {
22+
/// A notification that is triggered any time the data for one or more goals is updated
23+
public static let goalsUpdated = NSNotification.Name(rawValue: "com.beeminder.goalsUpdatedNotification")
24+
}
25+
2426
private let requestManager: RequestManager
2527
private nonisolated let currentUserManager: CurrentUserManager
2628

@@ -42,7 +44,7 @@ public actor GoalManager {
4244
// 2) Other methods may be called with the actor executor, so it is no longer safe for the constructor to
4345
// access class properties.
4446

45-
NotificationCenter.default.addObserver(self, selector: #selector(self.onSignedOutNotification), name: NSNotification.Name(rawValue: CurrentUserManager.signedOutNotificationName), object: nil)
47+
NotificationCenter.default.addObserver(self, selector: #selector(self.onSignedOutNotification), name: CurrentUserManager.NotificationName.signedOut, object: nil)
4648
}
4749

4850
/// Return the state of goals the last time they were fetched from the server. This could have been an arbitrarily long time ago.
@@ -132,7 +134,7 @@ public actor GoalManager {
132134
// Notify all listeners of the update
133135
await Task { @MainActor in
134136
modelContainer.viewContext.refreshAllObjects()
135-
NotificationCenter.default.post(name: Notification.Name(rawValue: GoalManager.goalsUpdatedNotificationName), object: self)
137+
NotificationCenter.default.post(name: GoalManager.NotificationName.goalsUpdated, object: self)
136138
}.value
137139
}
138140

BeeSwift/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
4141

4242
NetworkActivityIndicatorManager.shared.isEnabled = true
4343

44-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleGoalsUpdated), name: NSNotification.Name(rawValue: GoalManager.goalsUpdatedNotificationName), object: nil)
45-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUserSignedOut), name: NSNotification.Name(rawValue: CurrentUserManager.signedOutNotificationName), object: nil)
44+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleGoalsUpdated), name: GoalManager.NotificationName.goalsUpdated, object: nil)
45+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUserSignedOut), name: CurrentUserManager.NotificationName.signedOut, object: nil)
4646

4747
backgroundUpdates.startUpdatingRegularlyInBackground()
4848

BeeSwift/Components/GoalImageView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class GoalImageView : UIView {
5757
beeLemniscateView.isHidden = true
5858

5959
NotificationCenter.default.addObserver(
60-
forName: NSNotification.Name(rawValue: GoalManager.goalsUpdatedNotificationName),
60+
forName: GoalManager.NotificationName.goalsUpdated,
6161
object: nil,
6262
queue: OperationQueue.main
6363
) { [weak self] _ in

BeeSwift/Gallery/GalleryViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class GalleryViewController: UIViewController, UICollectionViewDelegateFlowLayou
6565
override func viewDidLoad() {
6666
super.viewDidLoad()
6767

68-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignIn), name: NSNotification.Name(rawValue: CurrentUserManager.signedInNotificationName), object: nil)
69-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignOut), name: NSNotification.Name(rawValue: CurrentUserManager.signedOutNotificationName), object: nil)
68+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignIn), name: CurrentUserManager.NotificationName.signedIn, object: nil)
69+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignOut), name: CurrentUserManager.NotificationName.signedOut, object: nil)
7070
NotificationCenter.default.addObserver(self, selector: #selector(self.openGoalFromNotification(_:)), name: NSNotification.Name(rawValue: "openGoal"), object: nil)
71-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleGoalsFetchedNotification), name: NSNotification.Name(rawValue: GoalManager.goalsUpdatedNotificationName), object: nil)
71+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleGoalsFetchedNotification), name: GoalManager.NotificationName.goalsUpdated, object: nil)
7272

7373
self.view.addSubview(stackView)
7474
stackView.axis = .vertical

BeeSwift/GoalViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
293293
self.navigationItem.rightBarButtonItems?.append(UIBarButtonItem(image: UIImage(systemName: "stopwatch"), style: .plain, target: self, action: #selector(self.timerButtonPressed)))
294294
}
295295

296-
NotificationCenter.default.addObserver(self, selector: #selector(onGoalsUpdatedNotification), name: NSNotification.Name(rawValue: GoalManager.goalsUpdatedNotificationName), object: nil)
296+
NotificationCenter.default.addObserver(self, selector: #selector(onGoalsUpdatedNotification), name: GoalManager.NotificationName.goalsUpdated, object: nil)
297297

298298
setValueTextField()
299299
updateInterfaceToMatchGoal()

BeeSwift/Settings/HealthKitConfigViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class HealthKitConfigViewController: UIViewController {
5050

5151
self.updateGoals()
5252

53-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleMetricRemovedNotification(notification:)), name: NSNotification.Name(rawValue: CurrentUserManager.healthKitMetricRemovedNotificationName), object: nil)
53+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleMetricRemovedNotification(notification:)), name: CurrentUserManager.NotificationName.healthKitMetricRemoved, object: nil)
5454
}
5555

5656
func updateGoals() {

BeeSwift/Settings/RemoveHKMetricViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class RemoveHKMetricViewController: UIViewController {
8989
hud.mode = .customView
9090
hud.customView = UIImageView(image: UIImage(systemName: "checkmark"))
9191

92-
NotificationCenter.default.post(name: Notification.Name(rawValue: CurrentUserManager.healthKitMetricRemovedNotificationName), object: self, userInfo: ["goal": self.goal as Any])
92+
NotificationCenter.default.post(name: CurrentUserManager.NotificationName.healthKitMetricRemoved, object: self, userInfo: ["goal": self.goal as Any])
9393

9494
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
9595
hud.hide(animated: true, afterDelay: 2)

BeeSwift/SignInViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class SignInViewController : UIViewController, UITextFieldDelegate {
3030
make.edges.equalTo(self.view)
3131
}
3232

33-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleFailedSignIn(_:)), name: NSNotification.Name(rawValue: CurrentUserManager.failedSignInNotificationName), object: nil)
34-
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignedIn(_:)), name: NSNotification.Name(rawValue: CurrentUserManager.signedInNotificationName), object: nil)
33+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleFailedSignIn(_:)), name: CurrentUserManager.NotificationName.failedSignIn, object: nil)
34+
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignedIn(_:)), name: CurrentUserManager.NotificationName.signedIn, object: nil)
3535
self.view.backgroundColor = UIColor.systemBackground
3636

3737

0 commit comments

Comments
 (0)