Skip to content

Commit

Permalink
Merge pull request hyperoslo#128 from aashishdhawan/refactor/location…
Browse files Browse the repository at this point in the history
…-manager

Refactored LocationManager into its own File
  • Loading branch information
zenangst committed Apr 2, 2016
2 parents b1c07e8 + ee541bc commit c53f332
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
4 changes: 4 additions & 0 deletions ImagePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
39D134101CAC4B4E00EA2ECE /* AssetManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39D1340F1CAC4B4E00EA2ECE /* AssetManager.swift */; };
39E3C3311CAFD79200340DAD /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39E3C3301CAFD79200340DAD /* LocationManager.swift */; };
D5D370C01C44FD1600690C0A /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D5D370BA1C44FD1600690C0A /* [email protected] */; };
D5D370C11C44FD1600690C0A /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D5D370BB1C44FD1600690C0A /* [email protected] */; };
D5D370C21C44FD1600690C0A /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D5D370BC1C44FD1600690C0A /* [email protected] */; };
Expand Down Expand Up @@ -43,6 +44,7 @@

/* Begin PBXFileReference section */
39D1340F1CAC4B4E00EA2ECE /* AssetManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetManager.swift; sourceTree = "<group>"; };
39E3C3301CAFD79200340DAD /* LocationManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
D5D370BA1C44FD1600690C0A /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D5D370BB1C44FD1600690C0A /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D5D370BC1C44FD1600690C0A /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -152,6 +154,7 @@
D5DC59BB1C201CC4003BD79B /* Photos.swift */,
D5DC59BC1C201CC4003BD79B /* TopView */,
39D1340F1CAC4B4E00EA2ECE /* AssetManager.swift */,
39E3C3301CAFD79200340DAD /* LocationManager.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -316,6 +319,7 @@
files = (
D5DC59CE1C201CC4003BD79B /* TopView.swift in Sources */,
D5DC59C41C201CC4003BD79B /* ImageStack.swift in Sources */,
39E3C3311CAFD79200340DAD /* LocationManager.swift in Sources */,
D5DC59CB1C201CC4003BD79B /* ImageGalleryViewDataSource.swift in Sources */,
D5DC59CA1C201CC4003BD79B /* ImageGalleryViewCell.swift in Sources */,
D5DC59C81C201CC4003BD79B /* ConstraintsSetup.swift in Sources */,
Expand Down
35 changes: 0 additions & 35 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,38 +386,3 @@ class CameraView: UIViewController, CLLocationManagerDelegate {
}
}
}

class LocationManager: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var latestLocation: CLLocation?

override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}

func startUpdatingLocation() {
locationManager.startUpdatingLocation()
}

func stopUpdatingLocation() {
locationManager.stopUpdatingLocation()
}

// MARK: - CLLocationManagerDelegate

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Pick the location with best (= smallest value) horizontal accuracy
latestLocation = locations.sort{ $0.horizontalAccuracy < $1.horizontalAccuracy }.first
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
} else {
locationManager.stopUpdatingLocation()
}
}
}
37 changes: 37 additions & 0 deletions Source/LocationManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import CoreLocation

class LocationManager: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var latestLocation: CLLocation?

override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}

func startUpdatingLocation() {
locationManager.startUpdatingLocation()
}

func stopUpdatingLocation() {
locationManager.stopUpdatingLocation()
}

// MARK: - CLLocationManagerDelegate

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Pick the location with best (= smallest value) horizontal accuracy
latestLocation = locations.sort{ $0.horizontalAccuracy < $1.horizontalAccuracy }.first
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
} else {
locationManager.stopUpdatingLocation()
}
}
}

0 comments on commit c53f332

Please sign in to comment.