From d3db02b1bc5afc22e8b3d82beb3d40a6a93e248f Mon Sep 17 00:00:00 2001 From: Philipp Homann Date: Fri, 25 Oct 2019 22:34:36 +0200 Subject: [PATCH] allowes to switch off services wich are needed for region monitoring when all regions are removed --- XOZLocationManager/XOZLocationManager.swift | 36 ++++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/XOZLocationManager/XOZLocationManager.swift b/XOZLocationManager/XOZLocationManager.swift index fc26b9d..8543a7e 100644 --- a/XOZLocationManager/XOZLocationManager.swift +++ b/XOZLocationManager/XOZLocationManager.swift @@ -399,7 +399,7 @@ public class XOZLocationManager: NSObject, CLLocationManagerDelegate { if (self.wayToMonitorRegions == .iOSRegionsMonitoring) { self.tryToUpdateRegionsToMonitor() } else { - self.startUpdatingLocationFor(authType: .always) + self.startUpdatingLocationFor(authType: .always) } } @@ -407,12 +407,38 @@ public class XOZLocationManager: NSObject, CLLocationManagerDelegate { 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() { @@ -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)