<commit_before>//
//  SpringBoardViewController.swift
//  UIPlayground
//
//  Created by <NAME> on 9/20/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import UIPlaygroundElements

public class SpringBoardViewController: UIViewController {
    
    var selectedAppButton: UIButton?
    
    var appIconLayoutInfoItems = [
        [
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
        ],
        [
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
        ],
        [
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
        ],
    ]
    
    public var wallpaperImage: UIImage? {
        didSet {
            view.layoutIfNeeded()
            wallpaperView.image = prepareWallpaperImage(image: wallpaperImage)
        }
    }
    
    let containerView = UIView()
    let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    var pageViewSubViewControllers = [UIViewController]()
    let dockView = SpringBoardDockView()
    let wallpaperView = UIImageView()
    let backAppIconView = SpringBoardAppIconViewCell()
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        // Have the presented view controller specify the status bar style
        if let presentedViewController = presentedViewController, !presentedViewController.isBeingDismissed {
            return presentedViewController.preferredStatusBarStyle
        }
        
        return .lightContent
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    public init() {
        super.init(nibName: nil, bundle: nil)
        
        title = "Spring Board"
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    override public func viewDidLoad() {
        view.clipsToBounds = true
        
        let appCollectionLayout = SpringBoardAppCollectionLayout(screen: UIScreen.main)
        pageViewSubViewControllers = appIconLayoutInfoItems.map({ (appInfoItems) -> SpringBoardAppCollectionViewController in
            let controller = SpringBoardAppCollectionViewController(appInfoItems: appInfoItems, appCollectionLayout: appCollectionLayout)
            controller.delegate = self
            
            return controller
        })
        pageViewController.setViewControllers([pageViewSubViewControllers[0]], direction: .forward, animated: false, completion: nil)
        pageViewController.dataSource = self
        
        // Add wallpaper view
        view.addSubview(wallpaperView)
        wallpaperView.translatesAutoresizingMaskIntoConstraints = false
        wallpaperView.anchorConstraintsToFitSuperview()
        wallpaperView.contentMode = .scaleAspectFill
        
        // Add dock view
        view.addSubview(dockView)
        dockView.translatesAutoresizingMaskIntoConstraints = false
        dockView.heightAnchor.constraint(equalToConstant: 96).isActive = true
        dockView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        dockView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        dockView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        // Add container view for app collection
        view.addSubview(containerView)
        containerView.isOpaque = false
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.anchorConstraintsToFitSuperview()
        
        // Add page view
        pageViewController.willMove(toParentViewController: self)
        containerView.addSubview(pageViewController.view)
        addChildViewController(pageViewController)
        pageViewController.didMove(toParentViewController: self)
        pageViewController.view.translatesAutoresizingMaskIntoConstraints = false
        pageViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        pageViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        pageViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        
        // Add back app icon
        containerView.addSubview(backAppIconView)
        backAppIconView.appIconImage = UIImage(named: "BackIcon", inBundleForObject: self)!
        backAppIconView.appNameLabel.text = "Back"
        backAppIconView.appIconButtonView.addTarget(self, action: #selector(backButtonSelectedAction), for: .touchUpInside)
        backAppIconView.translatesAutoresizingMaskIntoConstraints = false
        backAppIconView.centerXAnchor.constraint(equalTo: dockView.centerXAnchor).isActive = true
        backAppIconView.bottomAnchor.constraint(equalTo: dockView.bottomAnchor, constant: -4).isActive = true
        
        // Constrain top of dock to bottom of page view
        dockView.topAnchor.constraint(equalTo: pageViewController.view.bottomAnchor, constant: appCollectionLayout.pageControlOfffset).isActive = true
        
        wallpaperImage = UIImage(named: "BackgroundWallpaper", inBundleForObject: self)
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setNeedsStatusBarAppearanceUpdate()
    }
    
    public override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        setNeedsStatusBarAppearanceUpdate()
    }
    
    public override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    // MARK: - Actions
    
    func backButtonSelectedAction(sender: UIButton) {
        let _ = (splitViewController?.viewControllers.first as? UINavigationController)?.popViewController(animated: true)
    }
    
    // MARK: - Working with wallpaper
    
    private func prepareWallpaperImage(image: UIImage?) -> UIImage? {
        guard let image = image else {
            return nil
        }

        let imageRect = CGRect(origin: .zero, size: image.size)
        
        return UIGraphicsImageRenderer(size: imageRect.size, scale: 0, opaque: true).image { (context) in
            // Resize image so that it's just big enough for the wallpaper view
            image.draw(in: imageRect)
            
            // Draw a transparent dark overlay to dim the image<commit_msg>Fix back button on spring board for iPads.<commit_after>//
//  SpringBoardViewController.swift
//  UIPlayground
//
//  Created by <NAME> on 9/20/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import UIPlaygroundElements

public class SpringBoardViewController: UIViewController {
    
    var selectedAppButton: UIButton?
    
    var appIconLayoutInfoItems = [
        [
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
        ],
        [
            SpringBoardAppInfo(appName: "UIPlayground", image: UIImage(named: "AppCard-UIPlayground-Icon")!),
            SpringBoardAppInfo(appName: "Overcast", image: UIImage(named: "AppCard-Overcast-Icon")!),
        ],
        [
            SpringBoardAppInfo(appName: "DavisTrans", image: UIImage(named: "AppCard-DavisTrans-Icon")!),
        ],
    ]
    
    public var wallpaperImage: UIImage? {
        didSet {
            view.layoutIfNeeded()
            wallpaperView.image = prepareWallpaperImage(image: wallpaperImage)
        }
    }
    
    let containerView = UIView()
    let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    var pageViewSubViewControllers = [UIViewController]()
    let dockView = SpringBoardDockView()
    let wallpaperView = UIImageView()
    let backAppIconView = SpringBoardAppIconViewCell()
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        // Have the presented view controller specify the status bar style
        if let presentedViewController = presentedViewController, !presentedViewController.isBeingDismissed {
            return presentedViewController.preferredStatusBarStyle
        }
        
        return .lightContent
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    public init() {
        super.init(nibName: nil, bundle: nil)
        
        title = "Spring Board"
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    override public func viewDidLoad() {
        view.clipsToBounds = true
        
        let appCollectionLayout = SpringBoardAppCollectionLayout(screen: UIScreen.main)
        pageViewSubViewControllers = appIconLayoutInfoItems.map({ (appInfoItems) -> SpringBoardAppCollectionViewController in
            let controller = SpringBoardAppCollectionViewController(appInfoItems: appInfoItems, appCollectionLayout: appCollectionLayout)
            controller.delegate = self
            
            return controller
        })
        pageViewController.setViewControllers([pageViewSubViewControllers[0]], direction: .forward, animated: false, completion: nil)
        pageViewController.dataSource = self
        
        // Add wallpaper view
        view.addSubview(wallpaperView)
        wallpaperView.translatesAutoresizingMaskIntoConstraints = false
        wallpaperView.anchorConstraintsToFitSuperview()
        wallpaperView.contentMode = .scaleAspectFill
        
        // Add dock view
        view.addSubview(dockView)
        dockView.translatesAutoresizingMaskIntoConstraints = false
        dockView.heightAnchor.constraint(equalToConstant: 96).isActive = true
        dockView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        dockView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        dockView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        // Add container view for app collection
        view.addSubview(containerView)
        containerView.isOpaque = false
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.anchorConstraintsToFitSuperview()
        
        // Add page view
        pageViewController.willMove(toParentViewController: self)
        containerView.addSubview(pageViewController.view)
        addChildViewController(pageViewController)
        pageViewController.didMove(toParentViewController: self)
        pageViewController.view.translatesAutoresizingMaskIntoConstraints = false
        pageViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        pageViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        pageViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        
        // Add back app icon
        containerView.addSubview(backAppIconView)
        backAppIconView.appIconImage = UIImage(named: "BackIcon", inBundleForObject: self)!
        backAppIconView.appNameLabel.text = "Back"
        backAppIconView.appIconButtonView.addTarget(self, action: #selector(backButtonSelectedAction), for: .touchUpInside)
        backAppIconView.translatesAutoresizingMaskIntoConstraints = false
        backAppIconView.centerXAnchor.constraint(equalTo: dockView.centerXAnchor).isActive = true
        backAppIconView.bottomAnchor.constraint(equalTo: dockView.bottomAnchor, constant: -4).isActive = true
        
        // Constrain top of dock to bottom of page view
        dockView.topAnchor.constraint(equalTo: pageViewController.view.bottomAnchor, constant: appCollectionLayout.pageControlOfffset).isActive = true
        
        wallpaperImage = UIImage(named: "BackgroundWallpaper", inBundleForObject: self)
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setNeedsStatusBarAppearanceUpdate()
    }
    
    public override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        setNeedsStatusBarAppearanceUpdate()
    }
    
    public override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    // MARK: - Actions
    
    func backButtonSelectedAction(sender: UIButton) {
        if splitViewController!.isCollapsed {
            let _ = (splitViewController?.viewControllers.first as? UINavigationController)?.popViewController(animated: true)
        }
        else {
            let btn = splitViewController!.displayModeButtonItem
            let _ = btn.target?.perform(btn.action, with: btn)            
        }
    }
    
    // MARK: - Working with wallpaper
    
    private func prepareWallpaperImage(image: UIImage?) -> UIImage? {
        guard let image = image else {
            return nil
        }

        let imageRect = CGRect(origin: .zero, size: image.size)
        
        return UIGraphicsImageRenderer(size: imageRect.size, scale: 0, opaque: true).image { (context) in
            // Resize image so that it's just big enough for the wallpaper view
            image.draw(in: imageRect)
            
            // Draw a transparent dark overlay to dim the image
{'fraction_non_alphanumeric': 0.09238183718499593, 'fraction_numerical': 0.0016258274300313552, 'mean_word_length': 3.177298083919476, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1551025', 'n_tokens_mistral': 4736, 'n_tokens_neox': 4591, 'n_words': 898}