mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-20 20:06:04 -06:00
## Summary Swift lint and swiftformat are outdated. This PR does update those + applies the new formatting form swiftformat. There is 1 swift file with a manual change: `Sources/Vehicle/Templates/Areas/CarPlayAreasViewModel.swift`. This is done because `swiftlint` did create the following swiftlint error: `error: Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)`. Because it does change a lot of files the question is if we want to finetune the `swiftformat` rules. ## Screenshots No user facing changes. ## Link to pull request in Documentation repository NA ## Any other notes NA
60 lines
1.9 KiB
Swift
60 lines
1.9 KiB
Swift
import CoreMotion
|
|
import Foundation
|
|
import PromiseKit
|
|
|
|
public class ActivitySensor: SensorProvider {
|
|
public enum ActivityError: Error {
|
|
case unauthorized
|
|
case unavailable
|
|
case noData
|
|
}
|
|
|
|
public let request: SensorProviderRequest
|
|
public required init(request: SensorProviderRequest) {
|
|
self.request = request
|
|
}
|
|
|
|
public func sensors() -> Promise<[WebhookSensor]> {
|
|
firstly {
|
|
Self.latestMotionActivity()
|
|
}.map { activity in
|
|
with(WebhookSensor(name: "Activity", uniqueID: "activity")) {
|
|
$0.State = activity.activityTypes.first
|
|
$0.Attributes = [
|
|
"Confidence": activity.confidence.description,
|
|
"Types": activity.activityTypes,
|
|
]
|
|
$0.Icon = activity.icons.first
|
|
}
|
|
}.map {
|
|
[$0]
|
|
}
|
|
}
|
|
|
|
private static func latestMotionActivity() -> Promise<CMMotionActivity> {
|
|
guard Current.motion.isAuthorized() else {
|
|
return .init(error: ActivityError.unauthorized)
|
|
}
|
|
|
|
guard Current.motion.isActivityAvailable() else {
|
|
Current.Log.warning("Activity is not available")
|
|
return .init(error: ActivityError.unavailable)
|
|
}
|
|
|
|
let (promise, seal) = Promise<CMMotionActivity>.pending()
|
|
let end = Current.date()
|
|
let start = Current.calendar().date(byAdding: .minute, value: -10, to: end)!
|
|
let queue = OperationQueue.main
|
|
Current.motion.queryStartEndOnQueueHandler(start, end, queue) { activities, error in
|
|
if let latestActivity = activities?.last {
|
|
seal.fulfill(latestActivity)
|
|
} else if let error {
|
|
seal.reject(error)
|
|
} else {
|
|
seal.reject(ActivityError.noData)
|
|
}
|
|
}
|
|
return promise
|
|
}
|
|
}
|