craft-native 0.0.435 → 0.0.442

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE.md +30 -0
  2. package/README.md +35 -0
  3. package/dist/android/src/index.d.ts +8 -0
  4. package/dist/android/src/index.js +17 -2
  5. package/dist/android/templates/CraftHealthServices.kt.template +2 -0
  6. package/dist/android/templates/CraftNativeCapabilities.kt.template +42 -1
  7. package/dist/android/templates/CraftNativeMotion.kt.template +269 -0
  8. package/dist/android/templates/CraftNativeServices.kt.template +6 -1
  9. package/dist/android/templates/CraftPassiveSteps.kt.template +159 -0
  10. package/dist/android/templates/CraftRecordingPressure.kt.template +156 -0
  11. package/dist/android/templates/LocationRecordingService.kt.template +77 -9
  12. package/dist/android/templates/MainActivityNative.kt.template +129 -1
  13. package/dist/cli.js +289 -108
  14. package/dist/components/index.d.ts +25 -0
  15. package/dist/index.cjs +207 -27
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.js +321 -140
  19. package/dist/installer-background.d.ts +2 -0
  20. package/dist/installer-layout.d.ts +1 -0
  21. package/dist/ios/src/index.d.ts +10 -0
  22. package/dist/ios/src/index.js +28 -2
  23. package/dist/ios/templates/CraftApp.swift +14 -0
  24. package/dist/ios/templates/CraftHealth.swift +2 -1
  25. package/dist/ios/templates/CraftHealthBackground.swift +308 -0
  26. package/dist/ios/templates/CraftNativeActions.swift +153 -0
  27. package/dist/ios/templates/CraftNativeMutation.swift +18 -3
  28. package/dist/ios/templates/CraftNativePedometer.swift +181 -0
  29. package/dist/ios/templates/CraftNativeScreen.swift +76 -0
  30. package/dist/ios/templates/testing/DriverTests.swift.template +24 -4
  31. package/dist/macos/src/index.js +1 -1
  32. package/dist/macos/src/toolchain.d.ts +1 -1
  33. package/dist/macos/templates/CraftMacApplication.swift +124 -39
  34. package/dist/macos/templates/CraftMacRenderer.swift +169 -37
  35. package/dist/macos/templates/CraftMacRuntime.swift +298 -77
  36. package/dist/macos/templates/CraftNativeMutation.swift +18 -3
  37. package/dist/package.d.ts +40 -0
  38. package/dist/types.d.ts +26 -0
  39. package/package.json +1 -1
@@ -0,0 +1,2 @@
1
+ /** Shared drag-to-Applications artwork, embedded so packaged SDKs need no external files. */
2
+ export declare const installerBackground: Buffer;
@@ -0,0 +1 @@
1
+ export declare function writeInstallerLayout(mount: string, volume: string, appName: string): void;
@@ -84,12 +84,20 @@ export interface CraftConfig {
84
84
  enableAudioRecording?: boolean;
85
85
  enableVideoRecording?: boolean;
86
86
  enableMotionSensors?: boolean;
87
+ /** Core Motion relative altitude and pressure; includes the motion usage description. */
88
+ enableBarometer?: boolean;
89
+ /** Core Motion interval steps, cadence and seven-day device history. */
90
+ enablePedometer?: boolean;
87
91
  /** Shaking the phone raises a `shake` event. Arms the accelerometer while the app is in front. */
88
92
  enableShakeGesture?: boolean;
89
93
  enableLocalDatabase?: boolean;
90
94
  enableBluetooth?: boolean;
91
95
  enableNFC?: boolean;
92
96
  enableHealthKit?: boolean;
97
+ /** Observe declared HealthKit types on background wakes; requires enableHealthKit. */
98
+ enableHealthKitBackground?: boolean;
99
+ healthKitBackgroundTypes?: Array<'steps' | 'heartRate' | 'restingHeartRate' | 'heartRateVariability' | 'activeEnergy' | 'distance' | 'bodyMass' | 'sleep' | 'workouts'>;
100
+ healthKitBackgroundFrequency?: 'immediate' | 'hourly' | 'daily' | 'weekly';
93
101
  /** Native Apple Wallet pass presentation and lookup for native screens. */
94
102
  enableWallet?: boolean;
95
103
  enableLiveActivities?: boolean;
@@ -361,6 +369,8 @@ export declare function hybridConfigProblems(config: Pick<CraftConfig, 'renderer
361
369
  * well as the one-file bundle, and the app runs each screen from its own
362
370
  * route's file. Stacks asks for route bundles only when this is true.
363
371
  */
372
+ /** Build-time proof for Stacks capability mappings, including unreleased source builds. */
373
+ export declare const nativeSensorCapabilities: readonly ['barometer', 'pedometer', 'healthKitBackground'];
364
374
  export declare const nativeRouteBundles = true;
365
375
  /**
366
376
  * The app evaluates stx's shared runtime (`__runtime.js`, stx's
@@ -18,7 +18,7 @@ function iosPermissions(config) {
18
18
  contacts: config.enableContacts,
19
19
  calendar: config.enableCalendar,
20
20
  bluetooth: config.enableBluetooth || config.enableHeartRate,
21
- motion: config.enableMotionSensors || config.enableShakeGesture,
21
+ motion: config.enableMotionSensors || config.enableShakeGesture || config.enableBarometer || config.enablePedometer,
22
22
  biometric: config.enableBiometric,
23
23
  music: config.enableAppleMusic,
24
24
  health: config.enableHealthKit,
@@ -618,6 +618,7 @@ function hybridConfigProblems(config) {
618
618
  problems.push(`nativeScriptThread "${String(config.nativeScriptThread)}" must be "main" or "background".`);
619
619
  return problems;
620
620
  }
621
+ var nativeSensorCapabilities = ["barometer", "pedometer", "healthKitBackground"];
621
622
  var nativeRouteBundles = true;
622
623
  var nativeRouteRuntime = true;
623
624
  var nativeMessages = true;
@@ -701,11 +702,16 @@ var DEFAULT_CONFIG = {
701
702
  enableAudioRecording: false,
702
703
  enableVideoRecording: false,
703
704
  enableMotionSensors: false,
705
+ enableBarometer: false,
706
+ enablePedometer: false,
704
707
  enableShakeGesture: false,
705
708
  enableLocalDatabase: false,
706
709
  enableBluetooth: false,
707
710
  enableNFC: false,
708
711
  enableHealthKit: false,
712
+ enableHealthKitBackground: false,
713
+ healthKitBackgroundTypes: [],
714
+ healthKitBackgroundFrequency: "hourly",
709
715
  enableWallet: false,
710
716
  enableLiveActivities: false,
711
717
  enableWatchApp: false,
@@ -762,7 +768,7 @@ function renderUsageDescriptions(config) {
762
768
  [config.enableContacts, "NSContactsUsageDescription", `${config.appName} accesses contacts only when you choose a contact feature.`],
763
769
  [config.enableCalendar, "NSCalendarsUsageDescription", `${config.appName} accesses your calendar only when you choose a calendar feature.`],
764
770
  [config.enableBluetooth || config.enableHeartRate, "NSBluetoothAlwaysUsageDescription", config.enableHeartRate ? `${config.appName} connects to your heart-rate monitor over Bluetooth to show and record your heart rate during a workout.` : `${config.appName} uses Bluetooth to connect to nearby devices.`],
765
- [config.enableMotionSensors, "NSMotionUsageDescription", `${config.appName} uses motion data for activity features.`],
771
+ [config.enableMotionSensors || config.enableBarometer || config.enablePedometer, "NSMotionUsageDescription", `${config.appName} uses motion data for activity features.`],
766
772
  [config.enableNFC, "NFCReaderUsageDescription", `${config.appName} reads NFC tags when you start a scan.`],
767
773
  [config.enableLocalNetwork, "NSLocalNetworkUsageDescription", `${config.appName} connects to your own devices on this network.`],
768
774
  [config.enableHealthKit, "NSHealthShareUsageDescription", `${config.appName} reads health data you choose to share.`],
@@ -871,8 +877,24 @@ ${plistArray([identifier])}
871
877
  function associatedDomainEntries(domains) {
872
878
  return [...new Set((domains ?? []).map((value) => value.trim()).filter(Boolean).map((value) => /^[a-z]+:/i.test(value) ? value : `applinks:${value}`))];
873
879
  }
880
+ function validateHealthKitBackground(config) {
881
+ if (!config.enableHealthKitBackground)
882
+ return;
883
+ if (!config.enableHealthKit)
884
+ throw new Error("enableHealthKitBackground requires enableHealthKit");
885
+ const types = config.healthKitBackgroundTypes;
886
+ const supported = ["steps", "heartRate", "restingHeartRate", "heartRateVariability", "activeEnergy", "distance", "bodyMass", "sleep", "workouts"];
887
+ if (!Array.isArray(types) || !types.length || types.some((type) => !supported.includes(type)))
888
+ throw new Error("healthKitBackgroundTypes must declare supported HealthKit sample types");
889
+ if (!["immediate", "hourly", "daily", "weekly"].includes(config.healthKitBackgroundFrequency ?? "hourly"))
890
+ throw new Error("healthKitBackgroundFrequency must be immediate, hourly, daily or weekly");
891
+ }
874
892
  function renderEntitlements(config) {
893
+ validateHealthKitBackground(config);
875
894
  const entries = [];
895
+ if (config.enableHealthKitBackground)
896
+ entries.push(` <key>com.apple.developer.healthkit.background-delivery</key>
897
+ <true/>`);
876
898
  const associatedDomains = associatedDomainEntries(config.associatedDomains);
877
899
  if (associatedDomains.length) {
878
900
  entries.push(` <key>com.apple.developer.associated-domains</key>
@@ -1358,6 +1380,8 @@ function writeShell(output, config) {
1358
1380
  writeSource(join4(output, "Sources", "CraftNativeScreen.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftNativeScreen.swift"), "utf8"));
1359
1381
  writeSource(join4(output, "Sources", "CraftNativeFlatList.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftNativeFlatList.swift"), "utf8"));
1360
1382
  writeSource(join4(output, "Sources", "CraftNativeActions.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftNativeActions.swift"), "utf8"));
1383
+ writeSource(join4(output, "Sources", "CraftNativePedometer.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftNativePedometer.swift"), "utf8"));
1384
+ writeSource(join4(output, "Sources", "CraftHealthBackground.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftHealthBackground.swift"), "utf8"));
1361
1385
  writeSource(join4(output, "Sources", "CraftHealth.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftHealth.swift"), "utf8"));
1362
1386
  writeSource(join4(output, "Sources", "CraftMusic.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftMusic.swift"), "utf8"));
1363
1387
  writeSource(join4(output, "Sources", "CraftBluetooth.swift"), readFileSync3(join4(TEMPLATES_DIR, "CraftBluetooth.swift"), "utf8"));
@@ -1424,6 +1448,7 @@ async function init(options) {
1424
1448
  teamId: teamId || "",
1425
1449
  ...given
1426
1450
  };
1451
+ validateHealthKitBackground(config);
1427
1452
  if (config.renderer !== "web" && config.renderer !== "native") {
1428
1453
  throw new Error(`Unknown iOS renderer: ${config.renderer}. Expected web or native.`);
1429
1454
  }
@@ -1784,6 +1809,7 @@ export {
1784
1809
  nativeMessages,
1785
1810
  nativeRouteBundles,
1786
1811
  nativeRouteRuntime,
1812
+ nativeSensorCapabilities,
1787
1813
  nativeStartPath,
1788
1814
  normalizeNativePath,
1789
1815
  open,
@@ -299,6 +299,15 @@ final class CraftAppDelegate: NSObject, UIApplicationDelegate, UNUserNotificatio
299
299
  // push, a refresh, a route recording's location) draws nothing, and
300
300
  // its events still need someone to hear them.
301
301
  CraftHybrid.shared.configure(config)
302
+ CraftHealthBackground.shared.emit = { type in
303
+ let detail: [String: Any] = ["type": type]
304
+ CraftEventManager.shared.sendToWeb("craftHealthBackground", data: detail)
305
+ NotificationCenter.default.post(name: CraftWebServices.event, object: nil,
306
+ userInfo: ["event": "craftHealthBackground", "detail": detail])
307
+ }
308
+ CraftHealthBackground.shared.configure(types: config.enableHealthKit && config.enableHealthKitBackground ? config.healthKitBackgroundTypes : [],
309
+ frequency: config.healthKitBackgroundFrequency)
310
+
302
311
  if let shortcuts = config.appShortcuts {
303
312
  application.shortcutItems = shortcuts.prefix(4).map { shortcut in
304
313
  UIApplicationShortcutItem(type: shortcut.id, localizedTitle: shortcut.title, localizedSubtitle: shortcut.subtitle,
@@ -1830,6 +1839,8 @@ struct CraftConfig: Codable {
1830
1839
  var enableAudioRecording: Bool = false
1831
1840
  var enableVideoRecording: Bool = false
1832
1841
  var enableMotionSensors: Bool = false
1842
+ var enableBarometer: Bool = false
1843
+ var enablePedometer: Bool = false
1833
1844
  /// Shaking the phone raises a `shake` event, for a report-a-problem
1834
1845
  /// gesture. Its own flag rather than a corner of `enableMotionSensors`,
1835
1846
  /// because it arms the accelerometer for as long as the app is in front:
@@ -1839,6 +1850,9 @@ struct CraftConfig: Codable {
1839
1850
  var enableBluetooth: Bool = false
1840
1851
  var enableNFC: Bool = false
1841
1852
  var enableHealthKit: Bool = false
1853
+ var enableHealthKitBackground: Bool = false
1854
+ var healthKitBackgroundTypes: [String] = []
1855
+ var healthKitBackgroundFrequency: String = "hourly"
1842
1856
  /// Apple Wallet pass presentation and lookup for native screens.
1843
1857
  var enableWallet: Bool = false
1844
1858
  var enableLiveActivities: Bool = false
@@ -227,6 +227,7 @@ final class CraftHealth {
227
227
  if readOnly { shareTypes.removeAll() }
228
228
  healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { [weak self] success, error in
229
229
  if success {
230
+ DispatchQueue.main.async { CraftHealthBackground.shared.refresh() }
230
231
  completion(.success(true))
231
232
  } else {
232
233
  completion(.failure(CraftHealthError(message: error?.localizedDescription ?? "Authorization failed")))
@@ -401,7 +402,7 @@ final class CraftHealth {
401
402
 
402
403
  /// The quantity behind each health data type, its unit, and the one
403
404
  /// statistic that type supports.
404
- private static func healthQuantity(_ type: String) -> (type: HKQuantityType?, unit: HKUnit, options: HKStatisticsOptions)? {
405
+ static func healthQuantity(_ type: String) -> (type: HKQuantityType?, unit: HKUnit, options: HKStatisticsOptions)? {
405
406
  let bpm = HKUnit.count().unitDivided(by: .minute())
406
407
  switch type {
407
408
  case "steps": return (HKQuantityType.quantityType(forIdentifier: .stepCount), .count(), .cumulativeSum)
@@ -0,0 +1,308 @@
1
+ import Foundation
2
+ #if CRAFT_PERMISSION_HEALTH
3
+ import HealthKit
4
+ #endif
5
+
6
+ struct CraftHealthBackgroundSample: Codable {
7
+ let id: String
8
+ let startDate: Double
9
+ let endDate: Double
10
+ var value: Double?
11
+ var unit: String?
12
+ var category: Int?
13
+ var workoutActivity: UInt?
14
+ var durationSeconds: Double?
15
+ }
16
+
17
+ struct CraftHealthBackgroundPage {
18
+ let anchor: Data
19
+ let samples: [CraftHealthBackgroundSample]
20
+ let deleted: [String]
21
+ let more: Bool
22
+ }
23
+
24
+ struct CraftHealthBackgroundBatch: Codable {
25
+ let id: String
26
+ let type: String
27
+ let samples: [CraftHealthBackgroundSample]
28
+ let deleted: [String]
29
+ }
30
+
31
+ struct CraftHealthBackgroundState: Codable {
32
+ var anchors: [String: Data] = [:]
33
+ var observedTypes: [String]? = nil
34
+ var batches: [CraftHealthBackgroundBatch] = []
35
+ }
36
+
37
+ /// An atomic snapshot keeps a new anchor inseparable from its unconsumed delta.
38
+ protocol CraftHealthBackgroundPersistence {
39
+ func load() throws -> CraftHealthBackgroundState
40
+ func save(_ state: CraftHealthBackgroundState) throws
41
+ }
42
+
43
+ final class CraftHealthBackgroundFile: CraftHealthBackgroundPersistence {
44
+ private let url: URL
45
+ init(url: URL) { self.url = url }
46
+ func load() throws -> CraftHealthBackgroundState {
47
+ guard FileManager.default.fileExists(atPath: url.path) else { return CraftHealthBackgroundState() }
48
+ return try JSONDecoder().decode(CraftHealthBackgroundState.self, from: Data(contentsOf: url))
49
+ }
50
+ func save(_ state: CraftHealthBackgroundState) throws {
51
+ let data = try JSONEncoder().encode(state)
52
+ guard data.count <= 8 * 1024 * 1024 else { throw CraftHealthBackgroundError.outboxFull }
53
+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true,
54
+ attributes: [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication])
55
+ try data.write(to: url, options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
56
+ var excluded = url
57
+ var values = URLResourceValues()
58
+ values.isExcludedFromBackup = true
59
+ try excluded.setResourceValues(values)
60
+ }
61
+ }
62
+
63
+ enum CraftHealthBackgroundError: Error { case unavailable, outboxFull, invalidAnchor, missingAnchor }
64
+
65
+ /// All callbacks and ownership transitions are serialized on the main queue.
66
+ protocol CraftHealthBackgroundSource {
67
+ func observe(type: String, update: @escaping (Error?, @escaping () -> Void) -> Void) -> () -> Void
68
+ func enable(type: String, frequency: String, completion: @escaping (Error?) -> Void)
69
+ func disable(type: String)
70
+ func read(type: String, anchor: Data?, completion: @escaping (Result<CraftHealthBackgroundPage, Error>) -> Void) -> () -> Void
71
+ }
72
+
73
+ final class CraftHealthBackground {
74
+ static let shared: CraftHealthBackground = {
75
+ let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
76
+ #if CRAFT_PERMISSION_HEALTH
77
+ let source: CraftHealthBackgroundSource = CraftHealthKitBackgroundSource()
78
+ #else
79
+ let source: CraftHealthBackgroundSource = CraftUnavailableHealthBackgroundSource()
80
+ #endif
81
+ return CraftHealthBackground(source: source, persistence: CraftHealthBackgroundFile(url: base.appendingPathComponent("craft-health/delivery.json")))
82
+ }()
83
+
84
+ private final class Read {
85
+ var completions: [() -> Void] = []
86
+ var cancel: (() -> Void)?
87
+ var deadline: DispatchWorkItem?
88
+ var dirty = false
89
+ }
90
+ private let readTimeout: TimeInterval
91
+ private let source: CraftHealthBackgroundSource
92
+ private let persistence: CraftHealthBackgroundPersistence
93
+ private var state = CraftHealthBackgroundState()
94
+ private var loaded = false
95
+ private var observers: [String: () -> Void] = [:]
96
+ private var reads: [String: Read] = [:]
97
+ private var types: [String] = []
98
+ private var frequency = "hourly"
99
+ private var generation = UUID()
100
+ private(set) var errors: [String: String] = [:]
101
+ var emit: (String) -> Void = { _ in }
102
+
103
+ init(source: CraftHealthBackgroundSource, persistence: CraftHealthBackgroundPersistence, readTimeout: TimeInterval = 20) {
104
+ self.source = source
105
+ self.persistence = persistence
106
+ self.readTimeout = readTimeout
107
+ }
108
+
109
+ func configure(types: [String], frequency: String) {
110
+ stop()
111
+ self.types = Array(Set(types)).sorted()
112
+ self.frequency = frequency
113
+ refresh()
114
+ }
115
+
116
+ func refresh() {
117
+ if !loaded {
118
+ do { state = try persistence.load(); loaded = true }
119
+ catch { errors["storage"] = "Health delivery state could not be read"; return }
120
+ }
121
+ errors.removeValue(forKey: "storage")
122
+ let current = generation
123
+ let observed = Set(state.observedTypes ?? []).union(state.anchors.keys).union(types)
124
+ if Set(state.observedTypes ?? []) != observed {
125
+ do {
126
+ var next = state
127
+ next.observedTypes = observed.sorted()
128
+ try persistence.save(next)
129
+ state = next
130
+ } catch { errors["storage"] = "Health registration intent could not be persisted"; return }
131
+ }
132
+ // Keep retired declarations so a temporarily unavailable store can
133
+ // retry disabling them on a later launch, even before its first delta.
134
+ for old in observed where !types.contains(old) { source.disable(type: old) }
135
+ for type in types {
136
+ if observers[type] == nil {
137
+ observers[type] = source.observe(type: type) { [weak self] error, acknowledge in
138
+ guard let self, self.generation == current else { acknowledge(); return }
139
+ if error != nil { self.errors[type] = "HealthKit observer failed"; acknowledge(); return }
140
+ self.changed(type, acknowledge: acknowledge)
141
+ }
142
+ }
143
+ source.enable(type: type, frequency: frequency) { [weak self] error in
144
+ guard let self, self.generation == current else { return }
145
+ if error != nil { self.errors["\(type).registration"] = "Background delivery registration failed" }
146
+ else { self.errors.removeValue(forKey: "\(type).registration"); self.changed(type, acknowledge: {}) }
147
+ }
148
+ }
149
+ }
150
+
151
+ private func changed(_ type: String, acknowledge: @escaping () -> Void) {
152
+ if let read = reads[type] { read.completions.append(acknowledge); read.dirty = true; return }
153
+ let read = Read()
154
+ read.completions.append(acknowledge)
155
+ reads[type] = read
156
+ // Never strand HealthKit's completion waiting for an unavailable store,
157
+ // a locked device, an unresponsive query or JavaScript consumption.
158
+ let deadline = DispatchWorkItem { [weak self, weak read] in
159
+ guard let self, let read, self.reads[type] === read else { return }
160
+ self.errors[type] = "HealthKit read timed out; anchor retained for retry"
161
+ self.finish(type, read)
162
+ }
163
+ read.deadline = deadline
164
+ DispatchQueue.main.asyncAfter(deadline: .now() + readTimeout, execute: deadline)
165
+ next(type, read)
166
+ }
167
+
168
+ private func next(_ type: String, _ read: Read) {
169
+ read.dirty = false
170
+ read.cancel = source.read(type: type, anchor: state.anchors[type]) { [weak self, weak read] answer in
171
+ guard let self, let read, self.reads[type] === read else { return }
172
+ read.cancel = nil
173
+ switch answer {
174
+ case .failure:
175
+ self.errors[type] = "HealthKit read failed; anchor retained for retry"
176
+ self.finish(type, read)
177
+ case .success(let page):
178
+ do {
179
+ var next = self.state
180
+ if !page.samples.isEmpty || !page.deleted.isEmpty {
181
+ next.batches.append(CraftHealthBackgroundBatch(id: UUID().uuidString, type: type, samples: page.samples, deleted: page.deleted))
182
+ }
183
+ next.anchors[type] = page.anchor
184
+ try self.persistence.save(next)
185
+ self.state = next
186
+ self.errors.removeValue(forKey: type)
187
+ if !page.samples.isEmpty || !page.deleted.isEmpty { self.emit(type) }
188
+ if page.more || read.dirty { self.next(type, read) }
189
+ else { self.finish(type, read) }
190
+ } catch {
191
+ self.errors[type] = "Health delivery could not be persisted; anchor retained for retry"
192
+ self.finish(type, read)
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ private func finish(_ type: String, _ read: Read) {
199
+ guard reads[type] === read else { return }
200
+ reads.removeValue(forKey: type)
201
+ read.deadline?.cancel()
202
+ read.cancel?()
203
+ let completions = read.completions
204
+ read.completions.removeAll()
205
+ completions.forEach { $0() }
206
+ }
207
+
208
+ func stop() {
209
+ generation = UUID()
210
+ observers.values.forEach { $0() }
211
+ observers.removeAll()
212
+ for (type, read) in reads { finish(type, read) }
213
+ }
214
+
215
+ func perform(method: String, args: [Any], completion: @escaping (Result<Any, CraftNativeActionError>) -> Void) {
216
+ if !loaded { refresh() }
217
+ guard loaded else { completion(.failure(CraftNativeActionError(code: "STORAGE_ERROR", message: "Health delivery state is unavailable"))); return }
218
+ do {
219
+ switch method {
220
+ case "status": completion(.success(["types": types, "frequency": frequency, "pendingBatches": state.batches.count, "errors": errors]))
221
+ case "refresh": refresh(); completion(.success(true))
222
+ case "pending":
223
+ let data = try JSONEncoder().encode(Array(state.batches.prefix(20)))
224
+ completion(.success(try JSONSerialization.jsonObject(with: data)))
225
+ case "acknowledge":
226
+ guard let ids = args.first as? [String], ids.count <= 20 else {
227
+ completion(.failure(CraftNativeActionError(code: "INVALID_ARGUMENT", message: "acknowledge needs up to 20 batch ids"))); return
228
+ }
229
+ var next = state
230
+ let acknowledged = Set(ids)
231
+ next.batches.removeAll { acknowledged.contains($0.id) }
232
+ try persistence.save(next)
233
+ state = next
234
+ refresh()
235
+ completion(.success(true))
236
+ default: completion(.failure(CraftNativeActionError(code: "UNKNOWN_ACTION", message: "Unknown HealthBackground action")))
237
+ }
238
+ } catch { completion(.failure(CraftNativeActionError(code: "STORAGE_ERROR", message: "Health delivery state could not be saved"))) }
239
+ }
240
+ }
241
+
242
+ private final class CraftUnavailableHealthBackgroundSource: CraftHealthBackgroundSource {
243
+ func disable(type: String) {}
244
+ func observe(type: String, update: @escaping (Error?, @escaping () -> Void) -> Void) -> () -> Void { {} }
245
+ func enable(type: String, frequency: String, completion: @escaping (Error?) -> Void) { completion(CraftHealthBackgroundError.unavailable) }
246
+ func read(type: String, anchor: Data?, completion: @escaping (Result<CraftHealthBackgroundPage, Error>) -> Void) -> () -> Void { completion(.failure(CraftHealthBackgroundError.unavailable)); return {} }
247
+ }
248
+
249
+ #if CRAFT_PERMISSION_HEALTH
250
+ private final class CraftHealthKitBackgroundSource: CraftHealthBackgroundSource {
251
+ private var store: HKHealthStore? { CraftHealth.shared.healthStore }
252
+ private func sampleType(_ name: String) -> HKSampleType? {
253
+ if name == "sleep" { return HKCategoryType.categoryType(forIdentifier: .sleepAnalysis) }
254
+ if name == "workouts" { return HKObjectType.workoutType() }
255
+ return CraftHealth.healthQuantity(name)?.type
256
+ }
257
+ func disable(type: String) {
258
+ guard let store, let sample = sampleType(type) else { return }
259
+ store.disableBackgroundDelivery(for: sample) { _, _ in }
260
+ }
261
+ func observe(type: String, update: @escaping (Error?, @escaping () -> Void) -> Void) -> () -> Void {
262
+ guard let store, let sample = sampleType(type) else { return {} }
263
+ let query = HKObserverQuery(sampleType: sample, predicate: nil) { _, completion, error in
264
+ DispatchQueue.main.async { update(error, completion) }
265
+ }
266
+ store.execute(query)
267
+ return { store.stop(query) }
268
+ }
269
+ func enable(type: String, frequency: String, completion: @escaping (Error?) -> Void) {
270
+ guard let store, let sample = sampleType(type) else { completion(CraftHealthBackgroundError.unavailable); return }
271
+ let value: HKUpdateFrequency = ["immediate": .immediate, "hourly": .hourly, "daily": .daily, "weekly": .weekly][frequency] ?? .hourly
272
+ store.enableBackgroundDelivery(for: sample, frequency: value) { success, error in
273
+ DispatchQueue.main.async { completion(success ? nil : error ?? CraftHealthBackgroundError.unavailable) }
274
+ }
275
+ }
276
+ func read(type: String, anchor: Data?, completion: @escaping (Result<CraftHealthBackgroundPage, Error>) -> Void) -> () -> Void {
277
+ guard let store, let sample = sampleType(type) else { completion(.failure(CraftHealthBackgroundError.unavailable)); return {} }
278
+ let saved: HKQueryAnchor?
279
+ do {
280
+ saved = try anchor.map { data in
281
+ guard let value = try NSKeyedUnarchiver.unarchivedObject(ofClass: HKQueryAnchor.self, from: data) else { throw CraftHealthBackgroundError.invalidAnchor }
282
+ return value
283
+ }
284
+ } catch { completion(.failure(error)); return {} }
285
+ let query = HKAnchoredObjectQuery(type: sample, predicate: nil, anchor: saved, limit: 250) { _, samples, deleted, anchor, error in
286
+ let answer: Result<CraftHealthBackgroundPage, Error>
287
+ do {
288
+ if let error { throw error }
289
+ guard let anchor else { throw CraftHealthBackgroundError.missingAnchor }
290
+ let data = try NSKeyedArchiver.archivedData(withRootObject: anchor, requiringSecureCoding: true)
291
+ let records = (samples ?? []).map { sample -> CraftHealthBackgroundSample in
292
+ var record = CraftHealthBackgroundSample(id: sample.uuid.uuidString, startDate: sample.startDate.timeIntervalSince1970 * 1000, endDate: sample.endDate.timeIntervalSince1970 * 1000)
293
+ if let quantity = sample as? HKQuantitySample, let spec = CraftHealth.healthQuantity(type) {
294
+ record.value = quantity.quantity.doubleValue(for: spec.unit)
295
+ record.unit = spec.unit.unitString
296
+ } else if let category = sample as? HKCategorySample { record.category = category.value }
297
+ else if let workout = sample as? HKWorkout { record.workoutActivity = workout.workoutActivityType.rawValue; record.durationSeconds = workout.duration }
298
+ return record
299
+ }
300
+ answer = .success(CraftHealthBackgroundPage(anchor: data, samples: records, deleted: (deleted ?? []).map { $0.uuid.uuidString }, more: records.count + (deleted?.count ?? 0) >= 250))
301
+ } catch { answer = .failure(error) }
302
+ DispatchQueue.main.async { completion(answer) }
303
+ }
304
+ store.execute(query)
305
+ return { store.stop(query) }
306
+ }
307
+ }
308
+ #endif
@@ -1,4 +1,7 @@
1
1
  import Foundation
2
+ #if CRAFT_PERMISSION_MOTION
3
+ import CoreMotion
4
+ #endif
2
5
  import AuthenticationServices
3
6
  #if CRAFT_PERMISSION_BIOMETRIC
4
7
  import LocalAuthentication
@@ -60,6 +63,9 @@ enum CraftNativeActions {
60
63
  if config.enableDeepLinks { values.append("deepLinks") }
61
64
  if config.enableLocalNotifications { values.append("notifications") }
62
65
  if config.enableHealthKit { values.append("health") }
66
+ if config.enableHealthKit && config.enableHealthKitBackground { values.append("healthBackground") }
67
+ if config.enableBarometer { values.append("barometer") }
68
+ if config.enablePedometer { values.append("pedometer") }
63
69
  if config.enableWallet { values.append("wallet") }
64
70
  if config.enableLiveActivities { values.append("liveActivities") }
65
71
  // The in-app browser and the system's web sign-in sheet (CraftBrowser).
@@ -405,6 +411,17 @@ enum CraftNativeActions {
405
411
  complete(requestToken, result, completion)
406
412
  }
407
413
  return
414
+ case ("HealthBackground", _):
415
+ guard config.enableHealthKit && config.enableHealthKitBackground else {
416
+ complete(requestToken, failure("CAPABILITY_DISABLED", "HealthKit background delivery is disabled"), completion)
417
+ return
418
+ }
419
+ DispatchQueue.main.async {
420
+ CraftHealthBackground.shared.perform(method: method, args: args) { result in
421
+ complete(requestToken, result, completion)
422
+ }
423
+ }
424
+ return
408
425
  case ("Health", _):
409
426
  // Apple Health, the same as a page's `craft.health` (CraftHealth.swift).
410
427
  guard config.enableHealthKit else {
@@ -1999,3 +2016,139 @@ enum CraftHaptics {
1999
2016
  return generator
2000
2017
  }
2001
2018
  }
2019
+
2020
+ // MARK: - Route-owned barometer
2021
+
2022
+ struct CraftBarometerSample {
2023
+ let pressureHpa: Double
2024
+ let relativeAltitudeMeters: Double
2025
+ let timestampMs: Double
2026
+ }
2027
+
2028
+ protocol CraftBarometerSource: AnyObject {
2029
+ var available: Bool { get }
2030
+ var denied: Bool { get }
2031
+ func start(_ receive: @escaping (Result<CraftBarometerSample, Error>) -> Void) throws
2032
+ func stop()
2033
+ }
2034
+
2035
+ #if CRAFT_PERMISSION_MOTION
2036
+ private final class CraftCoreMotionBarometer: CraftBarometerSource {
2037
+ private let altimeter = CMAltimeter()
2038
+ var available: Bool { CMAltimeter.isRelativeAltitudeAvailable() }
2039
+ var denied: Bool { [.denied, .restricted].contains(CMAltimeter.authorizationStatus()) }
2040
+ func start(_ receive: @escaping (Result<CraftBarometerSample, Error>) -> Void) throws {
2041
+ altimeter.startRelativeAltitudeUpdates(to: .main) { data, error in
2042
+ if let error { receive(.failure(error)); return }
2043
+ guard let data else { return }
2044
+ receive(.success(CraftBarometerSample(pressureHpa: data.pressure.doubleValue * 10,
2045
+ relativeAltitudeMeters: data.relativeAltitude.doubleValue, timestampMs: data.timestamp * 1000)))
2046
+ }
2047
+ }
2048
+ func stop() { altimeter.stopRelativeAltitudeUpdates() }
2049
+ }
2050
+ #endif
2051
+
2052
+ private final class CraftUnavailableBarometer: CraftBarometerSource {
2053
+ var available: Bool { false }
2054
+ var denied: Bool { false }
2055
+ func start(_ receive: @escaping (Result<CraftBarometerSample, Error>) -> Void) throws {}
2056
+ func stop() {}
2057
+ }
2058
+
2059
+ final class CraftNativeBarometer {
2060
+ private final class Stream {
2061
+ let source: CraftBarometerSource
2062
+ var lastTimestamp: Double?
2063
+ init(_ source: CraftBarometerSource) { self.source = source }
2064
+ }
2065
+ private let makeSource: () -> CraftBarometerSource
2066
+ private var stream: Stream?
2067
+ private var closed = false
2068
+ var emit: ([String: Any]) -> Void = { _ in }
2069
+
2070
+ init(makeSource: (() -> CraftBarometerSource)? = nil) {
2071
+ self.makeSource = makeSource ?? {
2072
+ #if CRAFT_PERMISSION_MOTION
2073
+ return CraftCoreMotionBarometer()
2074
+ #else
2075
+ return CraftUnavailableBarometer()
2076
+ #endif
2077
+ }
2078
+ }
2079
+
2080
+ func perform(method: String, args: [Any]) -> Result<Any, CraftNativeActionError> {
2081
+ guard !closed else { return failure("HOST_CLOSED", "Barometer host is closed") }
2082
+ switch method {
2083
+ case "getAvailability":
2084
+ let source = makeSource()
2085
+ return .success(["relativeAltitude": source.available, "absoluteAltitude": false,
2086
+ "background": false, "permissionDenied": source.denied])
2087
+ case "start": return start(args)
2088
+ case "stop": return .success(stop(reason: "requested"))
2089
+ default: return failure("UNKNOWN_ACTION", "Unsupported Barometer method")
2090
+ }
2091
+ }
2092
+
2093
+ private func start(_ args: [Any]) -> Result<Any, CraftNativeActionError> {
2094
+ guard args.isEmpty || args.first is [String: Any] else { return failure("INVALID_ARGUMENT", "Barometer.start needs an options object") }
2095
+ let options = args.first as? [String: Any] ?? [:]
2096
+ if let background = options["background"] {
2097
+ guard let flag = background as? NSNumber, CFGetTypeID(flag) == CFBooleanGetTypeID() else {
2098
+ return failure("INVALID_ARGUMENT", "background must be boolean")
2099
+ }
2100
+ if flag.boolValue { return failure("UNSUPPORTED", "Background barometer recording is not supported") }
2101
+ }
2102
+ let value = options["intervalMs"] ?? 100
2103
+ guard let number = value as? NSNumber, CFGetTypeID(number) != CFBooleanGetTypeID(),
2104
+ number.doubleValue.isFinite, number.doubleValue >= 5, number.doubleValue <= 60000,
2105
+ number.doubleValue.rounded() == number.doubleValue else {
2106
+ return failure("INVALID_ARGUMENT", "intervalMs must be an integer from 5 through 60000")
2107
+ }
2108
+ let source = makeSource()
2109
+ guard source.available else { return failure("UNAVAILABLE", "Barometer is not present") }
2110
+ guard !source.denied else { return failure("PERMISSION_DENIED", "Motion access was denied or restricted") }
2111
+ let next = Stream(source)
2112
+ do {
2113
+ try source.start { [weak self, weak next] result in
2114
+ guard let self, let next, !self.closed, self.stream === next else { return }
2115
+ switch result {
2116
+ case .failure:
2117
+ self.stop(reason: next.source.denied ? "permissionDenied" : "sensorError")
2118
+ case .success(let sample):
2119
+ guard sample.pressureHpa.isFinite, sample.pressureHpa > 0,
2120
+ sample.relativeAltitudeMeters.isFinite, sample.timestampMs.isFinite else {
2121
+ self.stop(reason: "sensorError")
2122
+ return
2123
+ }
2124
+ // Core Motion chooses its own cadence. A requested slower
2125
+ // output rate never invents or interpolates sensor readings.
2126
+ if let last = next.lastTimestamp, sample.timestampMs - last < number.doubleValue { return }
2127
+ next.lastTimestamp = sample.timestampMs
2128
+ self.emit(["state": "sample", "pressureHpa": sample.pressureHpa,
2129
+ "relativeAltitudeMeters": sample.relativeAltitudeMeters, "timestampMs": sample.timestampMs])
2130
+ }
2131
+ }
2132
+ } catch {
2133
+ source.stop()
2134
+ return failure("SENSOR_ERROR", error.localizedDescription)
2135
+ }
2136
+ let previous = stream
2137
+ stream = next
2138
+ previous?.source.stop()
2139
+ return .success(true)
2140
+ }
2141
+
2142
+ @discardableResult func stop(reason: String) -> Bool {
2143
+ guard let previous = stream else { return false }
2144
+ stream = nil
2145
+ previous.source.stop()
2146
+ emit(["state": "stopped", "reason": reason])
2147
+ return true
2148
+ }
2149
+ func close() { closed = true; stop(reason: "disposed"); emit = { _ in } }
2150
+ deinit { stream?.source.stop() }
2151
+ private func failure(_ code: String, _ message: String) -> Result<Any, CraftNativeActionError> {
2152
+ .failure(CraftNativeActionError(code: code, message: message))
2153
+ }
2154
+ }