session-ios/SignalMessaging/ViewControllers/OWSViewController.m

179 lines
5.8 KiB
Mathematica
Raw Normal View History

//
2018-02-28 16:55:33 +01:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2018-04-02 23:00:55 +02:00
#import "OWSViewController.h"
2017-10-10 22:13:54 +02:00
#import "UIView+OWS.h"
2018-07-23 21:03:07 +02:00
#import <SignalMessaging/Theme.h>
NS_ASSUME_NONNULL_BEGIN
2017-10-10 22:13:54 +02:00
@interface OWSViewController ()
@property (nonatomic, weak) UIView *bottomLayoutView;
@property (nonatomic) NSLayoutConstraint *bottomLayoutConstraint;
@end
#pragma mark -
@implementation OWSViewController
- (void)dealloc
{
// Surface memory leaks by logging the deallocation of view controllers.
DDLogVerbose(@"Dealloc: %@", self.class);
2017-10-10 22:13:54 +02:00
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2018-07-16 18:53:10 +02:00
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) {
2018-07-23 21:03:07 +02:00
self.shouldUseTheme = YES;
2018-07-16 18:53:10 +02:00
return self;
}
[self observeActivation];
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) {
2018-07-23 21:03:07 +02:00
self.shouldUseTheme = YES;
2018-07-16 18:53:10 +02:00
return self;
}
[self observeActivation];
return self;
}
2018-07-23 21:03:07 +02:00
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.shouldUseTheme) {
self.view.backgroundColor = Theme.backgroundColor;
}
}
2018-04-02 23:00:55 +02:00
- (void)autoPinViewToBottomOfViewControllerOrKeyboard:(UIView *)view
2017-10-10 22:13:54 +02:00
{
OWSAssert(view);
OWSAssert(!self.bottomLayoutConstraint);
[[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;
2018-08-07 23:12:16 +02:00
self.bottomLayoutConstraint = [view autoPinToBottomLayoutGuideOfViewController:self withInset:0.f];
2017-10-10 22:13:54 +02:00
}
2018-07-16 18:53:10 +02:00
- (void)observeActivation
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(owsViewControllerApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)owsViewControllerApplicationDidBecomeActive:(NSNotification *)notification
{
[self setNeedsStatusBarAppearanceUpdate];
}
2017-10-10 22:13:54 +02:00
- (void)keyboardWillShow:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)keyboardDidHide:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)keyboardDidChangeFrame:(NSNotification *)notification
{
[self handleKeyboardNotification:notification];
}
- (void)handleKeyboardNotification:(NSNotification *)notification
{
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
2017-10-10 22:13:54 +02:00
if (self.shouldIgnoreKeyboardChanges) {
return;
}
2017-10-10 22:13:54 +02:00
NSDictionary *userInfo = [notification userInfo];
NSValue *_Nullable keyboardEndFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey];
if (!keyboardEndFrameValue) {
2017-11-08 20:04:51 +01:00
OWSFail(@"%@ Missing keyboard end frame", self.logTag);
2017-10-10 22:13:54 +02:00
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.
2017-11-14 15:13:04 +01:00
//
// 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));
2017-10-10 22:13:54 +02:00
// There's no need to use: [UIView animateWithDuration:...].
// Any layout changes made during these notifications are
// automatically animated.
self.bottomLayoutConstraint.constant = offset;
[self.bottomLayoutView.superview layoutIfNeeded];
}
@end
NS_ASSUME_NONNULL_END