Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / apple / FindFamily / FindFamilyiOS / Model / Models.swift

Models.swift171 lines · 6.0 KiBRaw
1import Foundation
2import CoreLocation
3
4// MARK: - Coord
5
6struct Coord: Codable, Hashable, Equatable {
7 var lat: Double
8 var lon: Double
9
10 init(_ lat: Double, _ lon: Double) {
11 self.lat = lat
12 self.lon = lon
13 }
14 init(lat: Double, lon: Double) {
15 self.lat = lat
16 self.lon = lon
17 }
18
19 var clLocationCoordinate: CLLocationCoordinate2D {
20 CLLocationCoordinate2D(latitude: lat, longitude: lon)
21 }
22
23 static let zero = Coord(0, 0)
24}
25
26/// Great-circle distance in meters (Haversine).
27func haversine(_ p1: Coord, _ p2: Coord) -> Double {
28 let R = 6_371_000.0
29 let dLat = (p2.lat - p1.lat) * .pi / 180
30 let dLon = (p2.lon - p1.lon) * .pi / 180
31 let a = sin(dLat / 2) * sin(dLat / 2)
32 + cos(p1.lat * .pi / 180) * cos(p2.lat * .pi / 180) * sin(dLon / 2) * sin(dLon / 2)
33 let c = 2 * atan2(sqrt(a), sqrt(1 - a))
34 return R * c
35}
36
37// MARK: - Request status
38
39enum RequestStatus: String, Codable, CaseIterable {
40 case mutualConnection = "MUTUAL_CONNECTION"
41 case awaitingRequest = "AWAITING_REQUEST"
42 case awaitingResponse = "AWAITING_RESPONSE"
43}
44
45// MARK: - User
46
47struct User: Identifiable, Codable, Hashable, Equatable {
48 var id: Int64
49 var name: String
50 /// Either an "image-data:base64,..." string, a contact identifier, or nil.
51 var photo: String?
52 var locationName: String
53 var sendingEnabled: Bool
54 var requestStatus: RequestStatus
55 var lastLocationChangeTime: Date
56 /// Cached recipient public key (base64-encoded PEM).
57 var encryptionKey: String?
58 /// Peer device platform (`"android"` or `"ios"`), learned from incoming heartbeats.
59 /// Used by the UWB ranging path to pick same-platform vs cross-platform protocol.
60 var platform: String? = nil
61 /// Post-quantum public bundle (base64 of [4B kemLen][kemPubDer][dsaPubDer]), nullable for migration.
62 /// Same format as Android Office `Pqc.bundle` / `ff_pqc` key directory.
63 var pqcEncryptionKey: String? = nil
64 /// Id of the waypoint this user is currently inside, used for enter/exit hysteresis.
65 /// Nullable for migration and when the user is outside every waypoint.
66 var lastWaypointId: Int64? = nil
67 /// Deadline at which `sendingEnabled` auto-flips (the "Disable/Enable after" timer).
68 /// `nil` means "Never" (no pending auto-toggle). Nullable for migration.
69 var sharingAutoToggleAt: Date? = nil
70
71 static let empty = User(
72 id: 0, name: "", photo: nil, locationName: "Unnamed Location",
73 sendingEnabled: false, requestStatus: .mutualConnection,
74 lastLocationChangeTime: Date(), encryptionKey: nil, platform: nil, pqcEncryptionKey: nil,
75 lastWaypointId: nil, sharingAutoToggleAt: nil
76 )
77}
78
79// MARK: - Waypoint
80
81struct Waypoint: Identifiable, Codable, Hashable, Equatable {
82 var id: Int64
83 var name: String
84 /// Geofence radius in meters.
85 var range: Double
86 var coord: Coord
87
88 static let newWaypoint = Waypoint(id: 0, name: "", range: 100, coord: .zero)
89}
90
91// MARK: - LocationValue
92
93struct LocationValue: Identifiable, Codable, Hashable, Equatable {
94 var id: Int64
95 var userid: Int64
96 var coord: Coord
97 /// Meters per second.
98 var speed: Float
99 /// Horizontal accuracy in meters.
100 var acc: Float
101 var timestamp: Date
102 /// 0–100.
103 var battery: Float
104
105 /// Convert to wire form. ULong on the wire, epoch-millis timestamp.
106 func toCompatible(sleep: Bool? = nil, senderPlatform: String? = nil) -> LocationValueCompatible {
107 LocationValueCompatible(
108 id: UInt64(bitPattern: id),
109 userid: UInt64(bitPattern: userid),
110 coord: coord,
111 speed: speed,
112 acc: acc,
113 timestamp: Int64((timestamp.timeIntervalSince1970 * 1000).rounded()),
114 battery: battery,
115 sleep: sleep,
116 senderPlatform: senderPlatform
117 )
118 }
119}
120
121/// JSON wire format for /api/location/* (mirrors `LocationValueCompatible` in Modern-Apps).
122/// The Modern-Apps Kotlin model has a NESTED `coord` object (the Room `@Embedded` flattens
123/// to DB columns, but kotlinx.serialization keeps the field nested in JSON).
124///
125/// `id` and `sleep` are optional here because kotlinx.serialization's default
126/// `encodeDefaults = false` omits fields that equal their declared default value
127/// (`id = 0uL`, `sleep = null`), so the inbound JSON may not include them.
128struct LocationValueCompatible: Codable {
129 var id: UInt64?
130 var userid: UInt64
131 var coord: Coord
132 var speed: Float
133 var acc: Float
134 var timestamp: Int64 // epoch millis
135 var battery: Float
136 var sleep: Bool?
137 /// Sender's platform tag (`"android"` or `"ios"`). Optional for backward compatibility.
138 var senderPlatform: String?
139
140 func toLocationValue() -> LocationValue {
141 LocationValue(
142 id: Int64(bitPattern: id ?? 0),
143 userid: Int64(bitPattern: userid),
144 coord: coord,
145 speed: speed,
146 acc: acc,
147 timestamp: Date(timeIntervalSince1970: TimeInterval(timestamp) / 1000.0),
148 battery: battery
149 )
150 }
151}
152
153// MARK: - TemporaryLink
154
155struct TemporaryLink: Identifiable, Codable, Hashable, Equatable {
156 var id: Int64
157 var name: String
158 /// Base64(PEM) RSA private key — embedded in the share URL fragment, never sent to the server.
159 var key: String
160 /// Base64(PEM) RSA public key — registered for the link's id and used by senders.
161 var publicKey: String
162 var deleteAt: Date
163 /// PQC ephemeral public bundle (base64 [4B kemLen][kemPub][dsaPub]), nullable for migration.
164 var pqcPublicKey: String? = nil
165 /// Legacy: the full PQC private bundle (base64 [4B kemPrivLen][kemPriv][dsaPriv]) that older
166 /// builds put in the fragment as `#pqc_key=`. Only set on links created before [pqcSeed].
167 var pqcKey: String? = nil
168 /// The 32-byte ML-KEM link seed (base64url, unpadded) — what goes in the fragment as `#s=`.
169 /// This is the link's entire secret; the recipient's browser expands it into the keypair.
170 var pqcSeed: String? = nil
171}