Stop worrying about notification order by using delegate pattern

// FREEBIE
pull/1/head
Michael Kirk 7 years ago committed by Matthew Chen
parent d9d0227ce5
commit 1b60716759

@ -267,22 +267,6 @@ typedef enum : NSUInteger {
return self; return self;
} }
- (CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container
withParentContainerSize:(CGSize)parentSize NS_AVAILABLE_IOS(8_0);
{
CGSize result = [super sizeForChildContentContainer:container withParentContainerSize:parentSize];
DDLogDebug(@"%@ in %s result: %@", self.logTag, __PRETTY_FUNCTION__, NSStringFromCGSize(result));
return result;
}
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
DDLogDebug(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__);
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
- (void)commonInit - (void)commonInit
{ {
_contactsManager = [Environment current].contactsManager; _contactsManager = [Environment current].contactsManager;

@ -45,7 +45,6 @@ static double const STALLED_PROGRESS = 0.9;
} }
} }
- (void)dealloc - (void)dealloc
{ {
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];

@ -7,7 +7,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface OWSNavigationController (OWSNavigationController) <UINavigationBarDelegate> @interface OWSNavigationController (OWSNavigationController) <UINavigationBarDelegate, NavBarLayoutDelegate>
@end @end
@ -52,41 +52,53 @@ NS_ASSUME_NONNULL_BEGIN
self = [self initWithNavigationBarClass:[OWSNavigationBar class] toolbarClass:nil]; self = [self initWithNavigationBarClass:[OWSNavigationBar class] toolbarClass:nil];
[self pushViewController:rootViewController animated:NO]; [self pushViewController:rootViewController animated:NO];
[self updateNavbarCallBannerLayout]; if (![self.navigationBar isKindOfClass:[OWSNavigationBar class]]) {
OWSFail(@"%@ navigationBar was unexpected class: %@", self.logTag, self.navigationBar);
return self;
}
[[NSNotificationCenter defaultCenter] addObserver:self OWSNavigationBar *navbar = (OWSNavigationBar *)self.navigationBar;
selector:@selector(windowManagerCallDidChange:) navbar.navBarLayoutDelegate = self;
name:OWSWindowManagerCallDidChangeNotification [self updateLayoutForNavbar:navbar];
object:nil];
return self; return self;
} }
- (void)windowManagerCallDidChange:(NSNotification *)notification - (void)navBarCallLayoutDidChangeWithNavbar:(OWSNavigationBar *)navbar
{ {
DDLogDebug(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__); [self updateLayoutForNavbar:navbar];
[self updateNavbarCallBannerLayout];
} }
- (void)updateNavbarCallBannerLayout - (void)updateLayoutForNavbar:(OWSNavigationBar *)navbar
{ {
DDLogDebug(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__);
if (@available(iOS 11.0, *)) { if (@available(iOS 11.0, *)) {
if (OWSWindowManager.sharedManager.hasCall) { if (OWSWindowManager.sharedManager.hasCall) {
self.additionalSafeAreaInsets = UIEdgeInsetsMake(64, 0, 0, 0); self.additionalSafeAreaInsets = UIEdgeInsetsMake(64, 0, 0, 0);
} else { } else {
self.additionalSafeAreaInsets = UIEdgeInsetsZero; self.additionalSafeAreaInsets = UIEdgeInsetsZero;
} }
[navbar layoutSubviews];
} else { } else {
if (![self.navigationBar isKindOfClass:[OWSNavigationBar class]]) { // Pre iOS11 we have to position the frame manually
OWSFail(@"%@ in %s navigationBar was unexpected class", self.logTag, __PRETTY_FUNCTION__); [navbar sizeToFit];
return;
if (OWSWindowManager.sharedManager.hasCall) {
CGRect oldFrame = navbar.frame;
CGRect newFrame
= CGRectMake(oldFrame.origin.x, navbar.callBannerHeight, oldFrame.size.width, oldFrame.size.height);
navbar.frame = newFrame;
} else {
CGRect oldFrame = navbar.frame;
CGRect newFrame
= CGRectMake(oldFrame.origin.x, navbar.statusBarHeight, oldFrame.size.width, oldFrame.size.height);
navbar.frame = newFrame;
} }
OWSNavigationBar *navBar = (OWSNavigationBar *)self.navigationBar; // Since the navbar's frame was updated, we need to be sure our child VC's
CGRect oldFrame = navBar.frame; // container view is updated.
CGRect newFrame [self.view setNeedsLayout];
= CGRectMake(oldFrame.origin.x, navBar.statusBarHeight, oldFrame.size.width, oldFrame.size.height);
navBar.frame = newFrame;
} }
} }

@ -5,9 +5,16 @@
import Foundation import Foundation
import UIKit import UIKit
@objc
protocol NavBarLayoutDelegate: class {
func navBarCallLayoutDidChange(navbar: OWSNavigationBar)
}
@objc @objc
class OWSNavigationBar: UINavigationBar { class OWSNavigationBar: UINavigationBar {
weak var navBarLayoutDelegate: NavBarLayoutDelegate?
// TODO - get a more precise value // TODO - get a more precise value
// TODO - test with other heights, e.g. w/ hotspot, w/ call in other app // TODO - test with other heights, e.g. w/ hotspot, w/ call in other app
let navbarWithoutStatusHeight: CGFloat = 44 let navbarWithoutStatusHeight: CGFloat = 44
@ -29,16 +36,8 @@ class OWSNavigationBar: UINavigationBar {
@objc @objc
public func callDidChange() { public func callDidChange() {
Logger.debug("\(self.logTag) in \(#function) OWSWindowManagerCallDidChange") Logger.debug("\(self.logTag) in \(#function)")
self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self)
if #available(iOS 11, *) {
self.layoutSubviews()
} else {
self.sizeToFit()
self.frame.origin.y = statusBarHeight
self.layoutSubviews()
}
} }
required init?(coder aDecoder: NSCoder) { required init?(coder aDecoder: NSCoder) {
@ -53,9 +52,9 @@ class OWSNavigationBar: UINavigationBar {
if #available(iOS 11, *) { if #available(iOS 11, *) {
return super.sizeThatFits(size) return super.sizeThatFits(size)
} else { } else {
// pre iOS11, sizeThatFits is repeatedly called to size the navbar // pre iOS11, sizeThatFits is repeatedly called to determine how much space to reserve for that navbar.
// as of iOS11, this is not true and we have to size things in layoutSubviews. // That is, increasing this causes the child view controller to be pushed down.
// FIXME: pre-iOS11, though the size is right, there's a glitch on the titleView while push/popping items. // (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)
let result = CGSize(width: CurrentAppContext().mainWindow!.bounds.width, height: navbarWithoutStatusHeight + statusBarHeight) let result = CGSize(width: CurrentAppContext().mainWindow!.bounds.width, height: navbarWithoutStatusHeight + statusBarHeight)
Logger.debug("\(self.logTag) in \(#function): \(result)") Logger.debug("\(self.logTag) in \(#function): \(result)")
@ -64,52 +63,10 @@ class OWSNavigationBar: UINavigationBar {
} }
} }
// override var center: CGPoint {
// get {
// Logger.debug("\(self.logTag) in \(#function)")
// return super.center
// }
// set {
// Logger.debug("\(self.logTag) in \(#function)")
// if OWSWindowManager.shared().hasCall() {
// var translated = newValue
//// translated.y -= 20
// super.center = translated
// } else {
// super.center = newValue
// }
// }
// }
// seems unused.
// override var intrinsicContentSize: CGSize {
// return CGSize(width: UIScreen.main.bounds.width, height: navbarWithoutStatusHeight)
// return CGSize(width: UIScreen.main.bounds.width, height: 20)
// }
// override var bounds: CGRect {
// get {
// return super.bounds
// }
// set {
// super.bounds = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: ios11NavbarHeight)
// }
// }
//
// override var frame: CGRect {
// get {
// return super.frame
// }
// set {
// super.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: ios11NavbarHeight)
// }
// }
override func layoutSubviews() { override func layoutSubviews() {
Logger.debug("\(self.logTag) in \(#function)") Logger.debug("\(self.logTag) in \(#function)")
guard OWSWindowManager.shared().hasCall() else { guard OWSWindowManager.shared().hasCall() else {
// guard #available(iOS 11.0, *), OWSWindowManager.shared().hasCall() else {
super.layoutSubviews() super.layoutSubviews()
return return
} }
@ -121,6 +78,11 @@ class OWSNavigationBar: UINavigationBar {
super.layoutSubviews() super.layoutSubviews()
guard #available(iOS 11, *) else {
return
}
// This is only necessary on iOS11, which has some private views within the navbar
for subview in self.subviews { for subview in self.subviews {
let stringFromClass = NSStringFromClass(subview.classForCoder) let stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("BarBackground") { if stringFromClass.contains("BarBackground") {

Loading…
Cancel
Save