Files
iOS/Sources/Shared/API/Webhook/Request&Response/WebhookResponseLocation.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

99 lines
2.9 KiB
Swift

import Foundation
import UserNotifications
import PromiseKit
import ObjectMapper
extension WebhookResponseIdentifier {
static var location: Self { .init(rawValue: "updateLocation") }
}
struct WebhookResponseLocationLocalMetadata: ImmutableMappable {
let trigger: LocationUpdateTrigger
let zoneName: String
init(trigger: LocationUpdateTrigger, zone: RLMZone?) {
self.trigger = trigger
self.zoneName = zone?.ID ?? "(unknown)"
}
init(map: Map) throws {
self.trigger = try map.value("trigger")
self.zoneName = try map.value("zone_name")
}
func mapping(map: Map) {
trigger >>> map["trigger"]
zoneName >>> map["zone_name"]
}
}
struct WebhookResponseLocation: WebhookResponseHandler {
let api: HomeAssistantAPI
init(api: HomeAssistantAPI) {
self.api = api
}
static func localMetdata(
trigger: LocationUpdateTrigger,
zone: RLMZone?
) -> [String: Any] {
WebhookResponseLocationLocalMetadata(
trigger: trigger,
zone: zone
).toJSON()
}
static func shouldReplace(request current: WebhookRequest, with proposed: WebhookRequest) -> Bool {
// recency should always win
return true
}
enum HandleError: Error {
case missingLocalMetadata
}
func handle(
request: Promise<WebhookRequest>,
result: Promise<Any>
) -> Guarantee<WebhookResponseHandlerResult> {
firstly {
when(fulfilled: request, result)
}.map { request, _ in
guard let localMetadata = request.localMetadata.flatMap({
Mapper<WebhookResponseLocationLocalMetadata>().map(JSON: $0)
}) else {
throw HandleError.missingLocalMetadata
}
let notificationOptions = localMetadata.trigger.notificationOptionsFor(zoneName: localMetadata.zoneName)
Current.clientEventStore.addEvent(ClientEvent(
text: notificationOptions.body,
type: .locationUpdate,
payload: try? request.asDictionary()
))
guard notificationOptions.shouldNotify else {
return nil
}
return UNNotificationRequest(
identifier: notificationOptions.identifier ?? UUID().uuidString,
content: with(UNMutableNotificationContent()) {
$0.title = notificationOptions.title
$0.body = notificationOptions.body
$0.sound = UNNotificationSound.default
},
trigger: nil
)
}.recover { _ -> Guarantee<UNNotificationRequest?> in
// don't send a notification for failed
return .value(nil)
}.map { notification in
var result = WebhookResponseHandlerResult.default
result.notification = notification
return result
}
}
}