<commit_before>//
//  BPManager.swift
//  Bellerophon
//
//  Created by <NAME> on 8/22/15.
//  Copyright (c) 2015 Prolific Interactive. All rights reserved.
//

import Foundation
import UIKit

public class BellerophonManager: NSObject {

    // MARK: Singleton instance

    /// Shared singleton instance of BellerophonManager
    public static let sharedInstance = BellerophonManager()
    override internal init() {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(stopTimer()), name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    // MARK: Public properties

    /// View to be shown when the kill switch is turned on. Must be set by the application using Bellerophon
    public var killSwitchView: UIView!

    /// BellerophonManager delegate
    public weak var delegate: BellerophonManagerDelegate?

    // MARK: internal properties
    internal lazy var killSwitchWindow: UIWindow = {
        let window = BellerophonHelperMethods.newWindow()
        window.windowLevel = UIWindowLevelAlert
        let rootViewController = UIViewController()
        rootViewController.view.addSubview(self.killSwitchView)
        window.rootViewController = rootViewController
        return window
    }()
    internal var requestPending = false
    internal var retryTimer: NSTimer?

    // MARK: Public Methods

    /**
     Retrieves and handles the app status from the AMS endpoint
     */
    public func checkAppStatus() {
        assert(killSwitchView != nil, "The kill switch view has to be defined.")

        if requestPending {
            return;
        }

        requestPending = true
        stopTimer()

        delegate?.bellerophonStatus(self) { status, error in
            self.requestPending = false
            if let status = status {
                self.handleAppStatus(status)
            } else {
                self.dismissKillSwitchIfNeeded()
            }
        }
    }

    /**
     Use this function to retrieve and handle app status when the app has background mode enabled.

     - parameter completionHandler: Completion handler
     */
    public func fetchAppStatus(completionHandler: (result: UIBackgroundFetchResult) -> ()) {
        delegate?.bellerophonStatus(self) { status, error in
            if let status = status {
                self.handleAppStatus(status)
                if status.apiInactive() {
                    // If the kill switch or queue-it is still active we don't need to update anything
                    completionHandler(result: .NoData)
                } else {
                    // If the kill switch and queue-it are back to normal, we can stop fetching
                    completionHandler(result: .NewData)
                }
            } else {
                // An error occurred
                completionHandler(result: .Failed)
            }
        }
    }

    // MARK: internal Methods

    internal func handleAppStatus(status: BellerophonObservable) {
        if status.apiInactive() {
            displayKillSwitch()
            startAutoChecking(status)
        } else if status.forceUpdate() {
            performForceUpdate()
        } else {
            dismissKillSwitchIfNeeded()
        }
    }

    internal func stopTimer() {
        retryTimer?.invalidate()
        retryTimer = nil
    }

    internal func performForceUpdate() {
        delegate?.shouldForceUpdate()
    }

    internal func displayKillSwitch() {
        if !killSwitchWindow.keyWindow {
            killSwitchView.frame = killSwitchWindow.bounds
            delegate?.bellerophonWillEngage?(self)
            // if there's no such keyWindow in the application, do not show killSwitchWindow
            if let _ = UIApplication.sharedApplication().keyWindow {
                killSwitchWindow.makeKeyAndVisible()
            }
        }
    }

    internal func dismissKillSwitchIfNeeded() {
        if killSwitchWindow.keyWindow {
            delegate?.bellerophonWillDisengage?(self)
            killSwitchWindow.hidden = true;
        }
    }

    internal func startAutoChecking(status: BellerophonObservable) {
        if retryTimer == nil {<commit_msg>Removed modification in the Bellerophon framework.<commit_after>//
//  BPManager.swift
//  Bellerophon
//
//  Created by <NAME> on 8/22/15.
//  Copyright (c) 2015 Prolific Interactive. All rights reserved.
//

import Foundation
import UIKit

public class BellerophonManager: NSObject {

    // MARK: Singleton instance

    /// Shared singleton instance of BellerophonManager
    public static let sharedInstance = BellerophonManager()
    override internal init() {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(stopTimer()), name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    // MARK: Public properties

    /// View to be shown when the kill switch is turned on. Must be set by the application using Bellerophon
    public var killSwitchView: UIView!

    /// BellerophonManager delegate
    public weak var delegate: BellerophonManagerDelegate?

    // MARK: internal properties
    internal lazy var killSwitchWindow: UIWindow = {
        let window = BellerophonHelperMethods.newWindow()
        window.windowLevel = UIWindowLevelAlert
        let rootViewController = UIViewController()
        rootViewController.view.addSubview(self.killSwitchView)
        window.rootViewController = rootViewController
        return window
    }()
    internal var requestPending = false
    internal var retryTimer: NSTimer?

    // MARK: Public Methods

    /**
     Retrieves and handles the app status from the AMS endpoint
     */
    public func checkAppStatus() {
        assert(killSwitchView != nil, "The kill switch view has to be defined.")

        if requestPending {
            return;
        }

        requestPending = true
        stopTimer()

        delegate?.bellerophonStatus(self) { status, error in
            self.requestPending = false
            if let status = status {
                self.handleAppStatus(status)
            } else {
                self.dismissKillSwitchIfNeeded()
            }
        }
    }

    /**
     Use this function to retrieve and handle app status when the app has background mode enabled.

     - parameter completionHandler: Completion handler
     */
    public func fetchAppStatus(completionHandler: (result: UIBackgroundFetchResult) -> ()) {
        delegate?.bellerophonStatus(self) { status, error in
            if let status = status {
                self.handleAppStatus(status)
                if status.apiInactive() {
                    // If the kill switch or queue-it is still active we don't need to update anything
                    completionHandler(result: .NoData)
                } else {
                    // If the kill switch and queue-it are back to normal, we can stop fetching
                    completionHandler(result: .NewData)
                }
            } else {
                // An error occurred
                completionHandler(result: .Failed)
            }
        }
    }

    // MARK: internal Methods

    internal func handleAppStatus(status: BellerophonObservable) {
        if status.apiInactive() {
            displayKillSwitch()
            startAutoChecking(status)
        } else if status.forceUpdate() {
            performForceUpdate()
        } else {
            dismissKillSwitchIfNeeded()
        }
    }

    internal func stopTimer() {
        retryTimer?.invalidate()
        retryTimer = nil
    }

    internal func performForceUpdate() {
        delegate?.shouldForceUpdate()
    }

    internal func displayKillSwitch() {
        if !killSwitchWindow.keyWindow {
            killSwitchView.frame = killSwitchWindow.bounds
            delegate?.bellerophonWillEngage?(self)
            killSwitchWindow.makeKeyAndVisible()
        }
    }

    internal func dismissKillSwitchIfNeeded() {
        if killSwitchWindow.keyWindow {
            delegate?.bellerophonWillDisengage?(self)
            killSwitchWindow.hidden = true;
        }
    }

    internal func startAutoChecking(status: BellerophonObservable) {
        if retryTimer == nil {
{'fraction_non_alphanumeric': 0.07426741013168822, 'fraction_numerical': 0.0021354846363744215, 'mean_word_length': 2.130337913108058, 'pattern_counts': {'":': 0, '<': 5, '<?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': '406448', 'n_tokens_mistral': 2164, 'n_tokens_neox': 2049, 'n_words': 588}