mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			143 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			143 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
|  | 
 | ||
| 
											5 years ago
										 | #import <UIKit/UIKit.h> | ||
|  | 
 | ||
| 
											7 years ago
										 | #import "Theme.h" | ||
| 
											7 years ago
										 | #import "UIColor+OWS.h" | ||
| 
											7 years ago
										 | #import "UIUtil.h" | ||
| 
											7 years ago
										 | #import "UIView+OWS.h" | ||
| 
											7 years ago
										 | #import "UIViewController+OWS.h" | ||
| 
											5 years ago
										 | #import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h> | ||
|  | 
 | ||
| 
											5 years ago
										 | #import <SessionUtilitiesKit/AppContext.h> | ||
| 
											9 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
|  | @implementation UIViewController (OWS) | ||
|  | 
 | ||
| 
											8 years ago
										 | - (UIViewController *)findFrontmostViewController:(BOOL)ignoringAlerts | ||
|  | { | ||
| 
											7 years ago
										 |     NSMutableArray<UIViewController *> *visitedViewControllers = [NSMutableArray new]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     UIViewController *viewController = self; | ||
|  |     while (YES) { | ||
| 
											7 years ago
										 |         [visitedViewControllers addObject:viewController]; | ||
|  | 
 | ||
| 
											8 years ago
										 |         UIViewController *_Nullable nextViewController = viewController.presentedViewController; | ||
|  |         if (nextViewController) { | ||
| 
											7 years ago
										 |             if (!ignoringAlerts || ![nextViewController isKindOfClass:[UIAlertController class]]) { | ||
| 
											7 years ago
										 |                 if ([visitedViewControllers containsObject:nextViewController]) { | ||
|  |                     // Cycle detected. | ||
|  |                     return viewController; | ||
|  |                 } | ||
| 
											7 years ago
										 |                 viewController = nextViewController; | ||
|  |                 continue; | ||
| 
											8 years ago
										 |             } | ||
| 
											7 years ago
										 |         } | ||
|  | 
 | ||
|  |         if ([viewController isKindOfClass:[UINavigationController class]]) { | ||
| 
											8 years ago
										 |             UINavigationController *navigationController = (UINavigationController *)viewController; | ||
| 
											7 years ago
										 |             nextViewController = navigationController.topViewController; | ||
|  |             if (nextViewController) { | ||
|  |                 if ([visitedViewControllers containsObject:nextViewController]) { | ||
|  |                     // Cycle detected. | ||
|  |                     return viewController; | ||
|  |                 } | ||
|  |                 viewController = nextViewController; | ||
| 
											8 years ago
										 |             } else { | ||
|  |                 break; | ||
|  |             } | ||
|  |         } else { | ||
|  |             break; | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     return viewController; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (UIBarButtonItem *)createOWSBackButton | ||
|  | { | ||
| 
											9 years ago
										 |     return [self createOWSBackButtonWithTarget:self selector:@selector(backButtonPressed:)]; | ||
|  | } | ||
|  | 
 | ||
|  | - (UIBarButtonItem *)createOWSBackButtonWithTarget:(id)target selector:(SEL)selector | ||
| 
											8 years ago
										 | { | ||
|  |     return [[self class] createOWSBackButtonWithTarget:target selector:selector]; | ||
|  | } | ||
|  | 
 | ||
|  | + (UIBarButtonItem *)createOWSBackButtonWithTarget:(id)target selector:(SEL)selector | ||
| 
											9 years ago
										 | { | ||
| 
											7 years ago
										 |     OWSAssertDebug(target); | ||
|  |     OWSAssertDebug(selector); | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 |     UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; | ||
| 
											7 years ago
										 |     BOOL isRTL = CurrentAppContext().isRTL; | ||
| 
											8 years ago
										 | 
 | ||
| 
											9 years ago
										 |     // Nudge closer to the left edge to match default back button item. | ||
| 
											8 years ago
										 |     const CGFloat kExtraLeftPadding = isRTL ? +0 : -8; | ||
| 
											9 years ago
										 | 
 | ||
|  |     // Give some extra hit area to the back button. This is a little smaller | ||
|  |     // than the default back button, but makes sense for our left aligned title | ||
|  |     // view in the MessagesViewController | ||
| 
											8 years ago
										 |     const CGFloat kExtraRightPadding = isRTL ? -0 : +10; | ||
| 
											9 years ago
										 | 
 | ||
|  |     // Extra hit area above/below | ||
|  |     const CGFloat kExtraHeightPadding = 4; | ||
|  | 
 | ||
|  |     // Matching the default backbutton placement is tricky. | ||
|  |     // We can't just adjust the imageEdgeInsets on a UIBarButtonItem directly, | ||
|  |     // so we adjust the imageEdgeInsets on a UIButton, then wrap that | ||
|  |     // in a UIBarButtonItem. | ||
| 
											9 years ago
										 |     [backButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; | ||
| 
											9 years ago
										 | 
 | ||
| 
											7 years ago
										 |     UIImage *backImage = [[UIImage imageNamed:(isRTL ? @"NavBarBackRTL" : @"NavBarBack")] | ||
|  |         imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; | ||
| 
											7 years ago
										 |     OWSAssertDebug(backImage); | ||
| 
											9 years ago
										 |     [backButton setImage:backImage forState:UIControlStateNormal]; | ||
| 
											7 years ago
										 |     backButton.tintColor = Theme.navbarIconColor; | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 |     backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; | ||
|  | 
 | ||
|  |     // Default back button is 1.5 pixel lower than our extracted image. | ||
| 
											9 years ago
										 |     const CGFloat kTopInsetPadding = 1.5; | ||
| 
											9 years ago
										 |     backButton.imageEdgeInsets = UIEdgeInsetsMake(kTopInsetPadding, kExtraLeftPadding, 0, 0); | ||
|  | 
 | ||
| 
											8 years ago
										 |     CGRect buttonFrame | ||
|  |         = CGRectMake(0, 0, backImage.size.width + kExtraRightPadding, backImage.size.height + kExtraHeightPadding); | ||
|  |     backButton.frame = buttonFrame; | ||
| 
											9 years ago
										 | 
 | ||
| 
											5 years ago
										 |     // In iOS 11.1 beta, the hot area of custom bar button items is _only_ | ||
|  |     // the bounds of the custom view, making them very hard to hit. | ||
|  |     // | ||
|  |     // TODO: Remove this hack if the bug is fixed in iOS 11.1 by the time | ||
|  |     //       it goes to production (or in a later release), | ||
|  |     //       since it has two negative side effects: 1) the layout of the | ||
|  |     //       back button isn't consistent with the iOS default back buttons | ||
|  |     //       2) we can't add the unread count badge to the back button | ||
|  |     //       with this hack. | ||
|  |     return [[UIBarButtonItem alloc] initWithImage:backImage | ||
|  |                                             style:UIBarButtonItemStylePlain | ||
|  |                                            target:target | ||
|  |                                            action:selector]; | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     UIBarButtonItem *backItem = | ||
|  |         [[UIBarButtonItem alloc] initWithCustomView:backButton | ||
|  |                             accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"back")]; | ||
| 
											8 years ago
										 |     backItem.width = buttonFrame.size.width; | ||
| 
											9 years ago
										 | 
 | ||
|  |     return backItem; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | #pragma mark - Event Handling | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)backButtonPressed:(id)sender | ||
|  | { | ||
| 
											9 years ago
										 |     [self.navigationController popViewControllerAnimated:YES]; | ||
|  | } | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |