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.
		
		
		
		
		
			
		
			
	
	
		
			216 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			216 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Swift
		
	
| 
											8 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import UIKit | ||
| 
											5 years ago
										 | import SessionUIKit | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 | @objc | ||
| 
											8 years ago
										 | public protocol NavBarLayoutDelegate: class { | ||
| 
											8 years ago
										 |     func navBarCallLayoutDidChange(navbar: OWSNavigationBar) | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | @objc | ||
| 
											8 years ago
										 | public class OWSNavigationBar: UINavigationBar { | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public weak var navBarLayoutDelegate: NavBarLayoutDelegate? | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public let navbarWithoutStatusHeight: CGFloat = 44 | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public var callBannerHeight: CGFloat { | ||
| 
											7 years ago
										 |         return OWSWindowManagerCallBannerHeight() | ||
| 
											8 years ago
										 |     } | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public var statusBarHeight: CGFloat { | ||
| 
											8 years ago
										 |         return CurrentAppContext().statusBarHeight | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public var fullWidth: CGFloat { | ||
| 
											8 years ago
										 |         return UIScreen.main.bounds.size.width | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     public required init?(coder aDecoder: NSCoder) { | ||
| 
											7 years ago
										 |         notImplemented() | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     public static let backgroundBlurMutingFactor: CGFloat = 0.5 | ||
| 
											7 years ago
										 |     var blurEffectView: UIVisualEffectView? | ||
| 
											7 years ago
										 | 
 | ||
| 
											8 years ago
										 |     override init(frame: CGRect) { | ||
|  |         super.init(frame: frame) | ||
|  | 
 | ||
| 
											7 years ago
										 |         applyTheme() | ||
|  | 
 | ||
|  |         NotificationCenter.default.addObserver(self, selector: #selector(callDidChange), name: .OWSWindowManagerCallDidChange, object: nil) | ||
| 
											7 years ago
										 |         NotificationCenter.default.addObserver(self, selector: #selector(didChangeStatusBarFrame), name: UIApplication.didChangeStatusBarFrameNotification, object: nil) | ||
| 
											7 years ago
										 |         NotificationCenter.default.addObserver(self, | ||
|  |                                                selector: #selector(themeDidChange), | ||
|  |                                                name: .ThemeDidChange, | ||
|  |                                                object: nil) | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: FirstResponder Stubbing | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public weak var stubbedNextResponder: UIResponder? | ||
|  | 
 | ||
|  |     override public var next: UIResponder? { | ||
|  |         if let stubbedNextResponder = self.stubbedNextResponder { | ||
|  |             return stubbedNextResponder | ||
|  |         } | ||
|  | 
 | ||
|  |         return super.next | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: Theme | ||
|  | 
 | ||
|  |     private func applyTheme() { | ||
| 
											7 years ago
										 |         guard respectsTheme else { | ||
|  |             return | ||
|  |         } | ||
|  | 
 | ||
| 
											6 years ago
										 |         backgroundColor = Colors.navigationBarBackground | ||
| 
											6 years ago
										 |          | ||
| 
											6 years ago
										 |         tintColor = Colors.text | ||
| 
											6 years ago
										 |          | ||
| 
											7 years ago
										 |         if UIAccessibility.isReduceTransparencyEnabled { | ||
| 
											7 years ago
										 |             blurEffectView?.isHidden = true | ||
| 
											7 years ago
										 |             let color = Theme.navbarBackgroundColor | ||
|  |             let backgroundImage = UIImage(color: color) | ||
|  |             self.setBackgroundImage(backgroundImage, for: .default) | ||
|  |         } else { | ||
| 
											7 years ago
										 |             // Make navbar more translucent than default. Navbars remove alpha from any assigned backgroundColor, so | ||
|  |             // to achieve transparency, we have to assign a transparent image. | ||
| 
											6 years ago
										 |             let color = Theme.navbarBackgroundColor | ||
| 
											7 years ago
										 |             let backgroundImage = UIImage(color: color) | ||
|  |             self.setBackgroundImage(backgroundImage, for: .default) | ||
|  | 
 | ||
|  |             // remove hairline below bar. | ||
|  |             self.shadowImage = UIImage() | ||
|  |         } | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     public func themeDidChange() { | ||
| 
											7 years ago
										 |         Logger.debug("") | ||
| 
											7 years ago
										 |         applyTheme() | ||
| 
											7 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     public var respectsTheme: Bool = true { | ||
|  |         didSet { | ||
|  |             themeDidChange() | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: Layout | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func callDidChange() { | ||
| 
											7 years ago
										 |         Logger.debug("") | ||
| 
											8 years ago
										 |         self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func didChangeStatusBarFrame() { | ||
| 
											7 years ago
										 |         Logger.debug("") | ||
| 
											8 years ago
										 |         self.navBarLayoutDelegate?.navBarCallLayoutDidChange(navbar: self) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     public override func sizeThatFits(_ size: CGSize) -> CGSize { | ||
| 
											8 years ago
										 |         guard OWSWindowManager.shared().hasCall() else { | ||
|  |             return super.sizeThatFits(size) | ||
|  |         } | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |         if #available(iOS 11, *) { | ||
|  |             return super.sizeThatFits(size) | ||
| 
											7 years ago
										 |         } else if #available(iOS 10, *) { | ||
|  |             // iOS10 | ||
|  |             // sizeThatFits is repeatedly called to determine how much space to reserve for that navbar. | ||
| 
											8 years ago
										 |             // That is, increasing this causes the child view controller to be pushed down. | ||
|  |             // (as of iOS11, this is not used and instead we use additionalSafeAreaInsets) | ||
| 
											8 years ago
										 |             return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + statusBarHeight) | ||
| 
											7 years ago
										 |         } else { | ||
|  |             // iOS9 | ||
|  |             // sizeThatFits is repeatedly called to determine how much space to reserve for that navbar. | ||
|  |             // That is, increasing this causes the child view controller to be pushed down. | ||
|  |             // (as of iOS11, this is not used and instead we use additionalSafeAreaInsets)             | ||
|  |             return CGSize(width: fullWidth, height: navbarWithoutStatusHeight + callBannerHeight + 20) | ||
| 
											8 years ago
										 |         } | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     public override func layoutSubviews() { | ||
| 
											7 years ago
										 |         guard CurrentAppContext().isMainApp else { | ||
|  |             super.layoutSubviews() | ||
|  |             return | ||
|  |         } | ||
|  |         guard OWSWindowManager.shared().hasCall() else { | ||
|  |             super.layoutSubviews() | ||
|  |             return | ||
| 
											8 years ago
										 |         } | ||
|  | 
 | ||
| 
											7 years ago
										 |         guard #available(iOS 11, *) else { | ||
|  |             super.layoutSubviews() | ||
|  |             return | ||
|  |         } | ||
|  | 
 | ||
| 
											8 years ago
										 |         self.frame = CGRect(x: 0, y: callBannerHeight, width: fullWidth, height: navbarWithoutStatusHeight) | ||
|  |         self.bounds = CGRect(x: 0, y: 0, width: fullWidth, height: navbarWithoutStatusHeight) | ||
| 
											8 years ago
										 | 
 | ||
|  |         super.layoutSubviews() | ||
|  | 
 | ||
| 
											8 years ago
										 |         // This is only necessary on iOS11, which has some private views within that lay outside of the navbar. | ||
|  |         // They aren't actually visible behind the call status bar, but they looks strange during present/dismiss | ||
|  |         // animations for modal VC's | ||
| 
											8 years ago
										 |         for subview in self.subviews { | ||
|  |             let stringFromClass = NSStringFromClass(subview.classForCoder) | ||
|  |             if stringFromClass.contains("BarBackground") { | ||
| 
											8 years ago
										 |                 subview.frame = self.bounds | ||
| 
											8 years ago
										 |             } else if stringFromClass.contains("BarContentView") { | ||
| 
											8 years ago
										 |                 subview.frame = self.bounds | ||
| 
											8 years ago
										 |             } | ||
| 
											8 years ago
										 |         } | ||
|  |     } | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |     // MARK: Override Theme | ||
| 
											7 years ago
										 | 
 | ||
|  |     @objc | ||
| 
											7 years ago
										 |     public enum NavigationBarThemeOverride: Int { | ||
|  |         case clear, alwaysDark | ||
| 
											7 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     public func overrideTheme(type: NavigationBarThemeOverride) { | ||
|  |         respectsTheme = false | ||
|  | 
 | ||
|  |         barStyle = .black | ||
| 
											7 years ago
										 |         titleTextAttributes = [NSAttributedString.Key.foregroundColor: Theme.darkThemePrimaryColor] | ||
| 
											7 years ago
										 |         barTintColor = Theme.darkThemeBackgroundColor.withAlphaComponent(0.6) | ||
|  |         tintColor = Theme.darkThemePrimaryColor | ||
|  | 
 | ||
|  |         switch type { | ||
|  |         case .clear: | ||
|  |             blurEffectView?.isHidden = true | ||
|  |             clipsToBounds = true | ||
|  | 
 | ||
|  |             // Making a toolbar transparent requires setting an empty uiimage | ||
|  |             setBackgroundImage(UIImage(), for: .default) | ||
|  |             shadowImage = UIImage() | ||
|  |             backgroundColor = .clear | ||
|  |         case .alwaysDark: | ||
|  |             blurEffectView?.isHidden = false | ||
|  |             clipsToBounds = false | ||
|  | 
 | ||
|  |             setBackgroundImage(nil, for: .default) | ||
|  |             shadowImage = nil | ||
| 
											7 years ago
										 |         } | ||
| 
											7 years ago
										 |     } | ||
| 
											8 years ago
										 | } |