Skip to content

Instantly share code, notes, and snippets.

@ajayjapan
Last active November 29, 2018 13:01
Show Gist options
  • Save ajayjapan/f9d1d5ecd1e67a130c233a4d61261f46 to your computer and use it in GitHub Desktop.
Save ajayjapan/f9d1d5ecd1e67a130c233a4d61261f46 to your computer and use it in GitHub Desktop.
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