Skip to content

Commit

Permalink
Fix crash when Map notification is provided invalid latitude/longitude (
Browse files Browse the repository at this point in the history
#1307)

- Fixes a crash when `latitude` or `longitude` are String types but not able to be converted to Double/CLLocationDegrees.
- Adds support for Int or Float-based `latitude` or `longitude`, e.g. what native templates would provide were it not currently broken.
  • Loading branch information
zacwest committed Dec 20, 2020
1 parent 66bd05b commit bcf50e1
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Sources/Extensions/NotificationContent/MapViewController.swift
Expand Up @@ -33,21 +33,31 @@ class MapViewController: UIViewController, NotificationCategory, MKMapViewDelega
}
}

private class func degrees(from value: Any?) -> CLLocationDegrees? {
if let value = value as? String {
return CLLocationDegrees(value)
} else if let value = value as? Double {
return CLLocationDegrees(value)
} else if let value = value as? Int {
return CLLocationDegrees(value)
} else {
return nil
}
}

// swiftlint:disable:next function_body_length
func didReceive(notification: UNNotification, extensionContext: NSExtensionContext?) -> Promise<Void> {
let userInfo = notification.request.content.userInfo

guard let haDict = userInfo["homeassistant"] as? [String: Any] else {
return .init(error: MapError.missingPayload)
}
guard let latitudeString = haDict["latitude"] as? String else {
guard let latitude = Self.degrees(from: haDict["latitude"]) else {
return .init(error: MapError.missingLatitude)
}
guard let longitudeString = haDict["longitude"] as? String else {
guard let longitude = Self.degrees(from: haDict["longitude"]) else {
return .init(error: MapError.missingLongitude)
}
let latitude = Double.init(latitudeString)! as CLLocationDegrees
let longitude = Double.init(longitudeString)! as CLLocationDegrees
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.mapView = MKMapView()

Expand All @@ -71,10 +81,8 @@ class MapViewController: UIViewController, NotificationCategory, MKMapViewDelega
let dropPin = MKPointAnnotation()
dropPin.coordinate = location

if let secondLatitudeString = haDict["second_latitude"] as? String,
let secondLongitudeString = haDict["second_longitude"] as? String {
let secondLatitude = Double.init(secondLatitudeString)! as CLLocationDegrees
let secondLongitude = Double.init(secondLongitudeString)! as CLLocationDegrees
if let secondLatitude = Self.degrees(from: haDict["second_latitude"]),
let secondLongitude = Self.degrees(from: haDict["second_longitude"]) {
let secondDropPin = MKPointAnnotation()
secondDropPin.coordinate = CLLocationCoordinate2D(latitude: secondLatitude, longitude: secondLongitude)
secondDropPin.title = L10n.Extensions.Map.Location.new
Expand Down

0 comments on commit bcf50e1

Please sign in to comment.