Skip to content

Commit

Permalink
Add hybrid app hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rlepinski committed Oct 23, 2024
1 parent 135272d commit e6fb71d
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 135 deletions.
4 changes: 4 additions & 0 deletions ios/AirshipFrameworkProxy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
6E5AD11A2C82388900EDC110 /* EmbeddedEventEmitter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5AD1192C82388900EDC110 /* EmbeddedEventEmitter.swift */; };
6EC7553E2A4A449300851ABB /* DefaultMessageCenterUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC7553D2A4A449300851ABB /* DefaultMessageCenterUI.swift */; };
6EC755402A4A494C00851ABB /* NotificationStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC7553F2A4A494C00851ABB /* NotificationStatus.swift */; };
6EC9A28B2CC963B100FFF4C5 /* AirshipPluginForwardListeners.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC9A28A2CC963A800FFF4C5 /* AirshipPluginForwardListeners.swift */; };
6ED117AF2CA750F400C41C56 /* AirshipFrameworkProxyLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ED117AE2CA750F000C41C56 /* AirshipFrameworkProxyLoader.swift */; };
6ED117B22CA75D1500C41C56 /* AirshipPluginLoaderProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ED117B12CA75D0F00C41C56 /* AirshipPluginLoaderProtocol.swift */; };
6ED117B52CA75D2300C41C56 /* AirshipPluginExtenderProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ED117B42CA75D2300C41C56 /* AirshipPluginExtenderProtocol.swift */; };
Expand Down Expand Up @@ -106,6 +107,7 @@
6E5AD1192C82388900EDC110 /* EmbeddedEventEmitter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmbeddedEventEmitter.swift; sourceTree = "<group>"; };
6EC7553D2A4A449300851ABB /* DefaultMessageCenterUI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultMessageCenterUI.swift; sourceTree = "<group>"; };
6EC7553F2A4A494C00851ABB /* NotificationStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationStatus.swift; sourceTree = "<group>"; };
6EC9A28A2CC963A800FFF4C5 /* AirshipPluginForwardListeners.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirshipPluginForwardListeners.swift; sourceTree = "<group>"; };
6ED117A82CA74F9100C41C56 /* UAirshipFrameworkProxyLoader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UAirshipFrameworkProxyLoader.h; sourceTree = "<group>"; };
6ED117A92CA74F9100C41C56 /* UAirshipFrameworkProxyLoader.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UAirshipFrameworkProxyLoader.m; sourceTree = "<group>"; };
6ED117AE2CA750F000C41C56 /* AirshipFrameworkProxyLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AirshipFrameworkProxyLoader.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,6 +166,7 @@
6E142E4E296E3A8800F71E23 /* AirshipFrameworkProxy */ = {
isa = PBXGroup;
children = (
6EC9A28A2CC963A800FFF4C5 /* AirshipPluginForwardListeners.swift */,
6ED117B32CA75D1B00C41C56 /* Loader */,
6E3A028F2CA60B1C00B0C5CF /* LiveActivity */,
6EFB83D82979BB1F0008BEB5 /* Proxies */,
Expand Down Expand Up @@ -480,6 +483,7 @@
6EFB83DA2979BE7F0008BEB5 /* ScopedSubscriptionListOperation.swift in Sources */,
6E142EBE296F85CD00F71E23 /* ProxyStore.swift in Sources */,
6EC755402A4A494C00851ABB /* NotificationStatus.swift in Sources */,
6EC9A28B2CC963B100FFF4C5 /* AirshipPluginForwardListeners.swift in Sources */,
6E142EBF296F85CD00F71E23 /* PushUtils.swift in Sources */,
6E142EC0296F85CD00F71E23 /* SubscriptionListOperation.swift in Sources */,
6E142EC2296F87DA00F71E23 /* AirshipProxyEventEmitter.swift in Sources */,
Expand Down
255 changes: 157 additions & 98 deletions ios/AirshipFrameworkProxy/AirshipDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import AirshipPreferenceCenter
import AirshipAutomation
#endif

class AirshipDelegate: NSObject,
PushNotificationDelegate,
MessageCenterDisplayDelegate,
PreferenceCenterOpenDelegate,
RegistrationDelegate,
DeepLinkDelegate
{
class AirshipDelegate: NSObject {

let proxyStore: ProxyStore
let eventEmitter: AirshipProxyEventEmitter
Expand All @@ -28,74 +22,8 @@ class AirshipDelegate: NSObject,
) {
self.proxyStore = proxyStore
self.eventEmitter = eventEmitter


super.init()
}

func displayMessageCenter(messageID: String) {
Task { @MainActor in
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display(messageID: messageID)
return
}

await self.eventEmitter.addEvent(
DisplayMessageCenterEvent(
messageID: messageID
)
)
}
}

func displayMessageCenter() {
Task { @MainActor in
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display()
return
}

await self.eventEmitter.addEvent(
DisplayMessageCenterEvent()
)
}
}

func dismissMessageCenter() {
Task { @MainActor in
DefaultMessageCenterUI.shared.dismiss()
}
}

func openPreferenceCenter(
_ preferenceCenterID: String
) -> Bool {
let autoLaunch = self.proxyStore.shouldAutoLaunchPreferenceCenter(
preferenceCenterID
)

guard !autoLaunch else {
return false
}

Task {
await self.eventEmitter.addEvent(
DisplayPreferenceCenterEvent(
preferenceCenterID: preferenceCenterID
)
)
}

return true
}

func receivedDeepLink(
_ deepLink: URL
) async {
await self.eventEmitter.addEvent(
DeepLinkEvent(deepLink)
)
}

func messageCenterInboxUpdated() {
Task {
Expand All @@ -122,57 +50,119 @@ class AirshipDelegate: NSObject,
}
}

}

extension AirshipDelegate: PushNotificationDelegate {

@MainActor
private var forwardPushDelegate: PushNotificationDelegate? {
AirshipPluginForwardDelegates.shared.pushNotificationDelegate
}

func receivedNotificationResponse(
_ notificationResponse: UNNotificationResponse,
completionHandler: @escaping () -> Void
) {
guard
notificationResponse.actionIdentifier != UNNotificationDismissActionIdentifier
else {
completionHandler()
return
}

Task {
await self.eventEmitter.addEvent(
NotificationResponseEvent(
response: notificationResponse
Task { @MainActor in
if (notificationResponse.actionIdentifier != UNNotificationDismissActionIdentifier) {
await self.eventEmitter.addEvent(
NotificationResponseEvent(
response: notificationResponse
)
)
)
completionHandler()
}

guard
let forward = forwardPushDelegate?.receivedNotificationResponse
else {
completionHandler()
return
}
forward(notificationResponse, completionHandler)
}
}

func receivedBackgroundNotification(
_ userInfo: [AnyHashable : Any],
completionHandler: @escaping (UIBackgroundFetchResult
) -> Void) {
Task {
Task { @MainActor in
await self.eventEmitter.addEvent(
PushReceivedEvent(
userInfo: userInfo,
isForeground: false
)
)
completionHandler(.noData)

guard
let forward = forwardPushDelegate?.receivedBackgroundNotification
else {
completionHandler(.noData)
return
}
forward(userInfo, completionHandler)
}
}

func receivedForegroundNotification(
_ userInfo: [AnyHashable : Any],
completionHandler: @escaping () -> Void
) {
Task {
Task { @MainActor in
await self.eventEmitter.addEvent(
PushReceivedEvent(
userInfo: userInfo,
isForeground: true
)
)
completionHandler()

guard
let forward = forwardPushDelegate?.receivedForegroundNotification
else {
completionHandler()
return
}
forward(userInfo, completionHandler)
}
}

func extendPresentationOptions(
_ options: UNNotificationPresentationOptions,
notification: UNNotification,
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
Task { @MainActor in
guard
let forward = forwardPushDelegate?.extendPresentationOptions
else {
let overrides = await AirshipProxy.shared.push.presentationOptions(
notification: notification
)
completionHandler(overrides ?? options)
return
}

forward(options, notification, completionHandler)
}
}

@preconcurrency @MainActor
func extend(
_ options: UNNotificationPresentationOptions,
notification: UNNotification
) -> UNNotificationPresentationOptions {
return forwardPushDelegate?.extend?(options, notification: notification) ?? options
}
}


extension AirshipDelegate: RegistrationDelegate {
@MainActor
private var forwardRegistrationDelegate: RegistrationDelegate? {
AirshipPluginForwardDelegates.shared.registrationDelegate
}

@preconcurrency @MainActor
func apnsRegistrationSucceeded(withDeviceToken deviceToken: Data) {
let token = AirshipUtils.deviceTokenStringFromDeviceToken(deviceToken)
Task {
Expand All @@ -183,8 +173,16 @@ class AirshipDelegate: NSObject,
replacePending: true
)
}

forwardRegistrationDelegate?.apnsRegistrationSucceeded?(withDeviceToken: deviceToken)
}

@preconcurrency @MainActor
func apnsRegistrationFailedWithError(_ error: Error) {
forwardRegistrationDelegate?.apnsRegistrationFailedWithError?(error)
}

@preconcurrency @MainActor
func notificationAuthorizedSettingsDidChange(
_ authorizedSettings: UAAuthorizedNotificationSettings
) {
Expand All @@ -196,19 +194,80 @@ class AirshipDelegate: NSObject,
replacePending: true
)
}

forwardRegistrationDelegate?.notificationAuthorizedSettingsDidChange?(authorizedSettings)
}
}

extension AirshipDelegate: MessageCenterDisplayDelegate {
@MainActor
func extendPresentationOptions(
_ options: UNNotificationPresentationOptions,
notification: UNNotification,
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
func displayMessageCenter(messageID: String) {
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display(messageID: messageID)
return
}

Task {
let overrides = await AirshipProxy.shared.push.presentationOptions(
notification: notification
await self.eventEmitter.addEvent(
DisplayMessageCenterEvent(
messageID: messageID
)
)
completionHandler(overrides ?? options)
}
}

func displayMessageCenter() {
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display()
return
}

Task {
await self.eventEmitter.addEvent(
DisplayMessageCenterEvent()
)
}
}

func dismissMessageCenter() {
DefaultMessageCenterUI.shared.dismiss()
}
}

extension AirshipDelegate: PreferenceCenterOpenDelegate {
func openPreferenceCenter(
_ preferenceCenterID: String
) -> Bool {
let autoLaunch = self.proxyStore.shouldAutoLaunchPreferenceCenter(
preferenceCenterID
)

guard !autoLaunch else {
return false
}

Task {
await self.eventEmitter.addEvent(
DisplayPreferenceCenterEvent(
preferenceCenterID: preferenceCenterID
)
)
}

return true
}
}

extension AirshipDelegate: DeepLinkDelegate {
func receivedDeepLink(
_ deepLink: URL
) async {
let delegate = AirshipPluginForwardDelegates.shared.deepLinkDelegate
guard await delegate?.receivedDeepLink(deepLink) == true else {
await self.eventEmitter.addEvent(
DeepLinkEvent(deepLink)
)
return
}
}
}
Expand Down
Loading

0 comments on commit e6fb71d

Please sign in to comment.