From a6a7616fdb54161959a38e949488dd83f1f0b5ab Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 30 Jan 2019 17:09:45 -0700 Subject: [PATCH] move notification action handlers to environment --- Signal/src/AppDelegate.m | 32 ++++++++++++------- .../LegacyNotificationsAdaptee.swift | 6 +--- .../UserNotificationsAdaptee.swift | 6 +--- Signal/src/environment/AppEnvironment.swift | 28 ++++++++++++++++ 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 9fe83a4ef..ca35cfd8b 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -179,6 +179,16 @@ static NSTimeInterval launchStartedAt; return AppEnvironment.shared.notificationPresenter; } +- (OWSUserNotificationActionHandler *)userNotificationActionHandler +{ + return AppEnvironment.shared.userNotificationActionHandler; +} + +- (OWSLegacyNotificationActionHandler *)legacyNotificationActionHandler +{ + return AppEnvironment.shared.legacyNotificationActionHandler; +} + #pragma mark - - (void)applicationDidEnterBackground:(UIApplication *)application { @@ -1159,8 +1169,8 @@ static NSTimeInterval launchStartedAt; return; } - [LegacyNotificationActionHandler.shared - handleNotificationResponseWithActionIdentifier:LegacyNotificationActionHandler.kDefaultActionIdentifier + [self.legacyNotificationActionHandler + handleNotificationResponseWithActionIdentifier:OWSLegacyNotificationActionHandler.kDefaultActionIdentifier notification:notification responseInfo:@{} completionHandler:^{ @@ -1194,10 +1204,10 @@ static NSTimeInterval launchStartedAt; return; } - [LegacyNotificationActionHandler.shared handleNotificationResponseWithActionIdentifier:identifier - notification:notification - responseInfo:@{} - completionHandler:completionHandler]; + [self.legacyNotificationActionHandler handleNotificationResponseWithActionIdentifier:identifier + notification:notification + responseInfo:@{} + completionHandler:completionHandler]; }]; } @@ -1230,10 +1240,10 @@ static NSTimeInterval launchStartedAt; return; } - [LegacyNotificationActionHandler.shared handleNotificationResponseWithActionIdentifier:identifier - notification:notification - responseInfo:responseInfo - completionHandler:completionHandler]; + [self.legacyNotificationActionHandler handleNotificationResponseWithActionIdentifier:identifier + notification:notification + responseInfo:responseInfo + completionHandler:completionHandler]; }]; } @@ -1529,7 +1539,7 @@ static NSTimeInterval launchStartedAt; { OWSLogInfo(@""); [AppReadiness runNowOrWhenAppDidBecomeReady:^() { - [UserNotificationActionHandler.shared handleNotificationResponse:response completionHandler:completionHandler]; + [self.userNotificationActionHandler handleNotificationResponse:response completionHandler:completionHandler]; }]; } diff --git a/Signal/src/UserInterface/Notifications/LegacyNotificationsAdaptee.swift b/Signal/src/UserInterface/Notifications/LegacyNotificationsAdaptee.swift index 205086e79..0d66667d6 100644 --- a/Signal/src/UserInterface/Notifications/LegacyNotificationsAdaptee.swift +++ b/Signal/src/UserInterface/Notifications/LegacyNotificationsAdaptee.swift @@ -217,16 +217,12 @@ extension LegacyNotificationPresenterAdaptee: NotificationPresenterAdaptee { } } -@objc +@objc(OWSLegacyNotificationActionHandler) public class LegacyNotificationActionHandler: NSObject { @objc public static let kDefaultActionIdentifier = "LegacyNotificationActionHandler.kDefaultActionIdentifier" - // TODO move this to environment? - @objc - static let shared: LegacyNotificationActionHandler = LegacyNotificationActionHandler() - var actionHandler: NotificationActionHandler { return NotificationActionHandler.shared } diff --git a/Signal/src/UserInterface/Notifications/UserNotificationsAdaptee.swift b/Signal/src/UserInterface/Notifications/UserNotificationsAdaptee.swift index 74ded3168..e49f20f8b 100644 --- a/Signal/src/UserInterface/Notifications/UserNotificationsAdaptee.swift +++ b/Signal/src/UserInterface/Notifications/UserNotificationsAdaptee.swift @@ -202,14 +202,10 @@ extension UserNotificationPresenterAdaptee: NotificationPresenterAdaptee { } } -@objc +@objc(OWSUserNotificationActionHandler) @available(iOS 10.0, *) public class UserNotificationActionHandler: NSObject { - // TODO move this to environment? - @objc - static let shared: UserNotificationActionHandler = UserNotificationActionHandler() - var actionHandler: NotificationActionHandler { return NotificationActionHandler.shared } diff --git a/Signal/src/environment/AppEnvironment.swift b/Signal/src/environment/AppEnvironment.swift index b6acf63d1..1febedb20 100644 --- a/Signal/src/environment/AppEnvironment.swift +++ b/Signal/src/environment/AppEnvironment.swift @@ -52,6 +52,30 @@ import SignalMessaging @objc public var backup: OWSBackup + private var _legacyNotificationActionHandler: LegacyNotificationActionHandler + @objc + public var legacyNotificationActionHandler: LegacyNotificationActionHandler { + get { + if #available(iOS 10, *) { + owsFailDebug("shouldn't user legacyNotificationActionHandler on modern iOS") + } + return _legacyNotificationActionHandler + } + set { + _legacyNotificationActionHandler = newValue + } + } + + // Stored properties cannot be marked as `@available`, only classes and functions. + // Instead, store a private `Any` and wrap it with a public `@available` getter + private var _userNotificationActionHandler: Any? + + @objc + @available(iOS 10.0, *) + public var userNotificationActionHandler: UserNotificationActionHandler { + return _userNotificationActionHandler as! UserNotificationActionHandler + } + @objc public var backupLazyRestore: BackupLazyRestore @@ -66,6 +90,10 @@ import SignalMessaging self.sessionResetJobQueue = SessionResetJobQueue() self.backup = OWSBackup() self.backupLazyRestore = BackupLazyRestore() + if #available(iOS 10.0, *) { + self._userNotificationActionHandler = UserNotificationActionHandler() + } + self._legacyNotificationActionHandler = LegacyNotificationActionHandler() super.init()