Skip to content

Commit

Permalink
Merge pull request #192 from klaviyo/ab/CHNL-5395/refactor-getNotific…
Browse files Browse the repository at this point in the history
…ationSettings

[CHNL-5395] (partial) refactored environment.getNotificationSettings
  • Loading branch information
ab1470 authored Aug 28, 2024
2 parents 72a4458 + 519b30b commit 9654b10
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
7 changes: 3 additions & 4 deletions Sources/KlaviyoSwift/Klaviyo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,9 @@ public struct KlaviyoSDK {
/// Set the current user's push token. This will be associated with profile and can be used to send them push notificaitons.
/// - Parameter pushToken: String formatted push token.
public func set(pushToken: String) {
environment.getNotificationSettings { enablement in
dispatchOnMainThread(action: .setPushToken(
pushToken,
enablement))
Task {
let enablement = await environment.getNotificationSettings()
dispatchOnMainThread(action: .setPushToken(pushToken, enablement))
}
}

Expand Down
9 changes: 4 additions & 5 deletions Sources/KlaviyoSwift/KlaviyoEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct KlaviyoEnvironment {
var getUserDefaultString: (String) -> String?
var appLifeCycle: AppLifeCycleEvents
var notificationCenterPublisher: (NSNotification.Name) -> AnyPublisher<Notification, Never>
var getNotificationSettings: (@escaping (KlaviyoState.PushEnablement) -> Void) -> Void
var getNotificationSettings: () async -> KlaviyoState.PushEnablement
var getBackgroundSetting: () -> KlaviyoState.PushBackground
var legacyIdentifier: () -> String
var startReachability: () throws -> Void
Expand All @@ -58,10 +58,9 @@ struct KlaviyoEnvironment {
NotificationCenter.default.publisher(for: name)
.eraseToAnyPublisher()
},
getNotificationSettings: { callback in
UNUserNotificationCenter.current().getNotificationSettings { settings in
callback(.create(from: settings.authorizationStatus))
}
getNotificationSettings: {
let notificationSettings = await UNUserNotificationCenter.current().notificationSettings()
return KlaviyoState.PushEnablement.create(from: notificationSettings.authorizationStatus)
},
getBackgroundSetting: { .create(from: UIApplication.shared.backgroundRefreshStatus) },
legacyIdentifier: { "iOS:\(UIDevice.current.identifierForVendor!.uuidString)" },
Expand Down
2 changes: 1 addition & 1 deletion Tests/KlaviyoSwiftTests/KlaviyoTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension KlaviyoEnvironment {
getUserDefaultString: { _ in "value" },
appLifeCycle: AppLifeCycleEvents.test,
notificationCenterPublisher: { _ in Empty<Notification, Never>().eraseToAnyPublisher() },
getNotificationSettings: { callback in callback(.authorized) },
getNotificationSettings: { .authorized },
getBackgroundSetting: { .available },
legacyIdentifier: { "iOS:\(UUID(uuidString: "00000000-0000-0000-0000-000000000002")!.uuidString)" },
startReachability: {},
Expand Down

0 comments on commit 9654b10

Please sign in to comment.