Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created February 6, 2021 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/dc31e25589a9a9b5afce88bc9eaa3195 to your computer and use it in GitHub Desktop.
Save sturdysturge/dc31e25589a9a9b5afce88bc9eaa3195 to your computer and use it in GitHub Desktop.
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