<commit_before>//
//  ViewController.swift
//  RangeWhileMonitor
//
//  Created by <NAME> on 26/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    
    var manager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        requestAlwaysAuthorization() // Shows an alert if authorization was determined earlier
        
        if let uuid = NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D") {
            let region = CLBeaconRegion(proximityUUID: uuid, identifier: uuid.UUIDString)
            manager.startMonitoringForRegion(region)
        }
    }
    
    // MARK: CLLocationManagerDelegate
    func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
        if let region = region as? CLBeaconRegion {
            manager.startRangingBeaconsInRegion(region)
        }
    }
    
    func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
        if let region = region as? CLBeaconRegion {
            manager.stopRangingBeaconsInRegion(region)
        }
    }

    // MARK: ()
    func requestAlwaysAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .AuthorizedAlways:
            break
        case .NotDetermined:
            manager.requestAlwaysAuthorization()
        case .AuthorizedWhenInUse, .Restricted, .Denied:
            let alertController = UIAlertController(
                title: "Background Location Access Disabled",
                message: "In order to be notified about iBeacons near you, please open this app's settings and set location access to 'Always'.",
                preferredStyle: .Alert)
            
            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)
            
            let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
                if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                    UIApplication.sharedApplication().openURL(url)
                }
            }
            alertController.addAction(openAction)
            
            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }

}
<commit_msg>Add regions sets into didRangeBeacons<commit_after>//
//  ViewController.swift
//  RangeWhileMonitor
//
//  Created by <NAME> on 26/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    
    var manager = CLLocationManager()
    var currentRegions = Set<CLBeaconRegion>()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        requestAlwaysAuthorization() // Shows an alert if authorization was determined earlier
        
        if let uuid = NSUUID(UUIDString: "8492E75F-4FD6-469D-B132-043FE94921D8") {
            let region = CLBeaconRegion(proximityUUID: uuid, identifier: uuid.UUIDString)
            manager.startMonitoringForRegion(region)
        }
    }
    
    // MARK: CLLocationManagerDelegate
    func locationManager(manager: CLLocationManager!, didDetermineState state: CLRegionState, forRegion region: CLRegion!) {
        if let region = region as? CLBeaconRegion {
            switch state {
            case .Inside:
                manager.startRangingBeaconsInRegion(region)
            case .Outside:
                delay(1.0) {
                    manager.stopRangingBeaconsInRegion(region)
                }
            case .Unknown:
                break
            }
        }
    }
    
    func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
        var latestRegions = Set<CLBeaconRegion>()
        
        // Create CLBeaconRegions from CLBeacons
        if let beacons = beacons as? [CLBeacon] {
            for beacon in beacons {
                latestRegions.insert(regionFromBeacon(beacon))
            }
        }
        
        let enteredRegions = latestRegions.subtract(currentRegions)
        let exitedRegions = currentRegions.subtract(latestRegions)
        currentRegions = latestRegions
        
        if enteredRegions.count > 0 {
            println("Entered")
            println(enteredRegions)
        }
        
        if exitedRegions.count > 0 {
            println("Exited")
            println(exitedRegions)
        }
    }

    // MARK: ()
    func regionFromBeacon(beacon: CLBeacon) -> CLBeaconRegion {
        let major = CLBeaconMajorValue(beacon.major.integerValue)
        let minor = CLBeaconMinorValue(beacon.minor.integerValue)
        let identifier = "\(beacon.proximityUUID.UUIDString).\(major).\(minor)" // Used for "is equal" check in Sets
        return CLBeaconRegion(proximityUUID: beacon.proximityUUID, major: major, minor: minor, identifier: identifier)
    }
    
    func requestAlwaysAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .AuthorizedAlways:
            break
        case .NotDetermined:
            manager.requestAlwaysAuthorization()
        case .AuthorizedWhenInUse, .Restricted, .Denied:
            let alertController = UIAlertController(
                title: "Background Location Access Disabled",
                message: "In order to be notified about iBeacons near you, please open this app's settings and set location access to 'Always'.",
                preferredStyle: .Alert)
            
            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)
            
            let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
                if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                    UIApplication.sharedApplication().openURL(url)
                }
            }
            alertController.addAction(openAction)
            
            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }
    
    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
}

{'fraction_non_alphanumeric': 0.08237431859479104, 'fraction_numerical': 0.010145366444579043, 'mean_word_length': 2.175480769230769, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '1543710', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1731, 'n_words': 404}