From a16058e4778ac77ddb5dc705286e7dd6dc1482cc Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 1 Dec 2017 17:22:40 -0500 Subject: [PATCH] Migrate environment to SignalMessaging. --- Signal.xcodeproj/project.pbxproj | 14 +++++---- Signal/src/UIApplication+OWS.swift | 22 +------------- Signal/src/util/MainAppContext.m | 10 +++++-- .../categories}/UIViewController+OWS.h | 4 ++- .../categories}/UIViewController+OWS.m | 30 ++++++++++++++++++- SignalMessaging/views/OWSAlerts.swift | 8 ++--- SignalServiceKit/src/Util/AppContext.h | 5 +++- .../utils/ShareAppExtensionContext.m | 12 ++++---- 8 files changed, 62 insertions(+), 43 deletions(-) rename {Signal/src/util => SignalMessaging/categories}/UIViewController+OWS.h (83%) rename {Signal/src/util => SignalMessaging/categories}/UIViewController+OWS.m (76%) diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index dd596016b..41654b539 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -38,7 +38,6 @@ 34480B641FD0A98800BC14EF /* UIView+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 34480B601FD0A98800BC14EF /* UIView+OWS.m */; }; 34480B671FD0AA9400BC14EF /* UIFont+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 34480B651FD0AA9400BC14EF /* UIFont+OWS.m */; }; 34480B681FD0AA9400BC14EF /* UIFont+OWS.h in Headers */ = {isa = PBXBuildFile; fileRef = 34480B661FD0AA9400BC14EF /* UIFont+OWS.h */; }; - 344F2F671E57A932000D9322 /* UIViewController+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 344F2F661E57A932000D9322 /* UIViewController+OWS.m */; }; 34533F181EA8D2070006114F /* OWSAudioAttachmentPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 34533F171EA8D2070006114F /* OWSAudioAttachmentPlayer.m */; }; 3461284B1FD0B94000532771 /* SAELoadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3461284A1FD0B93F00532771 /* SAELoadViewController.swift */; }; 346129321FD1A26100532771 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 346129051FD1A25B00532771 /* Localizable.strings */; }; @@ -89,6 +88,8 @@ 346129CD1FD2072E00532771 /* UIImage+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 346129C61FD2072D00532771 /* UIImage+OWS.m */; }; 346129D01FD207F300532771 /* OWSAlerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346129CF1FD207F200532771 /* OWSAlerts.swift */; }; 346129D21FD2085A00532771 /* CommonStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346129D11FD2085A00532771 /* CommonStrings.swift */; }; + 346129D51FD20ADC00532771 /* UIViewController+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 346129D31FD20ADB00532771 /* UIViewController+OWS.m */; }; + 346129D61FD20ADC00532771 /* UIViewController+OWS.h in Headers */ = {isa = PBXBuildFile; fileRef = 346129D41FD20ADC00532771 /* UIViewController+OWS.h */; settings = {ATTRIBUTES = (Public, ); }; }; 346B66311F4E29B200E5122F /* CropScaleImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346B66301F4E29B200E5122F /* CropScaleImageViewController.swift */; }; 3471B1DA1EB7C63600F6AEC8 /* NewNonContactConversationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3471B1D91EB7C63600F6AEC8 /* NewNonContactConversationViewController.m */; }; 3472229F1EB22FFE00E53955 /* AddToGroupViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3472229E1EB22FFE00E53955 /* AddToGroupViewController.m */; }; @@ -522,8 +523,6 @@ 34480B651FD0AA9400BC14EF /* UIFont+OWS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIFont+OWS.m"; sourceTree = ""; }; 34480B661FD0AA9400BC14EF /* UIFont+OWS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIFont+OWS.h"; sourceTree = ""; }; 34491FC11FB0F78500B3E5A3 /* my */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = my; path = translations/my.lproj/Localizable.strings; sourceTree = ""; }; - 344F2F651E57A932000D9322 /* UIViewController+OWS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIViewController+OWS.h"; path = "util/UIViewController+OWS.h"; sourceTree = ""; }; - 344F2F661E57A932000D9322 /* UIViewController+OWS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIViewController+OWS.m"; path = "util/UIViewController+OWS.m"; sourceTree = ""; }; 34533F161EA8D2070006114F /* OWSAudioAttachmentPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSAudioAttachmentPlayer.h; sourceTree = ""; }; 34533F171EA8D2070006114F /* OWSAudioAttachmentPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSAudioAttachmentPlayer.m; sourceTree = ""; }; 3461284A1FD0B93F00532771 /* SAELoadViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SAELoadViewController.swift; sourceTree = ""; }; @@ -626,6 +625,8 @@ 346129C61FD2072D00532771 /* UIImage+OWS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+OWS.m"; sourceTree = ""; }; 346129CF1FD207F200532771 /* OWSAlerts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OWSAlerts.swift; sourceTree = ""; }; 346129D11FD2085A00532771 /* CommonStrings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommonStrings.swift; sourceTree = ""; }; + 346129D31FD20ADB00532771 /* UIViewController+OWS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+OWS.m"; sourceTree = ""; }; + 346129D41FD20ADC00532771 /* UIViewController+OWS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+OWS.h"; sourceTree = ""; }; 346B66301F4E29B200E5122F /* CropScaleImageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropScaleImageViewController.swift; sourceTree = ""; }; 3471B1D81EB7C63600F6AEC8 /* NewNonContactConversationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewNonContactConversationViewController.h; sourceTree = ""; }; 3471B1D91EB7C63600F6AEC8 /* NewNonContactConversationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewNonContactConversationViewController.m; sourceTree = ""; }; @@ -1189,6 +1190,8 @@ 346129C61FD2072D00532771 /* UIImage+OWS.m */, 34480B5F1FD0A98800BC14EF /* UIView+OWS.h */, 34480B601FD0A98800BC14EF /* UIView+OWS.m */, + 346129D41FD20ADC00532771 /* UIViewController+OWS.h */, + 346129D31FD20ADB00532771 /* UIViewController+OWS.m */, ); path = categories; sourceTree = ""; @@ -2071,8 +2074,6 @@ 45C0DC1A1E68FE9000E04C47 /* UIApplication+OWS.swift */, 45BB93371E688E14001E3939 /* UIDevice+featureSupport.swift */, 45C0DC1D1E69011F00E04C47 /* UIStoryboard+OWS.swift */, - 344F2F651E57A932000D9322 /* UIViewController+OWS.h */, - 344F2F661E57A932000D9322 /* UIViewController+OWS.m */, EF764C331DB67CC5000D9A87 /* UIViewController+Permissions.h */, EF764C341DB67CC5000D9A87 /* UIViewController+Permissions.m */, ); @@ -2094,6 +2095,7 @@ 346129CA1FD2072E00532771 /* UIImage+OWS.h in Headers */, 346129711FD1D74C00532771 /* SignalKeyingStorage.h in Headers */, 346129C71FD2072E00532771 /* NSString+OWS.h in Headers */, + 346129D61FD20ADC00532771 /* UIViewController+OWS.h in Headers */, 3461296F1FD1D74C00532771 /* Release.h in Headers */, 34480B571FD0A7A400BC14EF /* OWSScrubbingLogFormatter.h in Headers */, 346129951FD1E30000532771 /* OWSDatabaseMigration.h in Headers */, @@ -2752,6 +2754,7 @@ 346129B61FD1F7E800532771 /* ProfileFetcherJob.swift in Sources */, 346129A01FD1EFE200532771 /* CryptoTools.m in Sources */, 3461295B1FD1D74C00532771 /* Environment.m in Sources */, + 346129D51FD20ADC00532771 /* UIViewController+OWS.m in Sources */, 346129C91FD2072E00532771 /* NSString+OWS.m in Sources */, 346129CB1FD2072E00532771 /* Promise+retainUntilComplete.swift in Sources */, 346129A61FD1F09100532771 /* OWSContactsManager.m in Sources */, @@ -2784,7 +2787,6 @@ 341F2C0F1F2B8AE700D07D6B /* DebugUIMisc.m in Sources */, 34E3EF0D1EFC235B007F6822 /* DebugUIDiskUsage.m in Sources */, 34D1F0871F8678AA0066283D /* ConversationViewItem.m in Sources */, - 344F2F671E57A932000D9322 /* UIViewController+OWS.m in Sources */, B6DA6B071B8A2F9A00CA6F98 /* AppStoreRating.m in Sources */, 451A13B11E13DED2000A50FD /* CallNotificationsAdapter.swift in Sources */, 450DF2091E0DD2C6003D14BE /* UserNotificationsAdaptee.swift in Sources */, diff --git a/Signal/src/UIApplication+OWS.swift b/Signal/src/UIApplication+OWS.swift index e7f66ff91..3e00699af 100644 --- a/Signal/src/UIApplication+OWS.swift +++ b/Signal/src/UIApplication+OWS.swift @@ -17,27 +17,7 @@ extension UIApplication { internal func findFrontmostViewController(ignoringAlerts: Bool) -> UIViewController? { let window = UIApplication.shared.keyWindow var viewController = window!.rootViewController - - while true { - if let nextViewController = viewController?.presentedViewController { - if ignoringAlerts { - if nextViewController as? UIAlertController != nil { - break - } - } - viewController = nextViewController - } else if let navigationController = viewController as? UINavigationController { - if let nextViewController = navigationController.topViewController { - viewController = nextViewController - } else { - break - } - } else { - break - } - } - - return viewController + return viewController.findFrontmostViewController(ignoringAlerts:ignoringAlerts) } func openSystemSettings() { diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index 9f42f689f..eccf1e995 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -8,6 +8,7 @@ #import "OWS103EnableVideoCalling.h" #import "OWS104CreateRecipientIdentities.h" #import "OWS105AttachmentFilePaths.h" +#import "Signal-Swift.h" NS_ASSUME_NONNULL_BEGIN @@ -62,9 +63,14 @@ NS_ASSUME_NONNULL_BEGIN ]; } -- (UIViewController *)frontmostViewController +- (nullable UIViewController *)frontmostViewController { - return UIApplication.sharedApplication.frontmostViewController; + return UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts; +} + +- (void)openSystemSettings +{ + [UIApplication.sharedApplication openSystemSettings]; } @end diff --git a/Signal/src/util/UIViewController+OWS.h b/SignalMessaging/categories/UIViewController+OWS.h similarity index 83% rename from Signal/src/util/UIViewController+OWS.h rename to SignalMessaging/categories/UIViewController+OWS.h index ac00b42e9..9a5622870 100644 --- a/Signal/src/util/UIViewController+OWS.h +++ b/SignalMessaging/categories/UIViewController+OWS.h @@ -8,11 +8,13 @@ NS_ASSUME_NONNULL_BEGIN @interface UIViewController (OWS) +- (UIViewController *)findFrontmostViewController:(BOOL)ignoringAlerts; + /** * Takes up a bit less space than the default system back button * used in the MessagesViewController to help left-align the title view. * - * **note** Using this breaks the interactive pop gesture (swipe back) unless you set/unset the + * **note** Using this breaks the interactive pop gesture (swipe back) unless you set/unset the * interactivePopGesture.delegate to self/nil on viewWillAppear/Disappear */ - (UIBarButtonItem *)createOWSBackButton; diff --git a/Signal/src/util/UIViewController+OWS.m b/SignalMessaging/categories/UIViewController+OWS.m similarity index 76% rename from Signal/src/util/UIViewController+OWS.m rename to SignalMessaging/categories/UIViewController+OWS.m index 726654544..6cd9a7b6a 100644 --- a/Signal/src/util/UIViewController+OWS.m +++ b/SignalMessaging/categories/UIViewController+OWS.m @@ -9,6 +9,33 @@ NS_ASSUME_NONNULL_BEGIN @implementation UIViewController (OWS) +- (UIViewController *)findFrontmostViewController:(BOOL)ignoringAlerts +{ + UIViewController *viewController = self; + while (YES) { + UIViewController *_Nullable nextViewController = viewController.presentedViewController; + if (nextViewController) { + if (ignoringAlerts) { + if ([nextViewController isKindOfClass:[UIAlertController class]]) { + break; + } + } + viewController = nextViewController; + } else if ([viewController isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = (UINavigationController *)viewController; + if (navigationController.topViewController) { + viewController = navigationController.topViewController; + } else { + break; + } + } else { + break; + } + } + + return viewController; +} + - (UIBarButtonItem *)createOWSBackButton { return [self createOWSBackButtonWithTarget:self selector:@selector(backButtonPressed:)]; @@ -77,7 +104,8 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Event Handling -- (void)backButtonPressed:(id)sender { +- (void)backButtonPressed:(id)sender +{ [self.navigationController popViewControllerAnimated:YES]; } diff --git a/SignalMessaging/views/OWSAlerts.swift b/SignalMessaging/views/OWSAlerts.swift index 1b46de176..9bf94e289 100644 --- a/SignalMessaging/views/OWSAlerts.swift +++ b/SignalMessaging/views/OWSAlerts.swift @@ -15,11 +15,11 @@ import Foundation let dismissAction = UIAlertAction(title: CommonStrings.dismissButton, style: .cancel) let settingsString = NSLocalizedString("OPEN_SETTINGS_BUTTON", comment: "Button text which opens the settings app") let settingsAction = UIAlertAction(title: settingsString, style: .default) { _ in - UIApplication.shared.openSystemSettings() + CurrentAppContext().openSystemSettings() } alertController.addAction(dismissAction) alertController.addAction(settingsAction) - UIApplication.shared.frontmostViewController?.present(alertController, animated: true, completion: nil) + CurrentAppContext().frontmostViewController()?.present(alertController, animated: true, completion: nil) } public class func showAlert(withTitle title: String) { @@ -37,7 +37,7 @@ import Foundation let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) alert.addAction(UIAlertAction(title: actionTitle, style: .default, handler: nil)) - UIApplication.shared.frontmostViewController?.present(alert, animated: true, completion: nil) + CurrentAppContext().frontmostViewController()?.present(alert, animated: true, completion: nil) } public class func showConfirmationAlert(withTitle title: String, message: String? = nil, proceedTitle: String? = nil, proceedAction: @escaping (UIAlertAction) -> Void) { @@ -49,7 +49,7 @@ import Foundation let actionTitle = proceedTitle ?? NSLocalizedString("OK", comment: "") alert.addAction(UIAlertAction(title: actionTitle, style: .default, handler: proceedAction)) - UIApplication.shared.frontmostViewController?.present(alert, animated: true, completion: nil) + CurrentAppContext().frontmostViewController()?.present(alert, animated: true, completion: nil) } public class var cancelAction: UIAlertAction { diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h index b31c8d5bb..8b94a4111 100755 --- a/SignalServiceKit/src/Util/AppContext.h +++ b/SignalServiceKit/src/Util/AppContext.h @@ -33,7 +33,10 @@ typedef void (^BackgroundTaskExpirationHandler)(void); - (NSArray *)allMigrations; // Returns the VC that should be used to present alerts, modals, etc. -- (UIViewController *)frontmostViewController; +- (nullable UIViewController *)frontmostViewController; + +// Should only be called if isMainApp is YES. +- (void)openSystemSettings; @end diff --git a/SignalShareExtension/utils/ShareAppExtensionContext.m b/SignalShareExtension/utils/ShareAppExtensionContext.m index 195d94e30..c6797b858 100644 --- a/SignalShareExtension/utils/ShareAppExtensionContext.m +++ b/SignalShareExtension/utils/ShareAppExtensionContext.m @@ -3,6 +3,7 @@ // #import "ShareAppExtensionContext.h" +#import NS_ASSUME_NONNULL_BEGIN @@ -79,19 +80,16 @@ NS_ASSUME_NONNULL_BEGIN return @[]; } -- (UIViewController *)frontmostViewController +- (nullable UIViewController *)frontmostViewController { OWSAssert(self.rootViewController); - return self.rootViewController; + return [self.rootViewController findFrontmostViewController:YES]; } -- (void)setRootViewController:(UIViewController *)viewController +- (void)openSystemSettings { - OWSAssert(!self.rootViewController); - OWSAssert(viewController); - - self.rootViewController = viewController; + OWSFail(@"%@ called %s.", self.logTag, __PRETTY_FUNCTION__); } @end