Files
iOS/Sources/Extensions/Watch/MapNotificationController.swift
Zac West 4d9a530637 Reorganize files in repo, pull out build settings from pbxproj (#1140)
This is somewhat in prep of being able to make the project file generated, but also just organizes things into more concrete directory structures.

This pulls out _all_ of the build settings from the root level, and most from the target level, into xcconfigs.

The new directory structure looks like:

- Sources
  - App
    - (everything from HomeAssistant/)
  - WatchApp
  - Shared
  - MacBridge
  - Extensions
    - Intents
    - NotificationContent
    - NotificationService
    - Share
    - Today
    - Watch
    - Widgets
- Tests
  - App
  - UI
  - Shared

Somewhat intentionally, the file structure under these is not yet standardized/organized.

The project targets are now:

- App
- WatchApp
- Shared-iOS
- Shared-watchOS
- MacBridge
- Tests-App
- Tests-UI
- Tests-Shared
- Extension-Intents
- Extension-NotificationContent
- Extension-NotificationService
- Extension-Share
- Extension-Today
- Extension-Widget
- WatchExtension-Watch

This does not yet clean up resources vs. sources, nor does it handle some of the "it's in Sources/App but it's part of Shared" crossover directory issues.
2020-10-03 00:15:04 -07:00

104 lines
3.8 KiB
Swift

//
// MapNotificationController.swift
// WatchAppExtension
//
// Created by Robert Trencheny on 2/27/19.
// Copyright © 2019 Robbie Trencheny. All rights reserved.
//
import WatchKit
import Foundation
import UserNotifications
import MapKit
import Shared
class MapNotificationController: WKUserNotificationInterfaceController {
@IBOutlet weak var mapView: WKInterfaceMap!
@IBOutlet weak var notificationTitleLabel: WKInterfaceLabel!
@IBOutlet weak var notificationSubtitleLabel: WKInterfaceLabel!
@IBOutlet weak var notificationAlertLabel: WKInterfaceLabel!
// MARK: - WKUserNotificationInterfaceController
override func didReceive(_ notification: UNNotification) {
self.notificationTitleLabel.setText(notification.request.content.title)
self.notificationSubtitleLabel.setText(notification.request.content.subtitle)
self.notificationAlertLabel!.setText(notification.request.content.body)
let userInfo = notification.request.content.userInfo
guard let haDict = userInfo["homeassistant"] as? [String: Any] else {
Current.Log.error(L10n.Extensions.Map.PayloadMissingHomeassistant.message)
return
}
guard let latitudeString = haDict["latitude"] as? String else {
Current.Log.error(L10n.Extensions.Map.ValueMissingOrUncastable.Latitude.message)
return
}
guard let longitudeString = haDict["longitude"] as? String else {
Current.Log.error(L10n.Extensions.Map.ValueMissingOrUncastable.Longitude.message)
return
}
let latitude = Double.init(latitudeString)! as CLLocationDegrees
let longitude = Double.init(longitudeString)! as CLLocationDegrees
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
var pinLocations: [CLLocationCoordinate2D] = [location]
self.mapView.addAnnotation(location, with: .red)
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
let secondCoords = CLLocationCoordinate2D(latitude: secondLatitude, longitude: secondLongitude)
pinLocations.append(secondCoords)
self.mapView.addAnnotation(secondCoords, with: .green)
}
var region = MKCoordinateRegion(center: pinLocations[0],
span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
if pinLocations.count > 1 {
region = MKCoordinateRegion(coordinates: pinLocations)
}
self.mapView.setRegion(region)
}
}
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)
}
}