<commit_before>//
//  CocoaBar.swift
//  CocoaBar Example
//
//  Created by <NAME> on 23/05/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import PureLayout

// MARK: Notifications
private let CocoaBarShowNotification: String = "CocoaBarShowNotification"
private let CocoaBarHideNotification: String = "CocoaBarHideNotification"

class CocoaBar: UIView {
    
    // MARK: Defaults
    let cocoaBarDefaultHeight: Float = 88.0
    
    // MARK: Variables
    var height: Float
    
    private var rootWindow: UIWindow?
    
    // MARK: Init
    
    convenience init(window: UIWindow?) {
        self.init(window: window, height: nil)
    }
    
    init(window: UIWindow?, height: Float?) {
        
        if let height = height {
            self.height = height
        } else {
            self.height = self.cocoaBarDefaultHeight
        }
        
        self.rootWindow = window
        super.init(frame: CGRectZero)
        
        self.registerForNotifications()
    }
    
    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        self.height = self.cocoaBarDefaultHeight
        
        super.init(coder: aDecoder)
        
        self.registerForNotifications()
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: Private
    
    private func registerForNotifications() {
        
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(showNotificationReceived), name: CocoaBarShowNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(hideNotificationReceived), name: CocoaBarHideNotification, object: nil)
        
        notificationCenter.addObserver(self, selector: #selector(windowDidBecomeVisible), name: UIWindowDidBecomeVisibleNotification, object: nil)
    }
    
    private func setUpIfRequired() {
        if let rootWindow = self.rootWindow {
            if self.superview == nil {
                
                // add bar to display view controller
                rootWindow.addSubview(self)
                self.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero, excludingEdge: ALEdge.Top)
                self.autoSetDimension(ALDimension.Height, toSize: CGFloat(self.height))
            }
        }
    }
    
    private func bringBarToFront() {
        if let rootWindow = self.rootWindow {
            rootWindow.bringSubviewToFront(self)
        }
    }
    
    // MARK: Public
    
    func show() {
        self.setUpIfRequired()
        
        self.backgroundColor = UIColor.redColor()
    }
    
    func hide() {
        
    }
    
    // MARK: Class
    
    class func show() {
        NSNotificationCenter.defaultCenter().postNotificationName(CocoaBarShowNotification, object: nil)<commit_msg>Add background style to bar<commit_after>//
//  CocoaBar.swift
//  CocoaBar Example
//
//  Created by <NAME> on 23/05/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import PureLayout

// MARK: Notifications
private let CocoaBarShowNotification: String = "CocoaBarShowNotification"
private let CocoaBarHideNotification: String = "CocoaBarHideNotification"

class CocoaBar: UIView {
    
    enum BackgroundStyle {
        case SolidColor
        case BlurExtraLight
        case BlurLight
        case BlurDark
        case Custom
    }
    
    // MARK: Defaults
    let cocoaBarDefaultHeight: Float = 88.0
    
    // MARK: Variables
    private var height: Float
    private var rootWindow: UIWindow?
    private var backgroundViewContainer: UIView?
    
    private var _backgroundView: UIView?
    
    // MARK: Properties
    
    var backgroundStyle: BackgroundStyle = .BlurExtraLight {
        willSet {
            if newValue != self.backgroundStyle {
                self.updateBackgroundStyle(newValue)
            }
        }
    }
    
    var backgroundView: UIView? {
        get {
            return _backgroundView
        }
    }
    
    // MARK: Init
    
    convenience init(window: UIWindow?) {
        self.init(window: window, height: nil)
    }
    
    init(window: UIWindow?, height: Float?) {
        
        if let height = height {
            self.height = height
        } else {
            self.height = self.cocoaBarDefaultHeight
        }
        
        self.rootWindow = window
        super.init(frame: CGRectZero)
        
        self.registerForNotifications()
    }
    
    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        self.height = self.cocoaBarDefaultHeight
        
        super.init(coder: aDecoder)
        
        self.registerForNotifications()
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: Private
    
    private func registerForNotifications() {
        
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(showNotificationReceived), name: CocoaBarShowNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(hideNotificationReceived), name: CocoaBarHideNotification, object: nil)
        
        notificationCenter.addObserver(self, selector: #selector(windowDidBecomeVisible), name: UIWindowDidBecomeVisibleNotification, object: nil)
    }
    
    private func setUpIfRequired() {
        if let rootWindow = self.rootWindow {
            if self.superview == nil {
                
                // add bar to display view controller
                rootWindow.addSubview(self)
                self.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero, excludingEdge: ALEdge.Top)
                self.autoSetDimension(ALDimension.Height, toSize: CGFloat(self.height))
            }
        }
        
        // set up view components
        if self.backgroundViewContainer == nil {
            self.setUpComponents()
        }
    }
    
    private func setUpComponents() {
        
        // add background view container
        let backgroundViewContainer = UIView()
        self.addSubview(backgroundViewContainer)
        backgroundViewContainer.autoPinEdgesToSuperviewEdges()
        self.backgroundViewContainer = backgroundViewContainer
        self.updateBackgroundStyle(self.backgroundStyle)
        
    }
    
    private func bringBarToFront() {
        if let rootWindow = self.rootWindow {
            rootWindow.bringSubviewToFront(self)
        }
    }
    
    private func updateBackgroundStyle(newStyle: BackgroundStyle) {
        if let backgroundViewContainer = self.backgroundViewContainer {
            
            // clear subviews
            for view in backgroundViewContainer.subviews{
                view.removeFromSuperview()
            }
            _backgroundView = nil
            
            switch newStyle {
                
            case .BlurExtraLight, .BlurLight, .BlurDark:
                self.backgroundViewContainer?.backgroundColor = UIColor.clearColor()
                
                var style: UIBlurEffectStyle
                switch newStyle {
                case .BlurExtraLight: style = UIBlurEffectStyle.ExtraLight
                case .BlurDark: style = UIBlurEffectStyle.Dark
                default: style = UIBlurEffectStyle.Light
                }
                
                // add blur view
                let blurEffect = UIBlurEffect(style: style)
                let visualEffectView = UIVisualEffectView(effect: blurEffect)
                
                self.backgroundViewContainer?.addSubview(visualEffectView)
                visualEffectView.autoPinEdgesToSuperviewEdges()
                
            case .Custom:
                
                // create custom background view
                let backgroundView = UIView()
                backgroundViewContainer.addSubview(backgroundView)
                backgroundView.autoPinEdgesToSuperviewEdges()
                _backgroundView = backgroundView
                
            default:()
            }
        }
    }
    
    // MARK: Public
    
    func show() {
        self.setUpIfRequired()
        
    }
    
    func hide() {
        
    }
    
    // MARK: Class
    
    class func show() {
        NSNotificationCenter.defaultCenter().postNotificationName(CocoaBarShowNotification, object: nil)
{'fraction_non_alphanumeric': 0.07518619222130275, 'fraction_numerical': 0.003546518501004847, 'mean_word_length': 1.9467084639498433, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6919998', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2190, 'n_words': 464}