<commit_before>import WMF

extension UIViewController {
    @objc class func wmf_viewControllerFromReferencePanelsStoryboard() -> Self {
        return self.wmf_viewControllerFromStoryboardNamed("WMFReferencePanels")
    }
}

@objc protocol WMFReferencePageViewAppearanceDelegate : NSObjectProtocol {
    func referencePageViewControllerWillAppear(_ referencePageViewController: WMFReferencePageViewController)
    func referencePageViewControllerWillDisappear(_ referencePageViewController: WMFReferencePageViewController)
}


class WMFReferencePageViewController: UIViewController, UIPageViewControllerDataSource, Themeable {
    @objc var lastClickedReferencesIndex:Int = 0
    @objc var lastClickedReferencesGroup = [WMFReference]()
    
    @objc weak internal var appearanceDelegate: WMFReferencePageViewAppearanceDelegate?
    
    @objc public var pageViewController = UIPageViewController()
    @IBOutlet fileprivate var containerView: UIView!
    
    var theme = Theme.standard
    
    func apply(theme: Theme) {
        self.theme = theme
        backgroundView.apply(theme: theme)
        guard viewIfLoaded != nil else {
            return
        }
    }

    fileprivate lazy var pageControllers: [UIViewController] = {
        var controllers:[UIViewController] = []
        
        for reference in self.lastClickedReferencesGroup {
            let panel = WMFReferencePanelViewController.wmf_viewControllerFromReferencePanelsStoryboard()
            panel.apply(theme: self.theme)
            panel.reference = reference
            controllers.append(panel)
        }
        
        return controllers
    }()
    
    @objc lazy var backgroundView: WMFReferencePageBackgroundView = {
        return WMFReferencePageBackgroundView()
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        pageViewController.willMove(toParentViewController: self)
        pageViewController.view.frame = containerView.bounds
        pageViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        containerView.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
        
        pageViewController.dataSource = self
        
        let direction:UIPageViewControllerNavigationDirection = UIApplication.shared.wmf_isRTL ? .forward : .reverse
        
        let initiallyVisibleController = pageControllers[lastClickedReferencesIndex]
        
        pageViewController.setViewControllers([initiallyVisibleController], direction: direction, animated: true, completion: nil)
        
        addBackgroundView()

        if let scrollView = view.wmf_firstSubviewOfType(UIScrollView.self) {
            scrollView.clipsToBounds = false
        }
        
        apply(theme: theme)
    }
    
    fileprivate func addBackgroundView() {
        view.addSubview(backgroundView)
        view.sendSubview(toBack: backgroundView)<commit_msg>Fix dismissing the reference popover<commit_after>import WMF

extension UIViewController {
    @objc class func wmf_viewControllerFromReferencePanelsStoryboard() -> Self {
        return self.wmf_viewControllerFromStoryboardNamed("WMFReferencePanels")
    }
}

@objc protocol WMFReferencePageViewAppearanceDelegate : NSObjectProtocol {
    func referencePageViewControllerWillAppear(_ referencePageViewController: WMFReferencePageViewController)
    func referencePageViewControllerWillDisappear(_ referencePageViewController: WMFReferencePageViewController)
}


class WMFReferencePageViewController: UIViewController, UIPageViewControllerDataSource, Themeable {
    @objc var lastClickedReferencesIndex:Int = 0
    @objc var lastClickedReferencesGroup = [WMFReference]()
    
    @objc weak internal var appearanceDelegate: WMFReferencePageViewAppearanceDelegate?
    
    @objc public var pageViewController = UIPageViewController()
    @IBOutlet fileprivate var containerView: UIView!
    
    var theme = Theme.standard
    
    func apply(theme: Theme) {
        self.theme = theme
        backgroundView.apply(theme: theme)
        guard viewIfLoaded != nil else {
            return
        }
    }

    fileprivate lazy var pageControllers: [UIViewController] = {
        var controllers:[UIViewController] = []
        
        for reference in self.lastClickedReferencesGroup {
            let panel = WMFReferencePanelViewController.wmf_viewControllerFromReferencePanelsStoryboard()
            panel.apply(theme: self.theme)
            panel.reference = reference
            controllers.append(panel)
        }
        
        return controllers
    }()
    
    @objc lazy var backgroundView: WMFReferencePageBackgroundView = {
        return WMFReferencePageBackgroundView()
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        pageViewController.willMove(toParentViewController: self)
        pageViewController.view.frame = containerView.bounds
        pageViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.addChildViewController(pageViewController)
        containerView.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
        
        pageViewController.dataSource = self
        
        let direction:UIPageViewControllerNavigationDirection = UIApplication.shared.wmf_isRTL ? .forward : .reverse
        
        let initiallyVisibleController = pageControllers[lastClickedReferencesIndex]
        
        pageViewController.setViewControllers([initiallyVisibleController], direction: direction, animated: true, completion: nil)
        
        addBackgroundView()

        if let scrollView = view.wmf_firstSubviewOfType(UIScrollView.self) {
            scrollView.clipsToBounds = false
        }
        
        apply(theme: theme)
    }
    
    fileprivate func addBackgroundView() {
        view.addSubview(backgroundView)
        view.sendSubview(toBack: backgroundView)
{'fraction_non_alphanumeric': 0.06297229219143577, 'fraction_numerical': 0.00033585222502099076, 'mean_word_length': 3.306579898770788, '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': '7583909', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1455, 'n_words': 286}