<commit_before>import UIKit

@objc (WMFNavigationBarHiderDelegate)
public protocol NavigationBarHiderDelegate: NSObjectProtocol {
    func navigationBarHider(_ hider: NavigationBarHider, didSetNavigationBarPercentHidden: CGFloat, underBarViewPercentHidden: CGFloat, extendedViewPercentHidden: CGFloat, animated: Bool)
}


extension CGFloat {
    func wmf_adjustedForRange(_ lower: CGFloat, upper: CGFloat, step: CGFloat) -> CGFloat {
        if self < lower + step {
            return lower
        } else if self > upper - step {
            return upper
        } else if isNaN || isInfinite {
            return lower
        } else {
            return self
        }
    }

    var wmf_normalizedPercentage: CGFloat {
        return wmf_adjustedForRange(0, upper: 1, step: 0.01)
    }
}


@objc(WMFNavigationBarHider)
public class NavigationBarHider: NSObject {
    @objc public weak var navigationBar: NavigationBar?
    @objc public weak var delegate: NavigationBarHiderDelegate?
    
    fileprivate var isUserScrolling: Bool = false
    fileprivate var isScrollingToTop: Bool = false
    var initialScrollY: CGFloat = 0
    var initialNavigationBarPercentHidden: CGFloat = 0

    public var isHidingEnabled: Bool = true
    public var isBarHidingEnabled: Bool = true
    
    public var isUnderBarViewHidingEnabled: Bool = true {
        didSet {
            navigationBar?.isUnderBarViewHidingEnabled = isUnderBarViewHidingEnabled
        }
    }
    
    public var isExtendedViewHidingEnabled: Bool = true {
        didSet {
            navigationBar?.isExtendedViewHidingEnabled = isExtendedViewHidingEnabled
        }
    }
    
    @objc public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }
        isUserScrolling = true
        initialScrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        initialNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
    }

    @objc public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }

        guard isUserScrolling || isScrollingToTop else {
            return
        }
        
        let animated = false

        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        let currentUnderBarViewPercentHidden = navigationBar.underBarViewPercentHidden
        let currentExtendedViewPercentHidden = navigationBar.extendedViewPercentHidden
        
        var navigationBarPercentHidden = currentNavigationBarPercentHidden
        var underBarViewPercentHidden = currentUnderBarViewPercentHidden
        var extendedViewPercentHidden = currentExtendedViewPercentHidden

        let scrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        if isUnderBarViewHidingEnabled && underBarViewHeight > 0 {
            underBarViewPercentHidden = (scrollY/underBarViewHeight).wmf_normalizedPercentage
        }
        
        let extendedViewHeight = navigationBar.extendedView.frame.size.height
        if isExtendedViewHidingEnabled && extendedViewHeight > 0 {
            extendedViewPercentHidden = (scrollY/extendedViewHeight).wmf_normalizedPercentage
        }
        
        let barHeight = navigationBar.bar.frame.size.height
        if !isBarHidingEnabled {
            navigationBarPercentHidden = 0
        } else if initialScrollY < extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = ((scrollY - extendedViewHeight - underBarViewHeight)/barHeight).wmf_normalizedPercentage
        } else if scrollY <= extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = min(initialNavigationBarPercentHidden, ((scrollY - extendedViewHeight - underBarViewHeight)/barHeight).wmf_normalizedPercentage)
        } else if initialNavigationBarPercentHidden == 0 && initialScrollY > extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = ((scrollY - initialScrollY)/barHeight).wmf_normalizedPercentage
        }

        guard currentExtendedViewPercentHidden != extendedViewPercentHidden || currentNavigationBarPercentHidden !=  navigationBarPercentHidden || currentUnderBarViewPercentHidden != underBarViewPercentHidden else {
            return
        }
        navigationBar.setNavigationBarPercentHidden(navigationBarPercentHidden, underBarViewPercentHidden: underBarViewPercentHidden, extendedViewPercentHidden: extendedViewPercentHidden, animated: animated, additionalAnimations:{
            self.delegate?.navigationBarHider(self, didSetNavigationBarPercentHidden: navigationBarPercentHidden, underBarViewPercentHidden: underBarViewPercentHidden, extendedViewPercentHidden: extendedViewPercentHidden, animated: animated)
        })
    }
    
    @objc public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }
        
        let barHeight = navigationBar.bar.frame.size.height
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        let extendedViewHeight = navigationBar.extendedView.frame.size.height

        let top = 0 - scrollView.contentInset.top
        let targetOffsetY = targetContentOffset.pointee.y - top
        if targetOffsetY < extendedViewHeight + barHeight + underBarViewHeight {
            if targetOffsetY < 0.5 * (extendedViewHeight + underBarViewHeight) { // both visible
                targetContentOffset.pointee = CGPoint(x: 0, y: top)
            } else if targetOffsetY < extendedViewHeight + underBarViewHeight + 0.5 * barHeight  { // only nav bar visible
                targetContentOffset.pointee = CGPoint(x: 0, y: top + extendedViewHeight + underBarViewHeight)
            } else if targetOffsetY < extendedViewHeight + barHeight {
                targetContentOffset.pointee = CGPoint(x: 0, y: top + extendedViewHeight + barHeight + underBarViewHeight)
            }
            return
        }
        
        if initialScrollY < extendedViewHeight + barHeight + underBarViewHeight && targetOffsetY > extendedViewHeight + barHeight + underBarViewHeight { // let it naturally hide
            return
        }

        isUserScrolling = false

        let animated = true

        let extendedViewPercentHidden = navigationBar.extendedViewPercentHidden
        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        var navigationBarPercentHidden: CGFloat = currentNavigationBarPercentHidden
        if !isNavigationBarHidingEnabled {
            navigationBarPercentHidden = 0
        } else if velocity.y > 0 {
            navigationBarPercentHidden = 1
        } else if velocity.y < 0 {
            navigationBarPercentHidden = 0
        } else if navigationBarPercentHidden < 0.5 {
            navigationBarPercentHidden = 0
        } else {
            navigationBarPercentHidden = 1
        }<commit_msg>get underBarViewPercentHidden off nav bar<commit_after>import UIKit

@objc (WMFNavigationBarHiderDelegate)
public protocol NavigationBarHiderDelegate: NSObjectProtocol {
    func navigationBarHider(_ hider: NavigationBarHider, didSetNavigationBarPercentHidden: CGFloat, underBarViewPercentHidden: CGFloat, extendedViewPercentHidden: CGFloat, animated: Bool)
}


extension CGFloat {
    func wmf_adjustedForRange(_ lower: CGFloat, upper: CGFloat, step: CGFloat) -> CGFloat {
        if self < lower + step {
            return lower
        } else if self > upper - step {
            return upper
        } else if isNaN || isInfinite {
            return lower
        } else {
            return self
        }
    }

    var wmf_normalizedPercentage: CGFloat {
        return wmf_adjustedForRange(0, upper: 1, step: 0.01)
    }
}


@objc(WMFNavigationBarHider)
public class NavigationBarHider: NSObject {
    @objc public weak var navigationBar: NavigationBar?
    @objc public weak var delegate: NavigationBarHiderDelegate?
    
    fileprivate var isUserScrolling: Bool = false
    fileprivate var isScrollingToTop: Bool = false
    var initialScrollY: CGFloat = 0
    var initialNavigationBarPercentHidden: CGFloat = 0

    public var isHidingEnabled: Bool = true
    public var isBarHidingEnabled: Bool = true
    
    public var isUnderBarViewHidingEnabled: Bool = true {
        didSet {
            navigationBar?.isUnderBarViewHidingEnabled = isUnderBarViewHidingEnabled
        }
    }
    
    public var isExtendedViewHidingEnabled: Bool = true {
        didSet {
            navigationBar?.isExtendedViewHidingEnabled = isExtendedViewHidingEnabled
        }
    }
    
    @objc public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }
        isUserScrolling = true
        initialScrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        initialNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
    }

    @objc public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }

        guard isUserScrolling || isScrollingToTop else {
            return
        }
        
        let animated = false

        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        let currentUnderBarViewPercentHidden = navigationBar.underBarViewPercentHidden
        let currentExtendedViewPercentHidden = navigationBar.extendedViewPercentHidden
        
        var navigationBarPercentHidden = currentNavigationBarPercentHidden
        var underBarViewPercentHidden = currentUnderBarViewPercentHidden
        var extendedViewPercentHidden = currentExtendedViewPercentHidden

        let scrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        if isUnderBarViewHidingEnabled && underBarViewHeight > 0 {
            underBarViewPercentHidden = (scrollY/underBarViewHeight).wmf_normalizedPercentage
        }
        
        let extendedViewHeight = navigationBar.extendedView.frame.size.height
        if isExtendedViewHidingEnabled && extendedViewHeight > 0 {
            extendedViewPercentHidden = (scrollY/extendedViewHeight).wmf_normalizedPercentage
        }
        
        let barHeight = navigationBar.bar.frame.size.height
        if !isBarHidingEnabled {
            navigationBarPercentHidden = 0
        } else if initialScrollY < extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = ((scrollY - extendedViewHeight - underBarViewHeight)/barHeight).wmf_normalizedPercentage
        } else if scrollY <= extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = min(initialNavigationBarPercentHidden, ((scrollY - extendedViewHeight - underBarViewHeight)/barHeight).wmf_normalizedPercentage)
        } else if initialNavigationBarPercentHidden == 0 && initialScrollY > extendedViewHeight + barHeight + underBarViewHeight {
            navigationBarPercentHidden = ((scrollY - initialScrollY)/barHeight).wmf_normalizedPercentage
        }

        guard currentExtendedViewPercentHidden != extendedViewPercentHidden || currentNavigationBarPercentHidden !=  navigationBarPercentHidden || currentUnderBarViewPercentHidden != underBarViewPercentHidden else {
            return
        }
        navigationBar.setNavigationBarPercentHidden(navigationBarPercentHidden, underBarViewPercentHidden: underBarViewPercentHidden, extendedViewPercentHidden: extendedViewPercentHidden, animated: animated, additionalAnimations:{
            self.delegate?.navigationBarHider(self, didSetNavigationBarPercentHidden: navigationBarPercentHidden, underBarViewPercentHidden: underBarViewPercentHidden, extendedViewPercentHidden: extendedViewPercentHidden, animated: animated)
        })
    }
    
    @objc public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        guard let navigationBar = navigationBar, isHidingEnabled else {
            return
        }
        
        let barHeight = navigationBar.bar.frame.size.height
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        let extendedViewHeight = navigationBar.extendedView.frame.size.height

        let top = 0 - scrollView.contentInset.top
        let targetOffsetY = targetContentOffset.pointee.y - top
        if targetOffsetY < extendedViewHeight + barHeight + underBarViewHeight {
            if targetOffsetY < 0.5 * (extendedViewHeight + underBarViewHeight) { // both visible
                targetContentOffset.pointee = CGPoint(x: 0, y: top)
            } else if targetOffsetY < extendedViewHeight + underBarViewHeight + 0.5 * barHeight  { // only nav bar visible
                targetContentOffset.pointee = CGPoint(x: 0, y: top + extendedViewHeight + underBarViewHeight)
            } else if targetOffsetY < extendedViewHeight + barHeight {
                targetContentOffset.pointee = CGPoint(x: 0, y: top + extendedViewHeight + barHeight + underBarViewHeight)
            }
            return
        }
        
        if initialScrollY < extendedViewHeight + barHeight + underBarViewHeight && targetOffsetY > extendedViewHeight + barHeight + underBarViewHeight { // let it naturally hide
            return
        }

        isUserScrolling = false

        let animated = true

        let extendedViewPercentHidden = navigationBar.extendedViewPercentHidden
        let underBarViewPercentHidden = navigationBar.underBarViewPercentHidden
        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        
        var navigationBarPercentHidden: CGFloat = currentNavigationBarPercentHidden
        if !isNavigationBarHidingEnabled {
            navigationBarPercentHidden = 0
        } else if velocity.y > 0 {
            navigationBarPercentHidden = 1
        } else if velocity.y < 0 {
            navigationBarPercentHidden = 0
        } else if navigationBarPercentHidden < 0.5 {
            navigationBarPercentHidden = 0
        } else {
            navigationBarPercentHidden = 1
        }
{'fraction_non_alphanumeric': 0.055513980542894074, 'fraction_numerical': 0.003809782978433907, 'mean_word_length': 3.082199389058595, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, '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': '6068793', 'n_tokens_mistral': 3710, 'n_tokens_neox': 3566, 'n_words': 821}