Skip to content

Commit

Permalink
Fix watchOS
Browse files Browse the repository at this point in the history
  • Loading branch information
rlepinski committed May 27, 2023
1 parent 8c8c2c6 commit 3cf702d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 56 deletions.
6 changes: 4 additions & 2 deletions Airship/AirshipCore/Source/Airship.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public class Airship: NSObject {
/// Initalizes Airship. Config will be read from `AirshipConfig.plist`.

@objc
@MainActor
public class func takeOff() {
takeOff(nil)
}
Expand All @@ -279,7 +280,8 @@ public class Airship: NSObject {
/// - Parameters:
/// - config: The Airship config.
@objc
public class func takeOff(_ config: Config?) {
@MainActor
public class func takeOff(_ config: AirshipConfig?) {

guard Thread.isMainThread else {
fatalError("TakeOff must be called on the main thread.")
Expand All @@ -304,7 +306,7 @@ public class Airship: NSObject {
}
}

let resolvedConfig = config?.copy() as? Config ?? Config.default()
let resolvedConfig = config?.copy() as? AirshipConfig ?? AirshipConfig.default()

guard resolvedConfig.validate() else {
AirshipLogger.impError("Config is invalid. Unable to takeOff.")
Expand Down
15 changes: 10 additions & 5 deletions Airship/AirshipCore/Source/AppStateTrackerAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum AppLifeCycleEvent: Sendable {


#if os(watchOS)
final class DefaultAppStateTrackerAdapter: ApplicationStateProvider, Sendable {
final class DefaultAppStateTrackerAdapter: AppStateTrackerAdapter, Sendable {
var state: ApplicationState {
let appState = WKExtension.shared().applicationState
switch appState {
Expand All @@ -49,7 +49,7 @@ final class DefaultAppStateTrackerAdapter: ApplicationStateProvider, Sendable {
object: nil,
queue: nil,
using: { _ in
MainActor.runUnsafe {
DefaultAppStateTrackerAdapter.runUnsafe {
eventHandler(.didBecomeActive)
}
}
Expand All @@ -61,7 +61,7 @@ final class DefaultAppStateTrackerAdapter: ApplicationStateProvider, Sendable {
object: nil,
queue: nil,
using: { _ in
MainActor.runUnsafe {
DefaultAppStateTrackerAdapter.runUnsafe {
eventHandler(.willResignActive)
}
}
Expand All @@ -73,7 +73,7 @@ final class DefaultAppStateTrackerAdapter: ApplicationStateProvider, Sendable {
object: nil,
queue: nil,
using: { _ in
MainActor.runUnsafe {
DefaultAppStateTrackerAdapter.runUnsafe {
eventHandler(.willEnterForeground)
}
}
Expand All @@ -85,12 +85,17 @@ final class DefaultAppStateTrackerAdapter: ApplicationStateProvider, Sendable {
object: nil,
queue: nil,
using: { _ in
MainActor.runUnsafe {
DefaultAppStateTrackerAdapter.runUnsafe {
eventHandler(.didEnterBackground)
}
}
)
}

@MainActor(unsafe)
static func runUnsafe(_ block: @MainActor () -> Void) {
block()
}
}
#else
final class DefaultAppStateTrackerAdapter: AppStateTrackerAdapter, Sendable {
Expand Down
1 change: 1 addition & 0 deletions Airship/AirshipCore/Source/DeepLinkAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class DeepLinkAction: NSObject, Action {

#else
WKExtension.shared().openSystemURL(url)
return ActionResult.empty()
#endif

}
Expand Down
73 changes: 31 additions & 42 deletions Airship/AirshipCore/Source/DefaultAppIntegrationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,62 +265,51 @@ class DefaultAppIntegrationDelegate: NSObject, AppIntegrationDelegate {
presentationOptions: UNNotificationPresentationOptions?,
completionHandler: @escaping (WKBackgroundFetchResult) -> Void
) {
AirshipLogger.info(
"Application received remote notification: \(userInfo)"
)
Task {
AirshipLogger.info(
"Application received remote notification: \(userInfo)"
)

let situation =
let situation =
isForeground
? Situation.foregroundPush : Situation.backgroundPush
let dispatchGroup = DispatchGroup()
var fetchResults: [UInt] = []
let lock = AirshipLock()
var metadata: [AnyHashable: Any] = [:]
metadata[UAActionMetadataPushPayloadKey] = userInfo
let dispatchGroup = DispatchGroup()
var fetchResults: [UInt] = []
let lock = AirshipLock()
var metadata: [AnyHashable: Any] = [:]
metadata[UAActionMetadataPushPayloadKey] = userInfo

if let presentationOptions = presentationOptions {
metadata[UAActionMetadataForegroundPresentationKey] =
if let presentationOptions = presentationOptions {
metadata[UAActionMetadataForegroundPresentationKey] =
self.isForegroundPresentation(presentationOptions)
}
}

// Pushable components
self.pushableComponents.forEach {
if $0.receivedRemoteNotification != nil {
dispatchGroup.enter()
$0.receivedRemoteNotification?(userInfo) { fetchResult in
lock.sync {
fetchResults.append(fetchResult.rawValue)
// Pushable components
self.pushableComponents.forEach {
if $0.receivedRemoteNotification != nil {
dispatchGroup.enter()
$0.receivedRemoteNotification?(userInfo) { fetchResult in
lock.sync {
fetchResults.append(fetchResult.rawValue)
}
dispatchGroup.leave()
}
dispatchGroup.leave()
}
}
}

// Actions -> Push
dispatchGroup.enter()
ActionRunner.run(
actionValues: userInfo,
situation: situation,
metadata: metadata
) { result in

let result = await ActionRunner.run(
actionValues: userInfo,
situation: situation,
metadata: metadata
)
lock.sync {
fetchResults.append(UInt(result.fetchResult.rawValue))
}
self.push.didReceiveRemoteNotification(
userInfo,
isForeground: isForeground
) { pushResult in
lock.sync {
let result: WKBackgroundFetchResult =
pushResult as! WKBackgroundFetchResult
fetchResults.append(result.rawValue)
}
dispatchGroup.leave()
}
}

dispatchGroup.notify(queue: .main) {
completionHandler(Utils.mergeFetchResults(fetchResults))
dispatchGroup.notify(queue: .main) {
completionHandler(AirshipUtils.mergeFetchResults(fetchResults))
}
}
}
#endif
Expand Down
1 change: 1 addition & 0 deletions Airship/AirshipCore/Source/OpenExternalURLAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class OpenExternalURLAction: NSObject, Action {

#else
WKExtension.shared().openSystemURL(url)
return ActionResult(value: url.absoluteString)
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Airship/AirshipCore/Source/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ fileprivate extension View {
action: @escaping ([PagerGestureLocation]) -> Void
) -> some View {
#if !os(tvOS)
if #available(iOS 16.0, *) {
if #available(iOS 16.0, watchOS 9.0, *) {
let pagerGestureEplorer = PagerGestureMapExplorer(
CGRect(
x: 0,
Expand Down
14 changes: 8 additions & 6 deletions Airship/AirshipCore/Source/WorkBackgroundTasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ protocol WorkBackgroundTasksProtocol: Sendable {

final class WorkBackgroundTasks: WorkBackgroundTasksProtocol, Sendable {

#if !os(watchOS)
private let requestMap: AirshipMainActorWrapper<[UInt: UIBackgroundTaskIdentifier]> = AirshipMainActorWrapper([:])
private let nextRequestID: AirshipMainActorWrapper<UInt> = AirshipMainActorWrapper(0)
#endif

@MainActor
func beginTask(
_ name: String,
expirationHandler: (@Sendable () -> Void)? = nil
) throws -> AirshipCancellable {
#if os(watchOS)
let cancellable: CancellabelValueHolder<UInt> = CancellabelValueHolder(value: 0) { _ in
}
return cancellable
#else

AirshipLogger.trace("Requesting task: \(name)")

let requestID = nextRequestID.value
Expand All @@ -32,11 +40,6 @@ final class WorkBackgroundTasks: WorkBackgroundTasksProtocol, Sendable {
}
}

#if os(watchOS)
return cancellable
#else


let application = UIApplication.shared
self.requestMap.value[requestID] = application.beginBackgroundTask(withName: name) {
AirshipLogger.trace("Task expired: \(name)")
Expand All @@ -56,7 +59,6 @@ final class WorkBackgroundTasks: WorkBackgroundTasksProtocol, Sendable {
@MainActor
private func cancel(requestID: UInt) {
#if !os(watchOS)

guard let taskID = self.requestMap.value.removeValue(forKey: requestID),
taskID != UIBackgroundTaskIdentifier.invalid
else {
Expand Down

0 comments on commit 3cf702d

Please sign in to comment.