<commit_before>/*
 * Copyright (C) 2015 - 2018, <NAME> and CosmicMind, Inc. <http://cosmicmind.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *	*	Redistributions of source code must retain the above copyright notice, this
 *		list of conditions and the following disclaimer.
 *
 *	*	Redistributions in binary form must reproduce the above copyright notice,
 *		this list of conditions and the following disclaimer in the documentation
 *		and/or other materials provided with the distribution.
 *
 *	*	Neither the name of CosmicMind nor the names of its
 *		contributors may be used to endorse or promote products derived from
 *		this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import UIKit
import Motion

extension NavigationController {
  /// Device status bar style.
  open var statusBarStyle: UIStatusBarStyle {
    get {
      return Application.statusBarStyle
    }
    set(value) {
      Application.statusBarStyle = value
    }
  }
}

open class NavigationController: UINavigationController {
  /**
   An initializer that initializes the object with a NSCoder object.
   - Parameter aDecoder: A NSCoder instance.
   */
  public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  
  /**
   An initializer that initializes the object with an Optional nib and bundle.
   - Parameter nibNameOrNil: An Optional String for the nib.
   - Parameter bundle: An Optional NSBundle where the nib is located.
   */
  public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }
  
  /**
   An initializer that initializes the object with a rootViewController.
   - Parameter rootViewController: A UIViewController for the rootViewController.
   */
  public override init(rootViewController: UIViewController) {
    super.init(navigationBarClass: NavigationBar.self, toolbarClass: nil)
    setViewControllers([rootViewController], animated: false)
  }
    
    public init(rootViewController: UIViewController, navigationBarClass: Swift.AnyClass?) {
        super.init(navigationBarClass: navigationBarClass, toolbarClass: nil)
        setViewControllers([rootViewController], animated: false)
    }
  
  open override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard let v = interactivePopGestureRecognizer else {
      return
    }
    
    guard let x = navigationDrawerController else {
      return
    }
    
    if let l = x.leftPanGesture {
      l.require(toFail: v)
    }
    
    if let r = x.rightPanGesture {
      r.require(toFail: v)
    }
  }
  
  open override func viewDidLoad() {
    super.viewDidLoad()
    prepare()
  }
  
  open override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    guard let v = navigationBar as? NavigationBar else {
      return
    }
    
    guard let item = v.topItem else {
      return
    }
    
    v.layoutNavigationItem(item: item)
  }
  
  open override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    layoutSubviews()
  }
  
  /**
   Prepares the view instance when intialized. When subclassing,
   it is recommended to override the prepare method
   to initialize property values and other setup operations.
   The super.prepare method should always be called immediately
   when subclassing.
   */
  open func prepare() {
    navigationBar.frame.size.width = view.bounds.width
    navigationBar.heightPreset = .normal
    
    view.clipsToBounds = true
    view.backgroundColor = .white
    view.contentScaleFactor = Screen.scale
    
    // This ensures the panning gesture is available when going back between views.
    if let v = interactivePopGestureRecognizer {
      v.isEnabled = true
      v.delegate = self
    }
  }
  
  /// Calls the layout functions for the view heirarchy.
  open func layoutSubviews() {
    navigationBar.setNeedsUpdateConstraints()
    navigationBar.updateConstraintsIfNeeded()
    navigationBar.setNeedsLayout()
    navigationBar.layoutIfNeeded()
  }
}

extension NavigationController: UINavigationBarDelegate {
  /**
   Delegation method that is called when a new UINavigationItem is about to be pushed.
   This is used to prepare the transitions between UIViewControllers on the stack.
   - Parameter navigationBar: A UINavigationBar that is used in the NavigationController.
   - Parameter item: The UINavigationItem that will be pushed on the stack.
   - Returns: A Boolean value that indicates whether to push the item on to the stack or not.
   True is yes, false is no.
   */
  public func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
    if let v = navigationBar as? NavigationBar {
      if nil == item.backButton.image && nil == item.backButton.title {
        item.backButton.image = v.backButtonImage
      }
      
      if !item.backButton.isHidden {
        item.leftViews.insert(item.backButton, at: 0)
      }
      
      item.backButton.addTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
      
      item.hidesBackButton = false
      item.setHidesBackButton(true, animated: false)
      
      v.layoutNavigationItem(item: item)
    }
    
    return true
  }
  
  public func navigationBar(_ navigationBar: UINavigationBar, didPop item: UINavigationItem) {
    if let index = item.leftViews.index(of: item.backButton) {
      item.leftViews.remove(at: index)
    }
    
    item.backButton.removeTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
  }
}

internal extension NavigationController {
  /// Handler for the backbutton.
  @objc
  func handle(backButton: UIButton) {
    popViewController(animated: true)
  }
}

extension NavigationController: UIGestureRecognizerDelegate {
  /**
   Detects the gesture recognizer being used. This is necessary when using
   NavigationDrawerController. It eliminates the conflict in panning.
   - Parameter gestureRecognizer: A UIGestureRecognizer to detect.
   - Parameter touch: The UITouch event.
   - Returns: A Boolean of whether to continue the gesture or not, true yes, false no.
   */
  public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return interactivePopGestureRecognizer == gestureRecognizer && nil != navigationBar.backItem
  }
}<commit_msg>Fixed backButton issues with navigation bar show/hide<commit_after>/*
 * Copyright (C) 2015 - 2018, <NAME> and CosmicMind, Inc. <http://cosmicmind.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *	*	Redistributions of source code must retain the above copyright notice, this
 *		list of conditions and the following disclaimer.
 *
 *	*	Redistributions in binary form must reproduce the above copyright notice,
 *		this list of conditions and the following disclaimer in the documentation
 *		and/or other materials provided with the distribution.
 *
 *	*	Neither the name of CosmicMind nor the names of its
 *		contributors may be used to endorse or promote products derived from
 *		this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import UIKit
import Motion

extension NavigationController {
  /// Device status bar style.
  open var statusBarStyle: UIStatusBarStyle {
    get {
      return Application.statusBarStyle
    }
    set(value) {
      Application.statusBarStyle = value
    }
  }
}

open class NavigationController: UINavigationController {
  /**
   An initializer that initializes the object with a NSCoder object.
   - Parameter aDecoder: A NSCoder instance.
   */
  public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  
  /**
   An initializer that initializes the object with an Optional nib and bundle.
   - Parameter nibNameOrNil: An Optional String for the nib.
   - Parameter bundle: An Optional NSBundle where the nib is located.
   */
  public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }
  
  /**
   An initializer that initializes the object with a rootViewController.
   - Parameter rootViewController: A UIViewController for the rootViewController.
   */
  public override init(rootViewController: UIViewController) {
    super.init(navigationBarClass: NavigationBar.self, toolbarClass: nil)
    setViewControllers([rootViewController], animated: false)
  }
    
    public init(rootViewController: UIViewController, navigationBarClass: Swift.AnyClass?) {
        super.init(navigationBarClass: navigationBarClass, toolbarClass: nil)
        setViewControllers([rootViewController], animated: false)
    }
  
  open override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard let v = interactivePopGestureRecognizer else {
      return
    }
    
    guard let x = navigationDrawerController else {
      return
    }
    
    if let l = x.leftPanGesture {
      l.require(toFail: v)
    }
    
    if let r = x.rightPanGesture {
      r.require(toFail: v)
    }
  }
  
  open override func viewDidLoad() {
    super.viewDidLoad()
    prepare()
  }
  
  open override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    guard let v = navigationBar as? NavigationBar else {
      return
    }
    
    guard let item = v.topItem else {
      return
    }
    
    v.layoutNavigationItem(item: item)
  }
  
  open override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    layoutSubviews()
  }
  
  /**
   Prepares the view instance when intialized. When subclassing,
   it is recommended to override the prepare method
   to initialize property values and other setup operations.
   The super.prepare method should always be called immediately
   when subclassing.
   */
  open func prepare() {
    navigationBar.frame.size.width = view.bounds.width
    navigationBar.heightPreset = .normal
    
    view.clipsToBounds = true
    view.backgroundColor = .white
    view.contentScaleFactor = Screen.scale
    
    // This ensures the panning gesture is available when going back between views.
    if let v = interactivePopGestureRecognizer {
      v.isEnabled = true
      v.delegate = self
    }
  }
  
  /// Calls the layout functions for the view heirarchy.
  open func layoutSubviews() {
    navigationBar.setNeedsUpdateConstraints()
    navigationBar.updateConstraintsIfNeeded()
    navigationBar.setNeedsLayout()
    navigationBar.layoutIfNeeded()
  }
  
  /**
   Sets whether the navigation bar is hidden.
   - Parameter _ hidden: Specify true to hide the navigation bar or false to show it.
   - Parameter animated: Specify true if you want to animate the change in visibility or false if you want the navigation bar to appear immediately.
   */
  open override func setNavigationBarHidden(_ hidden: Bool, animated: Bool) {
    super.setNavigationBarHidden(hidden, animated: animated)
    guard let items = navigationBar.items, items.count > 1 else {
      return
    }
    
    items.forEach {
      prepareBackButton(for: $0, in: navigationBar)
    }
  }
}

extension NavigationController: UINavigationBarDelegate {
  /**
   Delegation method that is called when a new UINavigationItem is about to be pushed.
   This is used to prepare the transitions between UIViewControllers on the stack.
   - Parameter navigationBar: A UINavigationBar that is used in the NavigationController.
   - Parameter item: The UINavigationItem that will be pushed on the stack.
   - Returns: A Boolean value that indicates whether to push the item on to the stack or not.
   True is yes, false is no.
   */
  public func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
    prepareBackButton(for: item, in: navigationBar)
    return true
  }
  
  public func navigationBar(_ navigationBar: UINavigationBar, didPop item: UINavigationItem) {
    removeBackButton(from: item)
  }
}

internal extension NavigationController {
  /// Handler for the backbutton.
  @objc
  func handle(backButton: UIButton) {
    popViewController(animated: true)
  }
  
  /**
   Prepares back button of the navigation item in navigation bar.
   - Parameter for item: A UINavigationItem.
   - Parameter in navigationBar: A UINavigationBar.
   */
  func prepareBackButton(for item: UINavigationItem, in navigationBar: UINavigationBar) {
    guard let v = navigationBar as? NavigationBar else {
      return
    }
    
    if nil == item.backButton.image && nil == item.backButton.title {
      item.backButton.image = v.backButtonImage
    }
    
    if !item.backButton.isHidden && !item.leftViews.contains(item.backButton) {
      item.leftViews.insert(item.backButton, at: 0)
    }
    
    item.backButton.addTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
    
    item.hidesBackButton = false
    item.setHidesBackButton(true, animated: false)
    
    v.layoutNavigationItem(item: item)
  }
  
  /**
   Removes back button of the navigation item.
   - Parameter from item: A UINavigationItem.
   */
  func removeBackButton(from item: UINavigationItem) {
    if let index = item.leftViews.index(of: item.backButton) {
      item.leftViews.remove(at: index)
    }
    
    item.backButton.removeTarget(self, action: #selector(handle(backButton:)), for: .touchUpInside)
  }
}

extension NavigationController: UIGestureRecognizerDelegate {
  /**
   Detects the gesture recognizer being used. This is necessary when using
   NavigationDrawerController. It eliminates the conflict in panning.
   - Parameter gestureRecognizer: A UIGestureRecognizer to detect.
   - Parameter touch: The UITouch event.
   - Returns: A Boolean of whether to continue the gesture or not, true yes, false no.
   */
  public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return interactivePopGestureRecognizer == gestureRecognizer && nil != navigationBar.backItem
  }
}
{'fraction_non_alphanumeric': 0.06992742189965288, 'fraction_numerical': 0.0012622278321236984, 'mean_word_length': 3.4573839662447257, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7134705', 'n_tokens_mistral': 4517, 'n_tokens_neox': 4278, 'n_words': 1541}