Avoid reporting duplicate regions

This commit is contained in:
Adrian Schoenig 2022-08-08 11:37:37 +10:00
parent 9416be5e5c
commit 7f11da6b83
1 changed files with 14 additions and 0 deletions

View File

@ -15,6 +15,7 @@ public class GeoMonitor: NSObject, ObservableObject {
static var maximumDistanceToRegionCenter: CLLocationDistance = 25_000
static var currentLocationFetchTimeOut: TimeInterval = 30
static var currentLocationFetchRecency: TimeInterval = 10
static var minIntervalBetweenEnteringSameRegion: TimeInterval = 120
}
public enum FetchTrigger: String {
@ -60,6 +61,8 @@ public class GeoMonitor: NSObject, ObservableObject {
private let enabledKey: String?
private var recentlyReportedRegionIdentifiers: [(String, Date)] = []
public var maxRegionsToMonitor = 20
/// Instantiates new monitor
@ -439,6 +442,17 @@ extension GeoMonitor: CLLocationManagerDelegate {
do {
let location = try await fetchCurrentLocation()
let minInterval = Constants.minIntervalBetweenEnteringSameRegion * -1
if let lastReport = recentlyReportedRegionIdentifiers.first(where: { $0.0 == region.identifier }), lastReport.1.timeIntervalSinceNow >= minInterval {
#if DEBUG
eventHandler(.debug("GeoMonitor reported duplicate for \(region.identifier). Entered \(lastReport.1.timeIntervalSinceNow * -1) seconds ago.", .enteredRegion))
#endif
return // Already reported with `minIntervalBetweenEnteringSameRegion`
}
recentlyReportedRegionIdentifiers.append((region.identifier, Date()))
recentlyReportedRegionIdentifiers.removeAll { $0.1.timeIntervalSinceNow < minInterval }
eventHandler(.entered(match, location))
} catch {
#if DEBUG