Avoid reporting duplicate regions
This commit is contained in:
parent
9416be5e5c
commit
7f11da6b83
|
@ -15,6 +15,7 @@ public class GeoMonitor: NSObject, ObservableObject {
|
||||||
static var maximumDistanceToRegionCenter: CLLocationDistance = 25_000
|
static var maximumDistanceToRegionCenter: CLLocationDistance = 25_000
|
||||||
static var currentLocationFetchTimeOut: TimeInterval = 30
|
static var currentLocationFetchTimeOut: TimeInterval = 30
|
||||||
static var currentLocationFetchRecency: TimeInterval = 10
|
static var currentLocationFetchRecency: TimeInterval = 10
|
||||||
|
static var minIntervalBetweenEnteringSameRegion: TimeInterval = 120
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FetchTrigger: String {
|
public enum FetchTrigger: String {
|
||||||
|
@ -60,6 +61,8 @@ public class GeoMonitor: NSObject, ObservableObject {
|
||||||
|
|
||||||
private let enabledKey: String?
|
private let enabledKey: String?
|
||||||
|
|
||||||
|
private var recentlyReportedRegionIdentifiers: [(String, Date)] = []
|
||||||
|
|
||||||
public var maxRegionsToMonitor = 20
|
public var maxRegionsToMonitor = 20
|
||||||
|
|
||||||
/// Instantiates new monitor
|
/// Instantiates new monitor
|
||||||
|
@ -439,6 +442,17 @@ extension GeoMonitor: CLLocationManagerDelegate {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let location = try await fetchCurrentLocation()
|
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))
|
eventHandler(.entered(match, location))
|
||||||
} catch {
|
} catch {
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
Loading…
Reference in New Issue