Skip to content

Commit 09c5040

Browse files
committed
Add swift style observers so we don't have to depend on Objc runtime in Swift code
1 parent f8b18f6 commit 09c5040

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/PushNotification/PushNotificationManager.swift

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class PushNotificationManager: NSObject {
7979

8080
var isSimulator: Bool = false
8181
private let notificationRegister: RemoteNotificationRegistering
82-
private var restClient: RestClient?
8382
private var preferences: SFPreferences?
8483

8584
private var loginObserver: NSObjectProtocol?
@@ -93,8 +92,7 @@ public class PushNotificationManager: NSObject {
9392
@objc
9493
public override convenience init() {
9594
self.init(notificationRegister: DefaultRemoteNotificationRegistrar(),
96-
restClient: RestClient.shared,
97-
preferences: SFPreferences.sharedPreferences(for: .user, user: UserAccountManager.shared.currentUserAccount))
95+
preferences: SFPreferences.sharedPreferences(for: .user, user: UserAccountManager.shared.currentUserAccount))
9896
}
9997

10098
/// Internal initializer used for testing and dependency injection.
@@ -103,10 +101,8 @@ public class PushNotificationManager: NSObject {
103101
/// - Parameter restClient: The REST client for making API calls. Defaults to RestClient.shared.
104102
/// - Parameter preferences: The preferences store to use. Defaults to user-level SFPreferences.
105103
internal init(notificationRegister: RemoteNotificationRegistering = DefaultRemoteNotificationRegistrar(),
106-
restClient : RestClient? = RestClient.shared,
107104
preferences: SFPreferences? = nil) {
108105
self.notificationRegister = notificationRegister
109-
self.restClient = restClient
110106

111107
if preferences == nil {
112108
self.preferences = SFPreferences.currentUserLevel()
@@ -130,9 +126,15 @@ public class PushNotificationManager: NSObject {
130126

131127
deinit {
132128
let center = NotificationCenter.default
133-
center.removeObserver(loginObserver ?? self)
134-
center.removeObserver(logoutObserver ?? self)
135-
center.removeObserver(enterForegroundObserver ?? self)
129+
if let loginObserver = loginObserver {
130+
center.removeObserver(loginObserver)
131+
}
132+
if let logoutObserver = logoutObserver {
133+
center.removeObserver(logoutObserver)
134+
}
135+
if let enterForegroundObserver = enterForegroundObserver {
136+
center.removeObserver(enterForegroundObserver)
137+
}
136138
}
137139

138140
/// Registers the app with Apple Push Notification Service (APNS).
@@ -214,6 +216,7 @@ public class PushNotificationManager: NSObject {
214216
return false
215217
}
216218

219+
let restClient = notificationRegister.client(for: user)
217220
let apiVersion = restClient?.apiVersion ?? SFRestDefaultAPIVersion
218221
let path = "/\(apiVersion)/\(PushNotificationConstants.endPoint)"
219222
let request = RestRequest(method: .POST, path: path, queryParams: nil)
@@ -337,7 +340,7 @@ public class PushNotificationManager: NSObject {
337340
SFSDKCoreLogger.e(Self.self, message: "Cannot unregister from notifications with Salesforce: no deviceSalesforceId")
338341
return false
339342
}
340-
343+
let restClient = notificationRegister.client(for: user)
341344
let apiVersion = restClient?.apiVersion ?? SFRestDefaultAPIVersion
342345
let path = "/\(apiVersion)/\(PushNotificationConstants.endPoint)/\(sfId)"
343346
let request = RestRequest(method: .DELETE,
@@ -455,12 +458,6 @@ private extension PushNotificationManager {
455458
queue: .main
456459
) { [weak self] in self?.onUserLoggedIn($0) }
457460

458-
logoutObserver = NotificationCenter.default.addObserver(
459-
forName: UserAccountManager.didLogoutUser,
460-
object: nil,
461-
queue: .main
462-
) { [weak self] in self?.onUserLoggedOut($0) }
463-
464461
enterForegroundObserver = NotificationCenter.default.addObserver(
465462
forName: UIApplication.willEnterForegroundNotification,
466463
object: nil,
@@ -469,7 +466,6 @@ private extension PushNotificationManager {
469466
}
470467

471468
@objc private func onUserLoggedIn(_ notification: Notification) {
472-
restClient = RestClient.shared
473469
if deviceToken != nil {
474470
SFSDKCoreLogger.i(Self.self, message: "User logged in, registering push")
475471
_ = registerSalesforceNotifications(completionBlock: nil, failBlock: nil)
@@ -484,10 +480,6 @@ private extension PushNotificationManager {
484480
}
485481

486482
SFSDKCoreLogger.i(Self.self, message: "App entering foreground, re-registering push")
487-
_ = registerSalesforceNotifications(completionBlock: nil, failBlock: nil)
488-
}
489-
490-
@objc private func onUserLoggedOut(_ notification: Notification) {
491-
restClient = RestClient.shared
483+
registerSalesforceNotifications(completionBlock: nil, failBlock: nil)
492484
}
493485
}

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/PushNotification/RemoteNotificationRegistering.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,24 @@ import Foundation
3131

3232
public protocol RemoteNotificationRegistering {
3333
func registerForRemoteNotifications()
34+
35+
func client(for user: UserAccount?) -> RestClient?
3436
}
3537

3638
// MARK: - Default Implementation
3739

38-
class DefaultRemoteNotificationRegistrar: RemoteNotificationRegistering {
40+
final class DefaultRemoteNotificationRegistrar: RemoteNotificationRegistering {
41+
3942
public init() {}
4043

4144
public func registerForRemoteNotifications() {
4245
SFApplicationHelper.sharedApplication()?.registerForRemoteNotifications()
4346
}
47+
48+
public func client(for user: UserAccount?) -> RestClient? {
49+
guard let account = user else {
50+
return RestClient.shared
51+
}
52+
return RestClient.restClient(for: account)
53+
}
4454
}

libs/SalesforceSDKCore/SalesforceSDKCoreTests/PushNotificationManagerTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class PushNotificationManagerTests: XCTestCase {
1616

1717
mockRestClient = MockRestClient()
1818
mockRestClient.apiVersion = SFRestDefaultAPIVersion
19+
mockApplicationHelper.client = mockRestClient
1920
mockUserAccount = UserAccount()
2021
UserAccountManager.shared.currentUserAccount = mockUserAccount
2122
mockPreferences = MockPreferences()
2223
mockPreferences.setObject("mock-sfid", forKey: PushNotificationConstants.deviceSalesforceId)
2324
pushNotificationManager = PushNotificationManager(notificationRegister: mockApplicationHelper,
24-
restClient: mockRestClient,
2525
preferences: mockPreferences)
2626
pushNotificationManager.isSimulator = false
2727
}
@@ -1040,7 +1040,13 @@ class ActionTypeTests: XCTestCase {
10401040
}
10411041

10421042
// MARK: - Mocks
1043-
class MockApplicationHelper: RemoteNotificationRegistering {
1043+
class MockApplicationHelper: RemoteNotificationRegistering {
1044+
var client: RestClient?
1045+
1046+
func client(for user: UserAccount?) -> RestClient? {
1047+
client
1048+
}
1049+
10441050
var registerForRemoteNotificationsCalled = false
10451051

10461052
func registerForRemoteNotifications() {

0 commit comments

Comments
 (0)