Files
iOS/Sources/Shared/ClientEvents/Model/ClientEvent.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

85 lines
2.4 KiB
Swift

//
// ClientEvent.swift
// HomeAssistant
//
// Created by Stephan Vanterpool on 6/17/18.
// Copyright © 2018 Robbie Trencheny. All rights reserved.
//
import Foundation
import RealmSwift
/// Contains data about an event that occurred on the client, used for logging.
public class ClientEvent: Object {
/// The type of event being logged.
public enum EventType: String {
case notification
case serviceCall
case locationUpdate
case networkRequest
case unknown
}
convenience public init(text: String, type: EventType, payload: [String: Any]? = nil) {
self.init()
self.text = text
self.type = type
self.jsonPayload = payload
}
/// The date the event occurred.
@objc public dynamic var date: Date = Current.date()
/// The text describing the event.
@objc public dynamic var text: String = ""
@objc private dynamic var typeString: String = EventType.unknown.rawValue
/// The even type
public var type: EventType {
get { return EventType(rawValue: self.typeString) ?? .unknown }
set { self.typeString = newValue.rawValue }
}
@objc private dynamic var jsonData: Data?
/// The payload for the event.
public var jsonPayload: [String: Any]? {
get {
guard let payloadData = self.jsonData,
let jsonObject = try? JSONSerialization.jsonObject(with: payloadData),
let dictionary = jsonObject as? [String: Any] else {
return nil
}
return dictionary
}
set {
guard let payload = newValue else {
self.jsonData = nil
return
}
do {
var writeOptions: JSONSerialization.WritingOptions = [.prettyPrinted]
if #available(iOS 13, watchOS 6, *) {
writeOptions.insert(.withoutEscapingSlashes)
}
jsonData = try JSONSerialization.data(withJSONObject: payload, options: writeOptions)
} catch {
Current.Log.error("Error serializing json payload: \(error)")
}
}
}
public var jsonPayloadDescription: String? {
jsonData.flatMap { String(data: $0, encoding: .utf8) }
}
override public static func indexedProperties() -> [String] {
return ["date", "typeString"]
}
}