-
-
Save sturdysturge/dc31e25589a9a9b5afce88bc9eaa3195 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import CoreLocation | |
final class DataModel: NSObject, ObservableObject, CLLocationManagerDelegate { | |
static let shared = DataModel() | |
private let locationManager = CLLocationManager() | |
private var multiplier: Double { return isKPH ? 3.6 : 2.236936 } | |
private override init() { | |
super.init() | |
locationManager.start(delegate: self) | |
} | |
@Published private(set) var speed = Double() | |
@Published var isKPH = UserDefaults.standard.bool(forKey: .isKPH) { | |
didSet { UserDefaults.standard.set(isKPH, forKey: .isKPH) } | |
} | |
private func updateSpeed(value: Double) { | |
speed = .zero | |
if value > 1 { speed = value + 0.5 } | |
} | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
let speedFromGPS = locationManager.speed(multipliedBy: multiplier) | |
updateSpeed(value: speedFromGPS) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment