-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationManager.swift
94 lines (81 loc) · 3.36 KB
/
LocationManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// LocationManager.swift
//
// Created by Василий Савчук on 07/03/2019.
// Copyright © 2019
//
import Foundation
import CoreLocation
final class LocationManager: NSObject {
private let manager = CLLocationManager()
private let gpsManager: GpsManager
private var geoType = GeolocationType.regular.rawValue
private var coordinates: CLLocationCoordinate2D?
private var coordinatesRecieved: ((CLLocationCoordinate2D?, Error?) -> Void)?
init(gpsManager: GpsManager = GpsManager(), geoType: GeolocationType) {
self.gpsManager = gpsManager
self.geoType = geoType.rawValue
super.init()
manager.requestAlwaysAuthorization()
}
private func configLocationManager() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
manager.allowsBackgroundLocationUpdates = true
manager.pausesLocationUpdatesAutomatically = false
manager.startUpdatingLocation()
manager.startMonitoringSignificantLocationChanges()
}
func getCurrentLocation(handler: @escaping ((CLLocationCoordinate2D?, Error?) -> Void)) {
coordinatesRecieved = handler
if CLLocationManager.locationServicesEnabled() {
configLocationManager()
} else {
coordinatesRecieved?(nil, CLError(.locationUnknown))
}
}
private func stopMonitoringLocationChanges() {
let timeForStopMonitoringLocation = Calendar.current.component(.hour, from: Date())
if timeForStopMonitoringLocation >= 18 {
manager.stopMonitoringSignificantLocationChanges()
}
}
func havePermissions() -> Bool {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
return false
case .authorizedAlways, .authorizedWhenInUse:
return true
@unknown default:
fatalError("\(#function) ==> Unknown CLLocationManager.authorizationStatus")
}
} else {
return false
}
}
private func sendGpsDataToTracker(with currentCoordinates: CLLocationCoordinate2D, geoType: String) {
guard let isGeolocationPermissionEnabled = isGeolocationPermissionEnabled() else { return }
if isGeolocationPermissionEnabled {
gpsManager.sendGps(with: currentCoordinates, geoType: geoType)
}
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentCoordinates = manager.location?.coordinate else { return }
coordinatesRecieved?(currentCoordinates, nil)
coordinatesRecieved = nil
manager.stopUpdatingLocation()
stopMonitoringLocationChanges()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
coordinatesRecieved?(nil, error)
coordinatesRecieved = nil
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
status == CLAuthorizationStatus.authorizedAlways ? setAuthorizedAlways(true) : setAuthorizedAlways(false)
}
}