← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Shared/Models/Context.swift (4972B)

1import Foundation
2
3/// Context captured automatically with a report. Every section is optional: a
4/// provider that failed, was denied or had nothing to say leaves its section
5/// `nil`.
6///
7/// Units are metric throughout and noted on each field.
8struct Context: Codable, Hashable, Sendable {
9    var location: LocationContext?
10    var weather: WeatherContext?
11    var audio: AudioContext?
12    var motion: MotionContext?
13    var device: DeviceContext?
14    var photos: PhotosContext?
15}
16
17struct LocationContext: Codable, Hashable, Sendable {
18    var coordinate: Coordinate
19    /// Metres above sea level.
20    var altitude: Double?
21    /// Metres.
22    var horizontalAccuracy: Double?
23    /// Metres.
24    var verticalAccuracy: Double?
25    /// Metres per second.
26    var speed: Double?
27    /// Degrees clockwise from true north.
28    var course: Double?
29    /// When the fix was taken.
30    var timestamp: Date?
31    /// The address in the user's first language.
32    var placemark: Placemark?
33    /// The address in the user's other languages, so it can be shown by the
34    /// same rule as place names. Reporter recorded one placemark only.
35    var localisedPlacemarks: [Placemark]?
36}
37
38struct Placemark: Codable, Hashable, Sendable {
39    var name: String?
40    var subThoroughfare: String?
41    var thoroughfare: String?
42    var subLocality: String?
43    var locality: String?
44    var subAdministrativeArea: String?
45    var administrativeArea: String?
46    var postalCode: String?
47    var country: String?
48    var inlandWater: String?
49    var ocean: String?
50    /// The BCP 47 language the names are in. Unknown for imported reports,
51    /// whose placemarks are in whatever language the device used.
52    var language: String?
53    /// ISO 3166-1 region code, for example `JP`.
54    var countryCode: String?
55    /// The city with its region, as Apple Maps writes it.
56    var cityWithContext: String?
57    var fullAddress: String?
58    var shortAddress: String?
59}
60
61extension Placemark {
62    /// Where this is, in a few words: the city with its region, as Apple
63    /// Maps writes it ('Suginami, Tokyo'). Imported placemarks don't have
64    /// that, so it's put together from their locality and region, or else
65    /// the most specific place there is.
66    var displayLocality: String? {
67        if let cityWithContext { return cityWithContext }
68        guard let locality else { return subLocality ?? name }
69        guard let administrativeArea, administrativeArea != locality else { return locality }
70        return "\(locality), \(administrativeArea)"
71    }
72}
73
74struct WeatherContext: Codable, Hashable, Sendable {
75    enum Source: String, Codable, Sendable {
76        case wunderground
77        case darkSky
78        case weatherKit
79    }
80
81    var source: Source
82    /// `true` when the values were fetched after the fact rather than at the
83    /// time of the report.
84    var backfilled: Bool?
85    var condition: String?
86    /// °C.
87    var temperature: Double?
88    /// °C.
89    var feelsLike: Double?
90    /// °C.
91    var dewPoint: Double?
92    /// Fraction, 0–1.
93    var humidity: Double?
94    /// Hectopascals.
95    var pressure: Double?
96    /// Kilometres per hour.
97    var windSpeed: Double?
98    /// Kilometres per hour.
99    var windGust: Double?
100    /// Degrees clockwise from north that the wind is blowing from.
101    var windDirection: Double?
102    /// Kilometres.
103    var visibility: Double?
104    var uvIndex: Double?
105    /// Fraction, 0–1.
106    var cloudCover: Double?
107    /// Fraction, 0–1.
108    var precipitationChance: Double?
109    /// Millimetres since local midnight.
110    var precipitationToday: Double?
111}
112
113struct AudioContext: Codable, Hashable, Sendable {
114    enum Source: String, Codable, Sendable {
115        case microphone
116        case healthEnvironmental
117    }
118
119    var source: Source
120    /// Decibels. Microphone values are dBFS; Health values are dBA SPL.
121    var average: Double?
122    var peak: Double?
123}
124
125struct MotionContext: Codable, Hashable, Sendable {
126    /// Steps so far today.
127    var steps: Int?
128    var floorsAscended: Int?
129    var floorsDescended: Int?
130    /// Barometric pressure, kilopascals.
131    var pressure: Double?
132    /// Metres.
133    var relativeAltitude: Double?
134}
135
136struct DeviceContext: Codable, Hashable, Sendable {
137    enum BatteryState: String, Codable, Sendable {
138        case unplugged
139        case charging
140        case full
141    }
142
143    enum Connectivity: String, Codable, Sendable {
144        case wifi
145        case cellular
146        case none
147    }
148
149    /// Fraction, 0–1.
150    var batteryLevel: Double?
151    var batteryState: BatteryState?
152    var connectivity: Connectivity?
153}
154
155struct PhotosContext: Codable, Hashable, Sendable {
156    /// Photos taken since the previous report.
157    var count: Int
158    var photos: [PhotoMetadata] = []
159}
160
161struct PhotoMetadata: Codable, Hashable, Sendable {
162    var date: Date?
163    var coordinate: Coordinate?
164    var pixelWidth: Int?
165    var pixelHeight: Int?
166    var make: String?
167    var model: String?
168}