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.
		
		
		
		
		
			
		
			
	
	
		
			243 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			243 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Matlab
		
	
| 
											8 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
| 
											8 years ago
										 | #import "OWSViewController.h" | ||
| 
											8 years ago
										 | #import "UIView+OWS.h" | ||
| 
											5 years ago
										 | #import <SignalUtilitiesKit/Theme.h> | ||
|  | #import "AppContext.h" | ||
| 
											8 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											7 years ago
										 | BOOL IsLandscapeOrientationEnabled(void) | ||
|  | { | ||
| 
											6 years ago
										 |     return NO; | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | UIInterfaceOrientationMask DefaultUIInterfaceOrientationMask(void) | ||
|  | { | ||
| 
											7 years ago
										 |     return (IsLandscapeOrientationEnabled() ? UIInterfaceOrientationMaskAllButUpsideDown | ||
|  |                                             : UIInterfaceOrientationMaskPortrait); | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | @interface OWSViewController () | ||
|  | 
 | ||
|  | @property (nonatomic, weak) UIView *bottomLayoutView; | ||
|  | @property (nonatomic) NSLayoutConstraint *bottomLayoutConstraint; | ||
| 
											7 years ago
										 | @property (nonatomic) BOOL shouldAnimateBottomLayout; | ||
| 
											8 years ago
										 | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
| 
											8 years ago
										 | @implementation OWSViewController | ||
|  | 
 | ||
|  | - (void)dealloc | ||
|  | { | ||
|  |     // Surface memory leaks by logging the deallocation of view controllers. | ||
| 
											7 years ago
										 |     OWSLogVerbose(@"Dealloc: %@", self.class); | ||
| 
											8 years ago
										 | 
 | ||
|  |     [[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil | ||
|  | { | ||
|  |     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | ||
|  |     if (!self) { | ||
| 
											7 years ago
										 |         self.shouldUseTheme = YES; | ||
| 
											7 years ago
										 |         return self; | ||
|  |     } | ||
|  | 
 | ||
|  |     [self observeActivation]; | ||
|  | 
 | ||
|  |     return self; | ||
|  | } | ||
|  | 
 | ||
|  | - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder | ||
|  | { | ||
|  |     self = [super initWithCoder:aDecoder]; | ||
|  |     if (!self) { | ||
| 
											7 years ago
										 |         self.shouldUseTheme = YES; | ||
| 
											7 years ago
										 |         return self; | ||
|  |     } | ||
|  | 
 | ||
|  |     [self observeActivation]; | ||
|  | 
 | ||
|  |     return self; | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | #pragma mark - View Lifecycle | ||
|  | 
 | ||
|  | - (void)viewDidAppear:(BOOL)animated | ||
|  | { | ||
|  |     [super viewDidAppear:animated]; | ||
|  | 
 | ||
| 
											7 years ago
										 |     self.shouldAnimateBottomLayout = YES; | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)viewDidDisappear:(BOOL)animated | ||
|  | { | ||
|  |     [super viewDidDisappear:animated]; | ||
|  | 
 | ||
| 
											7 years ago
										 |     self.shouldAnimateBottomLayout = NO; | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (void)viewDidLoad | ||
|  | { | ||
|  |     [super viewDidLoad]; | ||
|  | 
 | ||
|  |     if (self.shouldUseTheme) { | ||
|  |         self.view.backgroundColor = Theme.backgroundColor; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | #pragma mark - | ||
|  | 
 | ||
| 
											7 years ago
										 | - (void)autoPinViewToBottomOfViewControllerOrKeyboard:(UIView *)view avoidNotch:(BOOL)avoidNotch | ||
| 
											8 years ago
										 | { | ||
| 
											7 years ago
										 |     OWSAssertDebug(view); | ||
|  |     OWSAssertDebug(!self.bottomLayoutConstraint); | ||
| 
											8 years ago
										 | 
 | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardWillShow:) | ||
|  |                                                  name:UIKeyboardWillShowNotification | ||
|  |                                                object:nil]; | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardDidShow:) | ||
|  |                                                  name:UIKeyboardDidShowNotification | ||
|  |                                                object:nil]; | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardWillHide:) | ||
|  |                                                  name:UIKeyboardWillHideNotification | ||
|  |                                                object:nil]; | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardDidHide:) | ||
|  |                                                  name:UIKeyboardDidHideNotification | ||
|  |                                                object:nil]; | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardWillChangeFrame:) | ||
|  |                                                  name:UIKeyboardWillChangeFrameNotification | ||
|  |                                                object:nil]; | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(keyboardDidChangeFrame:) | ||
|  |                                                  name:UIKeyboardDidChangeFrameNotification | ||
|  |                                                object:nil]; | ||
|  | 
 | ||
|  |     self.bottomLayoutView = view; | ||
| 
											7 years ago
										 |     if (avoidNotch) { | ||
| 
											5 years ago
										 |         NSLayoutConstraint *bottomLayoutConstraint = [view.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor]; | ||
|  |         [bottomLayoutConstraint setActive:YES]; | ||
|  |         self.bottomLayoutConstraint = bottomLayoutConstraint; | ||
| 
											7 years ago
										 |     } else { | ||
|  |         self.bottomLayoutConstraint = [view autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view]; | ||
|  |     } | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (void)observeActivation | ||
|  | { | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(owsViewControllerApplicationDidBecomeActive:) | ||
|  |                                                  name:UIApplicationDidBecomeActiveNotification | ||
|  |                                                object:nil]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)owsViewControllerApplicationDidBecomeActive:(NSNotification *)notification | ||
|  | { | ||
|  |     [self setNeedsStatusBarAppearanceUpdate]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)keyboardWillShow:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)keyboardDidShow:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)keyboardWillHide:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)keyboardDidHide:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)keyboardWillChangeFrame:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)keyboardDidChangeFrame:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     [self handleKeyboardNotificationBase:notification]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | // We use the name `handleKeyboardNotificationBase` instead of | ||
|  | // `handleKeyboardNotification` to avoid accidentally | ||
|  | // calling similarly methods with that name in subclasses, | ||
|  | // e.g. ConversationViewController. | ||
|  | - (void)handleKeyboardNotificationBase:(NSNotification *)notification | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     OWSAssertIsOnMainThread(); | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     if (self.shouldIgnoreKeyboardChanges) { | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSDictionary *userInfo = [notification userInfo]; | ||
|  | 
 | ||
|  |     NSValue *_Nullable keyboardEndFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey]; | ||
|  |     if (!keyboardEndFrameValue) { | ||
| 
											7 years ago
										 |         OWSFailDebug(@"Missing keyboard end frame"); | ||
| 
											8 years ago
										 |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue]; | ||
|  |     CGRect keyboardEndFrameConverted = [self.view convertRect:keyboardEndFrame fromView:nil]; | ||
|  |     // Adjust the position of the bottom view to account for the keyboard's | ||
|  |     // intrusion into the view. | ||
| 
											8 years ago
										 |     // | ||
|  |     // On iPhoneX, when no keyboard is present, we include a buffer at the bottom of the screen so the bottom view | ||
|  |     // clears the floating "home button". But because the keyboard includes it's own buffer, we subtract the length | ||
|  |     // (height) of the bottomLayoutGuide, else we'd have an unnecessary buffer between the popped keyboard and the input | ||
|  |     // bar. | ||
|  |     CGFloat offset = -MAX(0, (self.view.height - self.bottomLayoutGuide.length - keyboardEndFrameConverted.origin.y)); | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     dispatch_block_t updateLayout = ^{ | ||
| 
											7 years ago
										 |         if (self.shouldBottomViewReserveSpaceForKeyboard && offset >= 0) { | ||
|  |             // To avoid unnecessary animations / layout jitter, | ||
|  |             // some views never reclaim layout space when the keyboard is dismissed. | ||
|  |             // | ||
|  |             // They _do_ need to relayout if the user switches keyboards. | ||
|  |             return; | ||
| 
											7 years ago
										 |         } | ||
| 
											7 years ago
										 |         self.bottomLayoutConstraint.constant = offset; | ||
| 
											7 years ago
										 |         [self.bottomLayoutView.superview layoutIfNeeded]; | ||
|  |     }; | ||
|  | 
 | ||
|  | 
 | ||
| 
											7 years ago
										 |     if (self.shouldAnimateBottomLayout && CurrentAppContext().isAppForegroundAndActive) { | ||
| 
											7 years ago
										 |         updateLayout(); | ||
|  |     } else { | ||
| 
											7 years ago
										 |         // UIKit by default animates all changes in response to keyboard events. | ||
|  |         // We want to suppress those animations if the view isn't visible, | ||
|  |         // otherwise presentation animations don't work properly. | ||
| 
											7 years ago
										 |         [UIView performWithoutAnimation:updateLayout]; | ||
|  |     } | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | #pragma mark - Orientation | ||
|  | 
 | ||
|  | - (UIInterfaceOrientationMask)supportedInterfaceOrientations | ||
|  | { | ||
| 
											7 years ago
										 |     return DefaultUIInterfaceOrientationMask(); | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |