<commit_before>import UIKit
import WebKit

extension WebViewController {

    /**
     Setup the refresh control for reloading the web view.
     */
    open func setupRefreshControl() {
        let newRefreshControl = UIRefreshControl()
//        newRefreshControl.attributedTitle = NSAttributedString(string: "")
// This is a hack to improve the way the large titles work. They were not returning to the origional position in many cases.
// Pulling down fast seemed to reproduce the problem the most.
// The refresh method is now called in the scrollViewDidEndDecelerating method.
//        newRefreshControl.addTarget(self, action: #selector(WebViewController.refresh), for: UIControlEvents.valueChanged)
        if #available(iOS 10.0, *) {
            webView.scrollView.refreshControl = newRefreshControl
        } else {
            webView.scrollView.insertSubview(newRefreshControl, at: 0)
        }
        neemanRefreshControl = newRefreshControl
    }
    
    /**
     This action is called by the refresh control.
     */
    @objc open func refresh() {
        loadURL(rootURL)
    }

    /**
     This sets up progress view in the top of the view. If progressView is non-nil
     the we use that instead. If you want to create your own progress view you can override this
     method or you can set the outlet in interface builder.
     */
    open func setupProgressView() {
        if let _ = progressView {
            return
        }
        
        progressView = UIProgressView(progressViewStyle: .default)
        guard let progressView = progressView else {
            return
        }
        progressView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(progressView)
        
        let views = Dictionary(dictionaryLiteral: ("progressView", progressView))
        
        let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[progressView]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views)
        view.addConstraints(hConstraints)

        let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:[progressView(1)]",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views)
        view.addConstraints(vConstraints)
        
        let yConstraint = NSLayoutConstraint(item: progressView,
            attribute: .top,
            relatedBy: .equal,
            toItem: topLayoutGuide,
            attribute: .bottom,
            multiplier: 1,
            constant: 0)
        view.addConstraint(yConstraint)
    }

    /**
     This sets up an activity indicator in the center of the screen. If activityIndicator is non-nil
     the we use that instead. If you want to create your own activityIndicator you can override this 
     method or you can set the outlet in interface builder.
     */
    open func setupActivityIndicator() {
        if let _ = activityIndicator {
            return
        }
        
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 8.5, y: 8.5, width: 24, height: 24))
        guard let activityIndicator = activityIndicator else {
            return
        }
        
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.isUserInteractionEnabled = false
        activityIndicator.translatesAutoresizingMaskIntoConstraints = true
        activityIndicator.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleLeftMargin]

        let holder = createActivityIndicatorHolder()
        holder.addSubview(activityIndicator)
        view.addSubview(holder)
        
        activityIndicator.startAnimating()
        
        holder.translatesAutoresizingMaskIntoConstraints = false
        let xCenterConstraint = NSLayoutConstraint(item: holder,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: view,
            attribute: .centerX,
            multiplier: 1,
            constant: 0)
        view.addConstraint(xCenterConstraint)

        let yConstraint = NSLayoutConstraint(item: holder,
            attribute: .bottom,
            relatedBy: .equal,
            toItem: bottomLayoutGuide,
            attribute: .top,
            multiplier: 1,
            constant: -65)
        view.addConstraint(yConstraint)
        
        let widthConstraint = NSLayoutConstraint(item: holder, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: holder.frame.width)
        let heightConstraint = NSLayoutConstraint(item: holder, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: holder.frame.height)
        view.addConstraints([widthConstraint, heightConstraint])
    }
    
    fileprivate func createActivityIndicatorHolder() -> UIView {
        let holder = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        holder.layer.cornerRadius = holder.frame.height / 2
        holder.backgroundColor = .white
        holder.layer.shadowColor = UIColor.black.cgColor
        let sizeOf1 = 1.0/UIScreen.main.scale
        holder.layer.shadowOffset = CGSize(width: sizeOf1, height: sizeOf1)
        holder.layer.masksToBounds = false
        holder.layer.shadowRadius = 0.5
        holder.layer.shadowOpacity = 0.25
        holder.isUserInteractionEnabled = false
        activityIndicatorHolder = holder
        
        return holder
    }
    
    /**
     This is called when there is a posibly a need to update the status of the activity indicator. 
     For example, when the web view loading status has changed.
     
     - parameter webView: The web view whose activity we should indicate.<commit_msg>Adjust the position of the activity indicator.<commit_after>import UIKit
import WebKit

extension WebViewController {

    /**
     Setup the refresh control for reloading the web view.
     */
    open func setupRefreshControl() {
        let newRefreshControl = UIRefreshControl()
//        newRefreshControl.attributedTitle = NSAttributedString(string: "")
// This is a hack to improve the way the large titles work. They were not returning to the origional position in many cases.
// Pulling down fast seemed to reproduce the problem the most.
// The refresh method is now called in the scrollViewDidEndDecelerating method.
//        newRefreshControl.addTarget(self, action: #selector(WebViewController.refresh), for: UIControlEvents.valueChanged)
        if #available(iOS 10.0, *) {
            webView.scrollView.refreshControl = newRefreshControl
        } else {
            webView.scrollView.insertSubview(newRefreshControl, at: 0)
        }
        neemanRefreshControl = newRefreshControl
    }
    
    /**
     This action is called by the refresh control.
     */
    @objc open func refresh() {
        loadURL(rootURL)
    }

    /**
     This sets up progress view in the top of the view. If progressView is non-nil
     the we use that instead. If you want to create your own progress view you can override this
     method or you can set the outlet in interface builder.
     */
    open func setupProgressView() {
        if let _ = progressView {
            return
        }
        
        progressView = UIProgressView(progressViewStyle: .default)
        guard let progressView = progressView else {
            return
        }
        progressView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(progressView)
        
        let views = Dictionary(dictionaryLiteral: ("progressView", progressView))
        
        let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[progressView]|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views)
        view.addConstraints(hConstraints)

        let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:[progressView(1)]",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: views)
        view.addConstraints(vConstraints)
        
        let yConstraint = NSLayoutConstraint(item: progressView,
            attribute: .top,
            relatedBy: .equal,
            toItem: topLayoutGuide,
            attribute: .bottom,
            multiplier: 1,
            constant: 0)
        view.addConstraint(yConstraint)
    }

    /**
     This sets up an activity indicator in the center of the screen. If activityIndicator is non-nil
     the we use that instead. If you want to create your own activityIndicator you can override this 
     method or you can set the outlet in interface builder.
     */
    open func setupActivityIndicator() {
        if let _ = activityIndicator {
            return
        }
        
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 8.5, y: 8.5, width: 24, height: 24))
        guard let activityIndicator = activityIndicator else {
            return
        }
        
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.isUserInteractionEnabled = false
        activityIndicator.translatesAutoresizingMaskIntoConstraints = true
        activityIndicator.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleLeftMargin]

        let holder = createActivityIndicatorHolder()
        holder.addSubview(activityIndicator)
        view.addSubview(holder)
        
        activityIndicator.startAnimating()
        
        holder.translatesAutoresizingMaskIntoConstraints = false
        let xCenterConstraint = NSLayoutConstraint(item: holder,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: view,
            attribute: .centerX,
            multiplier: 1,
            constant: 0)
        view.addConstraint(xCenterConstraint)

        let yConstraint = NSLayoutConstraint(item: holder,
            attribute: .bottom,
            relatedBy: .equal,
            toItem: bottomLayoutGuide,
            attribute: .top,
            multiplier: 1,
            constant: -25)
        view.addConstraint(yConstraint)
        
        let widthConstraint = NSLayoutConstraint(item: holder, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: holder.frame.width)
        let heightConstraint = NSLayoutConstraint(item: holder, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: holder.frame.height)
        view.addConstraints([widthConstraint, heightConstraint])
    }
    
    fileprivate func createActivityIndicatorHolder() -> UIView {
        let holder = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        holder.layer.cornerRadius = holder.frame.height / 2
        holder.backgroundColor = .white
        holder.layer.shadowColor = UIColor.black.cgColor
        let sizeOf1 = 1.0/UIScreen.main.scale
        holder.layer.shadowOffset = CGSize(width: sizeOf1, height: sizeOf1)
        holder.layer.masksToBounds = false
        holder.layer.shadowRadius = 0.5
        holder.layer.shadowOpacity = 0.25
        holder.isUserInteractionEnabled = false
        activityIndicatorHolder = holder
        
        return holder
    }
    
    /**
     This is called when there is a posibly a need to update the status of the activity indicator. 
     For example, when the web view loading status has changed.
     
     - parameter webView: The web view whose activity we should indicate.
{'fraction_non_alphanumeric': 0.07049084371602977, 'fraction_numerical': 0.006856760598712267, 'mean_word_length': 2.5114503816793894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4876629', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2999, 'n_words': 881}