Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hoereth committed Nov 17, 2020
1 parent f2a6e42 commit c5aadd6
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 21 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# CoreLocationUtils
# 📍 CoreLocationUtils

A description of this package.
Just a few but helpful classes for handling locations with CoreLocation.

## OneTimeLocation.swift

Allows you to query a CLLocation without managing CLLocationManager or any of its delegates.
3 changes: 0 additions & 3 deletions Sources/CoreLocationUtils/CoreLocationUtils.swift

This file was deleted.

91 changes: 91 additions & 0 deletions Sources/CoreLocationUtils/OneTimeLocation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import Foundation
import CoreLocation

public class OneTimeLocation : NSObject, CLLocationManagerDelegate {
public enum LocationError : Error {
case denied
case restricted
case unknown
}

let manager: CLLocationManager
let completion: (Result<CLLocation, Error>)->()

fileprivate static let instancesQueue = DispatchQueue(label: "OneTimeLocation.instances")
fileprivate static var instances = Set<OneTimeLocation>()

public static func queryLocation(desiredAccuracy: CLLocationAccuracy, timeout: TimeInterval, completion: @escaping (Result<CLLocation, Error>)->()) {
let oneTimeLocation = OneTimeLocation(desiredAccuracy: desiredAccuracy, completion: completion)
oneTimeLocation.manager.delegate = oneTimeLocation

switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
instancesQueue.sync {
oneTimeLocation.manager.startUpdatingLocation()
_ = instances.insert(oneTimeLocation)
}
case .notDetermined:
instancesQueue.sync {
oneTimeLocation.manager.requestWhenInUseAuthorization()
_ = instances.insert(oneTimeLocation)
}
case .denied:
completion(Result.failure(LocationError.denied))
case .restricted:
completion(Result.failure(LocationError.restricted))
@unknown default:
completion(Result.failure(LocationError.unknown))
}
}

fileprivate init(desiredAccuracy: CLLocationAccuracy, completion: @escaping (Result<CLLocation, Error>)->()) {
self.manager = CLLocationManager()
self.manager.desiredAccuracy = desiredAccuracy
self.completion = completion
}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locations.count > 0) {
self.completion(Result.success(locations[0]))
} else {
self.completion(Result.failure(LocationError.unknown))
}
self.manager.stopUpdatingLocation()
Self.instancesQueue.sync {
_ = OneTimeLocation.instances.remove(self)
}
}

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
self.completion(Result.failure(error))
self.manager.stopUpdatingLocation()
Self.instancesQueue.sync {
_ = OneTimeLocation.instances.remove(self)
}
}

public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
self.manager.startUpdatingLocation()
case .notDetermined:
break;
case .denied:
completion(Result.failure(LocationError.denied))
Self.instancesQueue.sync {
_ = OneTimeLocation.instances.remove(self)
}
case .restricted:
completion(Result.failure(LocationError.restricted))
Self.instancesQueue.sync {
_ = OneTimeLocation.instances.remove(self)
}
@unknown default:
completion(Result.failure(LocationError.unknown))
Self.instancesQueue.sync {
_ = OneTimeLocation.instances.remove(self)
}
}
}
}
15 changes: 0 additions & 15 deletions Tests/CoreLocationUtilsTests/CoreLocationUtilsTests.swift

This file was deleted.

8 changes: 8 additions & 0 deletions Tests/CoreLocationUtilsTests/OneTimeLocationTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by Mick on 18.11.20.
//

import Foundation
2 changes: 1 addition & 1 deletion Tests/CoreLocationUtilsTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(CoreLocationUtilsTests.allTests),
testCase(OneTimeLocationTest.allTests),
]
}
#endif

0 comments on commit c5aadd6

Please sign in to comment.