Skip to content

Commit

Permalink
return speedAccuracy, do not return -ve speed
Browse files Browse the repository at this point in the history
  • Loading branch information
bill-florio committed Oct 17, 2023
1 parent 739d055 commit b4d6edc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions Sources/SpeedManagerModule/SpeedManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class SpeedManager : NSObject, ObservableObject, SpeedManagerTrigger {

@Published public var authorizationStatus: SpeedManagerAuthorizationStatus = .notDetermined
@Published public var speed: Double = 0

@Published public var speedAccuracy: Double = 0

private var isRequestingLocation = false

public init(_ speedUnit: SpeedManagerUnit,
Expand Down Expand Up @@ -69,7 +70,7 @@ extension SpeedManager: CLLocationManagerDelegate {
case .authorizedWhenInUse,
.authorizedAlways:
authorizationStatus = .authorized
locationManager.requestLocation()
locationManager.requestLocation()
break

case .notDetermined:
Expand All @@ -87,10 +88,11 @@ extension SpeedManager: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let currentSpeed = locations.last?.speed ?? 0
let calculatedSpeed = currentSpeed * self.speedUnit.rawValue
self.speed = abs(calculatedSpeed)
self.delegate?.speedManager(self, didUpdateSpeed: calculatedSpeed)
speed = currentSpeed >= 0 ? currentSpeed * speedUnit.rawValue : .nan
speedAccuracy = locations.last?.speedAccuracy ?? .nan

self.delegate?.speedManager(self, didUpdateSpeed: speed, speedAccuracy: speedAccuracy)

self.locationManager.requestLocation()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SpeedManagerModule/SpeedManagerDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public protocol SpeedManagerDelegate {
func speedManager(_ manager: SpeedManager, didUpdateSpeed speed: Double)
func speedManager(_ manager: SpeedManager, didUpdateSpeed speed: Double, speedAccuracy: Double)
func speedManager(_ manager: SpeedManager, didFailWithError error: Error)
}

0 comments on commit b4d6edc

Please sign in to comment.