<commit_before>//
//  RHCAppDelegate.swift
//  VindsidenApp
//
//  Created by <NAME> on 16/10/14.
//  Copyright (c) 2014 RHC. All rights reserved.
//

import UIKit
import VindsidenKit


@UIApplicationMain
class RHCAppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let _formatterQueue = dispatch_queue_create("formatter queue", nil);

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true

        self.window?.tintColor = UIColor.vindsidenGloablTintColor()

        if Datamanager.sharedManager().sharedDefaults.integerForKey("selectedUnit") == 0 {
            Datamanager.sharedManager().sharedDefaults.setInteger(SpeedConvertion.ToMetersPerSecond.rawValue, forKey: "selectedUnit")
            Datamanager.sharedManager().sharedDefaults.synchronize()
        }


        Datamanager.sharedManager().cleanupPlots()

        application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

        return true
    }

    func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

        if let options = launchOptions {
            if let url = options[UIApplicationLaunchOptionsURLKey] as? NSURL {
                if let range = url.host?.rangeOfString("station", options: .CaseInsensitiveSearch) {
                    return true
                } else {
                    return false
                }
            }
        }

        return true
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

        if let range = url.host?.rangeOfString("station", options: .CaseInsensitiveSearch) {
            return openLaunchOptionsURL(url)
        }

        return false
    }

    func applicationWillResignActive(application: UIApplication) {
        RHEVindsidenAPIClient.defaultManager().background = true
    }

    func applicationDidEnterBackground(application: UIApplication) {
    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
        RHEVindsidenAPIClient.defaultManager().background = false
    }

    func applicationWillTerminate(application: UIApplication) {
        Datamanager.sharedManager().saveContext()
    }


    func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        let nc = self.window?.rootViewController as UINavigationController
        if let vc = nc.viewControllers.first as? RHCViewController {
            vc.updateContentWithCompletionHandler(completionHandler)
        }
    }

    // MARK: - 

    func openLaunchOptionsURL( url: NSURL) -> Bool {
        let ident = url.pathComponents.last as String
        var station: CDStation?

        if let stationId = ident.toInt() {
            station = CDStation.existingStation(stationId, inManagedObjectContext: Datamanager.sharedManager().managedObjectContext)
        } else {
            station = CDStation.searchForStation(ident, inManagedObjectContext: Datamanager.sharedManager().managedObjectContext)
        }

        if  let found = station {
            if found.isHidden.boolValue == true {
                found.managedObjectContext?.performBlockAndWait({ () -> Void in
                    found.isHidden = false
                    found.managedObjectContext?.save(nil)
                })
            }
        } else {
            return false
        }


        let nc = self.window?.rootViewController as? UINavigationController
        let controller = nc?.viewControllers.first as? RHCViewController

        if let pc = nc?.presentedViewController {
            nc?.dismissViewControllerAnimated(false, completion: { () -> Void in
                controller!.scrollToStation(station!)
            })
        } else {
            controller!.scrollToStation(station!)
        }

        return true
    }

    // MARK: - NSUserActivity

    func application(application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool {
        return true
    }


    func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool {
        NSLog("Activity: \(userActivity.userInfo)")

        if let userInfo = userActivity.userInfo {
            if let urlString = userInfo["urlToActivate"] as? String {
                let url = NSURL(string: urlString)
                let success = openLaunchOptionsURL(url!)
                return success
            }
        }
        return false
    }

    
    func application(application: UIApplication, didFailToContinueUserActivityWithType userActivityType: String, error: NSError) {
        print("did fail to continue: \(userActivityType), \(error)")
    }
}<commit_msg>Updates logging and added primaryViewController<commit_after>//
//  RHCAppDelegate.swift
//  VindsidenApp
//
//  Created by <NAME> on 16/10/14.
//  Copyright (c) 2014 RHC. All rights reserved.
//

import UIKit
import VindsidenKit


func DLOG( message: String, file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__ ) {
//    #if Debug
//        NSLog("([\(file.lastPathComponent) \(function)] line: \(line)) \(message)")
//    #endif
}


@UIApplicationMain
class RHCAppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let _formatterQueue = dispatch_queue_create("formatter queue", nil);

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true

        self.window?.tintColor = UIColor.vindsidenGloablTintColor()

        if Datamanager.sharedManager().sharedDefaults.integerForKey("selectedUnit") == 0 {
            Datamanager.sharedManager().sharedDefaults.setInteger(SpeedConvertion.ToMetersPerSecond.rawValue, forKey: "selectedUnit")
            Datamanager.sharedManager().sharedDefaults.synchronize()
        }


        Datamanager.sharedManager().cleanupPlots()

        application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

        return true
    }

    func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

        if let options = launchOptions {
            if let url = options[UIApplicationLaunchOptionsURLKey] as? NSURL {
                if let range = url.host?.rangeOfString("station", options: .CaseInsensitiveSearch) {
                    return true
                } else {
                    return false
                }
            }
        }

        return true
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

        if let range = url.host?.rangeOfString("station", options: .CaseInsensitiveSearch) {
            return openLaunchOptionsURL(url)
        }

        return false
    }

    func applicationWillResignActive(application: UIApplication) {
        RHEVindsidenAPIClient.defaultManager().background = true
    }

    func applicationDidEnterBackground(application: UIApplication) {
    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
        RHEVindsidenAPIClient.defaultManager().background = false
    }

    func applicationWillTerminate(application: UIApplication) {
        Datamanager.sharedManager().saveContext()
    }


    func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if let vc = primaryViewController() {
            vc.updateContentWithCompletionHandler(completionHandler)
        }
    }

    // MARK: - 

    func openLaunchOptionsURL( url: NSURL) -> Bool {
        let ident = url.pathComponents.last as String
        var station: CDStation?

        if let stationId = ident.toInt() {
            station = CDStation.existingStation(stationId, inManagedObjectContext: Datamanager.sharedManager().managedObjectContext)
        } else {
            station = CDStation.searchForStation(ident, inManagedObjectContext: Datamanager.sharedManager().managedObjectContext)
        }

        if  let found = station {
            if found.isHidden.boolValue == true {
                found.managedObjectContext?.performBlockAndWait({ () -> Void in
                    found.isHidden = false
                    found.managedObjectContext?.save(nil)
                })
            }
        } else {
            return false
        }


        let nc = self.window?.rootViewController as? UINavigationController
        let controller = primaryViewController()

        if let pc = nc?.presentedViewController {
            nc?.dismissViewControllerAnimated(false, completion: { () -> Void in
                controller!.scrollToStation(station!)
            })
        } else {
            controller!.scrollToStation(station!)
        }

        return true
    }

    // MARK: - NSUserActivity

    func application(application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool {
        return true
    }


    func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool {
        DLOG("Activity: \(userActivity.userInfo)")

        if let userInfo = userActivity.userInfo {
            if let urlString = userInfo["urlToActivate"] as? String {
                let url = NSURL(string: urlString)
                let success = openLaunchOptionsURL(url!)
                return success
            }
        }
        return false
    }

    
    func application(application: UIApplication, didFailToContinueUserActivityWithType userActivityType: String, error: NSError) {
        DLOG("did fail to continue: \(userActivityType), \(error)")
    }


    func primaryViewController() -> RHCViewController? {
        let nc = self.window?.rootViewController as? UINavigationController
        let controller = nc?.viewControllers.first as? RHCViewController
        return controller
    }
}
{'fraction_non_alphanumeric': 0.0872699677480554, 'fraction_numerical': 0.0020868905331056726, 'mean_word_length': 2.694113524877365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '1189837', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2586, 'n_words': 571}