<commit_before>//
//  NFX.swift
//  netfox
//
//  Copyright © 2015 kasketis. All rights reserved.
//

import Foundation
import UIKit

let nfxVersion = "0.1.8"

@objc
public class NFX: NSObject
{
    
    // swiftSharedInstance is not accessible from ObjC
    class var swiftSharedInstance: NFX
    {
        struct Singleton
        {
            static let instance = NFX()
        }
        return Singleton.instance
    }
    
    // the sharedInstance class method can be reached from ObjC
    public class func sharedInstance() -> NFX
    {
        return NFX.swiftSharedInstance
    }
    
    @objc public enum ENFXGesture: Int
    {
        case shake
        case custom
        
        func name() -> String {
            switch self {
            case .shake: return "shake"
            case .custom: return "custom"
            }
        }
    }
    
    var started: Bool = false
    var presented: Bool = false
    var selectedGesture: ENFXGesture = .shake
    var ignoredURLs = [String]()
    
    @objc public func start()
    {
        self.started = true
        NSURLProtocol.registerClass(NFXProtocol)
        print("netfox \(nfxVersion) - [https://github.com/kasketis/netfox]: Started!")
        clearOldData()
    }
    
    func motionDetected()
    {
        if self.started {
            if self.presented {
                hideNFX()
            } else {
                showNFX()
            }
        }
    }
    
    @objc public func setGesture(gesture: ENFXGesture)
    {
        self.selectedGesture = gesture
    }
    
    @objc public func show()
    {
        if (self.started) && (self.selectedGesture == .custom) {
            showNFX()
        } else {
            print("netfox \(nfxVersion) - [ERROR]: Please call start() and setGesture(.custom) first")
        }
    }
    
    @objc public func hide()
    {
        if (self.started) && (self.selectedGesture == .custom) {
            hideNFX()
        } else {
            print("netfox \(nfxVersion) - [ERROR]: Please call start() and setGesture(.custom) first")
        }
    }
    
    @objc public func ignoreURL(url: String)
    {
        self.ignoredURLs.append(url)
    }
    
    private func showNFX()
    {
        if self.presented {
            return
        }
        
        var navigationController: UINavigationController?

        var listController: NFXListController
        listController = NFXListController()
        
        navigationController = UINavigationController(rootViewController: listController)
        navigationController!.navigationBar.translucent = false
        navigationController!.navigationBar.tintColor = UIColor.init(netHex: 0xec5e28)
        navigationController!.navigationBar.barTintColor = UIColor.init(netHex: 0xccc5b9)
        navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.init(netHex: 0xec5e28)]
        
        presentingViewController?.presentViewController(navigationController!, animated: true, completion: { () -> Void in
            self.presented = true
        })


    }
    
    private var presentingViewController: UIViewController? {
        let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
        return rootViewController?.presentedViewController ?? rootViewController
    }
    
    private func hideNFX()
    {
        if !self.presented {
            return
        }
        
        presentingViewController?.dismissViewControllerAnimated(true, completion: { () -> Void in
            self.presented = false
        })
    }
    
    private func clearOldData()
    {
        do {<commit_msg>A bug exists where it's possible to end up with multiple copies of the debug view controller presented.<commit_after>//
//  NFX.swift
//  netfox
//
//  Copyright © 2015 kasketis. All rights reserved.
//

import Foundation
import UIKit

let nfxVersion = "0.1.8"

@objc
public class NFX: NSObject
{
    
    // swiftSharedInstance is not accessible from ObjC
    class var swiftSharedInstance: NFX
    {
        struct Singleton
        {
            static let instance = NFX()
        }
        return Singleton.instance
    }
    
    // the sharedInstance class method can be reached from ObjC
    public class func sharedInstance() -> NFX
    {
        return NFX.swiftSharedInstance
    }
    
    @objc public enum ENFXGesture: Int
    {
        case shake
        case custom
        
        func name() -> String {
            switch self {
            case .shake: return "shake"
            case .custom: return "custom"
            }
        }
    }
    
    var started: Bool = false
    var presented: Bool = false
    var selectedGesture: ENFXGesture = .shake
    var ignoredURLs = [String]()
    
    @objc public func start()
    {
        self.started = true
        NSURLProtocol.registerClass(NFXProtocol)
        print("netfox \(nfxVersion) - [https://github.com/kasketis/netfox]: Started!")
        clearOldData()
    }
    
    func motionDetected()
    {
        if self.started {
            if self.presented {
                hideNFX()
            } else {
                showNFX()
            }
        }
    }
    
    @objc public func setGesture(gesture: ENFXGesture)
    {
        self.selectedGesture = gesture
    }
    
    @objc public func show()
    {
        if (self.started) && (self.selectedGesture == .custom) {
            showNFX()
        } else {
            print("netfox \(nfxVersion) - [ERROR]: Please call start() and setGesture(.custom) first")
        }
    }
    
    @objc public func hide()
    {
        if (self.started) && (self.selectedGesture == .custom) {
            hideNFX()
        } else {
            print("netfox \(nfxVersion) - [ERROR]: Please call start() and setGesture(.custom) first")
        }
    }
    
    @objc public func ignoreURL(url: String)
    {
        self.ignoredURLs.append(url)
    }
    
    private func showNFX()
    {
        if self.presented {
            return
        }
        
        var navigationController: UINavigationController?

        var listController: NFXListController
        listController = NFXListController()
        
        navigationController = UINavigationController(rootViewController: listController)
        navigationController!.navigationBar.translucent = false
        navigationController!.navigationBar.tintColor = UIColor.init(netHex: 0xec5e28)
        navigationController!.navigationBar.barTintColor = UIColor.init(netHex: 0xccc5b9)
        navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.init(netHex: 0xec5e28)]
        
        self.presented = true
        presentingViewController?.presentViewController(navigationController!, animated: true, completion: nil)
    }
    
    private var presentingViewController: UIViewController? {
        let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
        return rootViewController?.presentedViewController ?? rootViewController
    }
    
    private func hideNFX()
    {
        if !self.presented {
            return
        }
        
        presentingViewController?.dismissViewControllerAnimated(true, completion: { () -> Void in
            self.presented = false
        })
    }
    
    private func clearOldData()
    {
        do {
{'fraction_non_alphanumeric': 0.08758134490238612, 'fraction_numerical': 0.004880694143167028, 'mean_word_length': 1.9425608296769046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 2, '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': '6912122', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2010, 'n_words': 472}