Here App Motion Monitoring and Notification Skipping Logic
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
// 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