<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
    
    @objc public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, navigationBar.isInteractiveHidingEnabled else {
            return
        }
        isUserScrolling = true
        initialScrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        initialNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
    }

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

        let scrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        let barHeight = navigationBar.bar.frame.size.height
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        
        guard isUserScrolling || isScrollingToTop else {
            if navigationBar.shouldTransformUnderBarViewWithBar {
                navigationBar.shadowAlpha = (scrollY/(barHeight + underBarViewHeight)).wmf_normalizedPercentage
            }
            return
        }
        
        let animated = false
        
        var shadowAlpha: CGFloat = 1
        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        let currentUnderBarViewPercentHidden = navigationBar.underBarViewPercentHidden
        let currentExtendedViewPercentHidden = navigationBar.extendedViewPercentHidden

        var navigationBarPercentHidden = currentNavigationBarPercentHidden
        var underBarViewPercentHidden = currentUnderBarViewPercentHidden
        var extendedViewPercentHidden = currentExtendedViewPercentHidden

        let extendedViewHeight = navigationBar.extendedView.frame.size.height

        let shouldHideUnderBarView = navigationBar.isUnderBarViewHidingEnabled && underBarViewHeight > 0
        let shouldHideExtendedView = navigationBar.isExtendedViewHidingEnabled && extendedViewHeight > 0

        if navigationBar.shouldTransformUnderBarViewWithBar && navigationBar.isBarHidingEnabled {
            underBarViewPercentHidden = (scrollY/(barHeight + underBarViewHeight)).wmf_normalizedPercentage
            shadowAlpha = underBarViewPercentHidden
        } else if shouldHideUnderBarView {
            underBarViewPercentHidden = (scrollY/underBarViewHeight).wmf_normalizedPercentage
        }
        
        if shouldHideUnderBarView && shouldHideExtendedView {
            extendedViewPercentHidden = ((scrollY - underBarViewHeight)/extendedViewHeight).wmf_normalizedPercentage
        } else if shouldHideExtendedView {
            extendedViewPercentHidden = (scrollY/extendedViewHeight).wmf_normalizedPercentage
        }

        if !navigationBar.isBarHidingEnabled {
            navigationBarPercentHidden = 0
        } else if navigationBar.shouldTransformUnderBarViewWithBar {
            navigationBarPercentHidden = underBarViewPercentHidden
        } 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, shadowAlpha: shadowAlpha, 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, navigationBar.isInteractiveHidingEnabled 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<commit_msg>fix hide/unhide when showing the navigation bar while scrolled down the page<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
    
    @objc public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let navigationBar = navigationBar, navigationBar.isInteractiveHidingEnabled else {
            return
        }
        isUserScrolling = true
        initialScrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        initialNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
    }

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

        let scrollY = scrollView.contentOffset.y + scrollView.contentInset.top
        let barHeight = navigationBar.bar.frame.size.height
        let underBarViewHeight = navigationBar.underBarView.frame.size.height
        
        guard isUserScrolling || isScrollingToTop else {
            if navigationBar.shouldTransformUnderBarViewWithBar {
                navigationBar.shadowAlpha = (scrollY/(barHeight + underBarViewHeight)).wmf_normalizedPercentage
            }
            return
        }
        
        let animated = false
        
        var shadowAlpha: CGFloat = 1
        let currentNavigationBarPercentHidden = navigationBar.navigationBarPercentHidden
        let currentUnderBarViewPercentHidden = navigationBar.underBarViewPercentHidden
        let currentExtendedViewPercentHidden = navigationBar.extendedViewPercentHidden

        var navigationBarPercentHidden = currentNavigationBarPercentHidden
        var underBarViewPercentHidden = currentUnderBarViewPercentHidden
        var extendedViewPercentHidden = currentExtendedViewPercentHidden

        let extendedViewHeight = navigationBar.extendedView.frame.size.height

        let shouldHideUnderBarView = navigationBar.isUnderBarViewHidingEnabled && underBarViewHeight > 0
        let shouldHideExtendedView = navigationBar.isExtendedViewHidingEnabled && extendedViewHeight > 0

        if shouldHideUnderBarView {
            underBarViewPercentHidden = (scrollY/underBarViewHeight).wmf_normalizedPercentage
        }
        
        if shouldHideUnderBarView && shouldHideExtendedView {
            extendedViewPercentHidden = ((scrollY - underBarViewHeight)/extendedViewHeight).wmf_normalizedPercentage
        } else if shouldHideExtendedView {
            extendedViewPercentHidden = (scrollY/extendedViewHeight).wmf_normalizedPercentage
        }

        if !navigationBar.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 {
            if navigationBar.shouldTransformUnderBarViewWithBar {
                navigationBarPercentHidden = ((scrollY - initialScrollY)/(barHeight + underBarViewHeight)).wmf_normalizedPercentage
                underBarViewPercentHidden = navigationBarPercentHidden
            } else {
                navigationBarPercentHidden = ((scrollY - initialScrollY)/barHeight).wmf_normalizedPercentage
            }
        }
        
        if navigationBar.shouldTransformUnderBarViewWithBar {
            shadowAlpha = (underBarViewPercentHidden + navigationBarPercentHidden)/2
        }

        guard currentExtendedViewPercentHidden != extendedViewPercentHidden || currentNavigationBarPercentHidden !=  navigationBarPercentHidden || currentUnderBarViewPercentHidden != underBarViewPercentHidden else {
            return
        }
        navigationBar.setNavigationBarPercentHidden(navigationBarPercentHidden, underBarViewPercentHidden: underBarViewPercentHidden, extendedViewPercentHidden: extendedViewPercentHidden, shadowAlpha: shadowAlpha, 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, navigationBar.isInteractiveHidingEnabled 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
{'fraction_non_alphanumeric': 0.05488974113135187, 'fraction_numerical': 0.0021572387344199425, 'mean_word_length': 3.518772563176895, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '6068795', 'n_tokens_mistral': 3095, 'n_tokens_neox': 2996, 'n_words': 634}