Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Here App Motion Monitoring and Notification Skipping Logic
// MARK - Motion Tracking Logic
func startTrackingMotionActivity(handler: @escaping (CMMotionActivity) -> Void) {
manager.startActivityUpdates(to: .main) { (activity) in
guard let activity = activity else { return }
if let lastActivity = self.currentActivity, lastActivity.automotive {
self.stoppedDrivingAt = Date() // now
}
self.currentActivity = activity
handler(activity)
}
}
var isDriving: Bool {
guard let activity = currentActivity else {
return false
}
return activity.automotive
}
var drivingThreshold: Int {
return 2
}
var hasBeenDriving: Bool {
if isDriving { return true }
guard let lastDroveAt = self.stoppedDrivingAt else {
return false
}
return lastDroveAt > self.drivingThreshold.minutes.ago
}
var isUnknown: Bool {
guard let activity = currentActivity else {
return true
}
return activity.unknown
}
var skipNotifications: Bool {
// ensure notification when motion unavailable (sim, denied access, etc.)
guard MotionManager.isActivityAvailable() else {
return false
}
return (self.hasBeenDriving || self.isUnknown)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment