<commit_before>import CoreLocation

protocol AirportsWatcherDelegate {
    func visitorChangedState(newState: AirportsVisitorState)
}

class AirportsWatcher: NSObject {
    var locationManager: CLLocationManager = CLLocationManager()
    var delegate: AirportsWatcherDelegate?
    var regions: [CLCircularRegion] = []
    var lastAirportsVisitorState = AirportsVisitorState()
 
    override init() {
        super.init()
        locationManager.delegate = self
        if (PRIOR_IOS8) {
            locationManager.allowDeferredLocationUpdatesUntilTraveled(allowDeferredLocationUpdatesUntilTraveledDistance, timeout: CLTimeIntervalMax) // 2000 km
            locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
            locationManager.distanceFilter = kCLDistanceFilterNone;
        }
    }
    
    func registerAirports(airportsProvider: AirportsProvider) {
        func buildOuterRegionFromAirport(airport: Airport) -> CLCircularRegion {
            let center = CLLocationCoordinate2D(latitude:airport.latitude, longitude:airport.longitude)
            let radius = CLLocationDistance(outerAirportPerimeterDistance)
            let id = String(airport.id)
            let region = CLCircularRegion(center: center, radius: radius, identifier: id)
            return region
        }
        
        let airports = airportsProvider.airports
        for airport in airports {
            var region = buildOuterRegionFromAirport(airport)
            regions.append(region)
        }
    }
    
    func isLocationMonitoringAvailable() -> Bool {
        return CLLocationManager.significantLocationChangeMonitoringAvailable()
    }
    
    func hasRequiredAuthorizations() -> Bool {
        let status = CLLocationManager.authorizationStatus()
        log("CLLocationManager.authorizationStatus() returned \(status.rawValue) (expected 3)")
        return status.rawValue == 3 // Swift problem: .AuthorizedAlways is missing
    }
    
    func requestRequiredAuthorizations() {
        if SINCE_IOS8 {
            log("requestAlwaysAuthorization")
            locationManager.requestAlwaysAuthorization()
        }
    }
    
    func start() {
        log("AirportsWatcher start");
        if inSimulator() {
            // startMonitoringSignificantLocationChanges does not work in simulator, see http://stackoverflow.com/a/6213528
            locationManager.startUpdatingLocation()
        } else {
            if useSignificantLocationChanges {
                locationManager.startMonitoringSignificantLocationChanges()
            } else {
                locationManager.startUpdatingLocation()
            }
        }
    }
    
    func stop() {
        log("AirportsWatcher stop");
        if inSimulator() {
            locationManager.stopUpdatingLocation()
        } else {
            locationManager.stopMonitoringSignificantLocationChanges()
            locationManager.stopUpdatingLocation()
        }
    }
    
    func hitTest(latitude:Double, _ longitude:Double) -> [CLCircularRegion] {<commit_msg>requestRequiredAuthorizations() works under iOS7<commit_after>import CoreLocation

protocol AirportsWatcherDelegate {
    func visitorChangedState(newState: AirportsVisitorState)
}

class AirportsWatcher: NSObject {
    var locationManager: CLLocationManager = CLLocationManager()
    var delegate: AirportsWatcherDelegate?
    var regions: [CLCircularRegion] = []
    var lastAirportsVisitorState = AirportsVisitorState()
 
    override init() {
        super.init()
        locationManager.delegate = self
        if (PRIOR_IOS8) {
            locationManager.allowDeferredLocationUpdatesUntilTraveled(allowDeferredLocationUpdatesUntilTraveledDistance, timeout: CLTimeIntervalMax) // 2000 km
            locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
            locationManager.distanceFilter = kCLDistanceFilterNone;
        }
    }
    
    func registerAirports(airportsProvider: AirportsProvider) {
        func buildOuterRegionFromAirport(airport: Airport) -> CLCircularRegion {
            let center = CLLocationCoordinate2D(latitude:airport.latitude, longitude:airport.longitude)
            let radius = CLLocationDistance(outerAirportPerimeterDistance)
            let id = String(airport.id)
            let region = CLCircularRegion(center: center, radius: radius, identifier: id)
            return region
        }
        
        let airports = airportsProvider.airports
        for airport in airports {
            var region = buildOuterRegionFromAirport(airport)
            regions.append(region)
        }
    }
    
    func isLocationMonitoringAvailable() -> Bool {
        return CLLocationManager.significantLocationChangeMonitoringAvailable()
    }
    
    func hasRequiredAuthorizations() -> Bool {
        let status = CLLocationManager.authorizationStatus()
        log("CLLocationManager.authorizationStatus() returned \(status.rawValue) (expected 3)")
        return status.rawValue == 3 // Swift problem: .AuthorizedAlways is missing
    }
    
    func requestRequiredAuthorizations() {
        if SINCE_IOS8 {
            log("requestAlwaysAuthorization")
            locationManager.requestAlwaysAuthorization()
        } else {
            if !hasRequiredAuthorizations() {
                // this is the way how to trigger system popup for allowing locations under iOS7
                log("enable/disable location manager to trigger system popup under iOS7")
                if useSignificantLocationChanges {
                    locationManager.startMonitoringSignificantLocationChanges()
                    locationManager.stopMonitoringSignificantLocationChanges()
                } else {
                    locationManager.startUpdatingLocation()
                    locationManager.stopUpdatingLocation()
                }
            }
        }
    }
    
    func start() {
        log("AirportsWatcher start");
        if inSimulator() {
            // startMonitoringSignificantLocationChanges does not work in simulator, see http://stackoverflow.com/a/6213528
            locationManager.startUpdatingLocation()
        } else {
            if useSignificantLocationChanges {
                locationManager.startMonitoringSignificantLocationChanges()
            } else {
                locationManager.startUpdatingLocation()
            }
        }
    }
    
    func stop() {
        log("AirportsWatcher stop");
        if inSimulator() {
            locationManager.stopUpdatingLocation()
        } else {
            locationManager.stopMonitoringSignificantLocationChanges()
            locationManager.stopUpdatingLocation()
        }
    }
    
    func hitTest(latitude:Double, _ longitude:Double) -> [CLCircularRegion] {
{'fraction_non_alphanumeric': 0.06955749593014651, 'fraction_numerical': 0.005179813526713039, 'mean_word_length': 2.56622691292876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5695370', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1634, 'n_words': 320}