Skip to content

Commit

Permalink
allowes to switch off services wich are needed for region monitoring …
Browse files Browse the repository at this point in the history
…when all regions are removed
  • Loading branch information
Philipp Homann authored and Philipp Homann committed Oct 25, 2019
1 parent 781b73d commit d3db02b
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions XOZLocationManager/XOZLocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,46 @@ public class XOZLocationManager: NSObject, CLLocationManagerDelegate {
if (self.wayToMonitorRegions == .iOSRegionsMonitoring) {
self.tryToUpdateRegionsToMonitor()
} else {
self.startUpdatingLocationFor(authType: .always)
self.startUpdatingLocationFor(authType: .always)
}
}

// removes a special region from the array which holds all regions which are to monitor
public func removeRegionToMonitor(region: CLCircularRegion) {
if let index = self.allRegionsToMonitor?.firstIndex(of: region) {
self.allRegionsToMonitor?.remove(at: index)
// @TODO: stop significant location changes or updating locations when no more is needed

if self.allRegionsToMonitor?.count == 0 {
self.switchOffRegionMonitoringServices()
}
}
self.startRegionMonitoring() // maybe we need to update regions again
}

private func switchOffRegionMonitoringServices()
{
if (self.wayToMonitorRegions == .iOSRegionsMonitoring) {
// @TODO needs to be testet
if (self.wayToDetermineNearestRegions == .significantLocationChanges){
self.locationManager.stopMonitoringSignificantLocationChanges()
} else if (self.wayToDetermineNearestRegions == .locationUpdates) {
self.locationManager.stopUpdatingLocation()
}
} else if (self.wayToMonitorRegions == .locationUpdates ) {
self.locationManager.stopUpdatingLocation()
}
self.startRegionMonitoring()
}


// removes a special region from the array which holds all regions which are to monitor
public func removeAllRegionsToMonitor() {
self.allRegionsToMonitor?.removeAll()
self.switchOffRegionMonitoringServices()
self.stopMonitoringAllRegions()
}



// this request to get the latest known location, it end's in didUpdateLocations, which calls updateRegionsToMonitor()
private func tryToUpdateRegionsToMonitor()
{
Expand Down Expand Up @@ -508,13 +534,13 @@ public class XOZLocationManager: NSObject, CLLocationManagerDelegate {
log("didEnterRegion \(region.description )")

var locationSpeed: CLLocationSpeed = -1;
var locationCourse: CLLocationDirection = -1;
var locationCourse: CLLocationDirection = -999;

if let loc = location {
locationSpeed = loc.speed
locationCourse = loc.course

#if (DEBUG && targetEnvironment(simulator))
#if (DEBUG ) //&& targetEnvironment(simulator))
// at simulator we don't have course information, se we calculate the course base on previous location und current one
if let prevLoc = self.previousLocation {
locationCourse = prevLoc.getBearingTo(location: loc)
Expand Down

0 comments on commit d3db02b

Please sign in to comment.