Files
iOS/Sources/Extensions/Watch/Notifications/NotificationSubControllerMap.swift
mat1th 97834bfd5e Update swift lint and format + appy fixes (#2585)
## 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
2024-02-22 13:06:39 +01:00

96 lines
3.0 KiB
Swift

import Foundation
import PromiseKit
import Shared
import UserNotifications
import WatchKit
class NotificationSubControllerMap: NotificationSubController {
let api: HomeAssistantAPI
let location: CLLocationCoordinate2D
let secondLocation: CLLocationCoordinate2D?
required init?(api: HomeAssistantAPI, notification: UNNotification) {
let userInfo = notification.request.content.userInfo
guard let haDict = userInfo["homeassistant"] as? [String: Any],
let latitude = CLLocationDegrees(templateValue: haDict["latitude"]),
let longitude = CLLocationDegrees(templateValue: haDict["longitude"]) else {
return nil
}
self.api = api
self.location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
if let secondLatitude = CLLocationDegrees(templateValue: haDict["second_latitude"]),
let secondLongitude = CLLocationDegrees(templateValue: haDict["second_longitude"]) {
self.secondLocation = CLLocationCoordinate2D(latitude: secondLatitude, longitude: secondLongitude)
} else {
self.secondLocation = nil
}
}
required init?(api: HomeAssistantAPI, url: URL) {
nil
}
func start(with elements: NotificationElements) -> Promise<Void> {
elements.map.setHidden(false)
var pinLocations: [CLLocationCoordinate2D] = [location]
elements.map.addAnnotation(location, with: .red)
if let secondLocation {
pinLocations.append(secondLocation)
elements.map.addAnnotation(secondLocation, with: .green)
}
var region = MKCoordinateRegion(
center: pinLocations[0],
span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
)
if pinLocations.count > 1 {
region = MKCoordinateRegion(coordinates: pinLocations)
}
elements.map.setRegion(region)
return .value(())
}
func stop() {
// nothing to stop
}
}
extension MKCoordinateRegion {
init(coordinates: [CLLocationCoordinate2D]) {
var minLat: CLLocationDegrees = 90.0
var maxLat: CLLocationDegrees = -90.0
var minLon: CLLocationDegrees = 180.0
var maxLon: CLLocationDegrees = -180.0
for coordinate in coordinates {
let lat = Double(coordinate.latitude)
let long = Double(coordinate.longitude)
if lat < minLat {
minLat = lat
}
if long < minLon {
minLon = long
}
if lat > maxLat {
maxLat = lat
}
if long > maxLon {
maxLon = long
}
}
let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 2.0, longitudeDelta: (maxLon - minLon) * 2.0)
let center = CLLocationCoordinate2DMake(maxLat - span.latitudeDelta / 4, maxLon - span.longitudeDelta / 4)
self.init(center: center, span: span)
}
}