-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePerformanceViewModel.swift
More file actions
464 lines (393 loc) · 14.4 KB
/
BasePerformanceViewModel.swift
File metadata and controls
464 lines (393 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import Defaults
import Foundation
import Logging
import Observation
import Sentry
import SwiftData
/// Abstract base class for performance view models.
///
/// ``BasePerformanceViewModel`` provides shared infrastructure for takeoff and landing
/// performance calculations, including:
///
/// - Input observation (airport, runway, weight, conditions)
/// - Model initialization based on user settings
/// - NOTAM fetching and caching
/// - Automatic recalculation when inputs change
///
/// ## Subclassing
///
/// Subclasses must override:
/// - ``airportDefaultsKey`` - Which airport setting to observe
/// - ``runwayDefaultsKey`` - Which runway setting to observe
/// - ``fuelDefaultsKey`` - Which fuel setting to observe
/// - ``defaultFlapSetting`` - Default flap setting for the operation
/// - ``recalculate()`` - Perform the actual performance calculation
///
/// ## Observation
///
/// The view model automatically observes changes to:
/// - Selected airport and runway (from Defaults)
/// - Weight components (empty weight, payload, fuel, density)
/// - Model settings (regression vs tabular, thrust schedule)
/// - Safety factor settings
///
/// ## NOTAM Support
///
/// Downloaded NOTAMs from the FAA API are available via ``downloadedNOTAMs``.
/// Call ``fetchNOTAMs(plannedTime:)`` to load NOTAMs for the current airport.
@Observable
@MainActor
open class BasePerformanceViewModel: WithIdentifiableError {
// MARK: - Properties
private static let logger = Logger(label: "codes.tim.SF50-TOLD.BasePerformanceViewModel")
private let container: ModelContainer
internal var model: PerformanceModel?
private var cancellables: Set<Task<Void, Never>> = []
private var runwayNOTAMObservationTask: Task<Void, Never>?
internal let calculationService: PerformanceCalculationService
// MARK: - Inputs (to be overridden or used by subclasses)
public var flapSetting: FlapSetting {
didSet {
model = initializeModel()
Task { recalculate() }
}
}
public private(set) var weight: Measurement<UnitMass> {
didSet {
model = initializeModel()
Task { recalculate() }
}
}
public private(set) var airport: Airport?
public private(set) var runway: Runway? {
didSet {
model = initializeModel()
Task { recalculate() }
// Set up observation for NOTAM changes on this runway
setupRunwayNOTAMObservation()
}
}
public var conditions: Conditions {
didSet {
model = initializeModel()
Task { recalculate() }
}
}
public var error: Error?
// MARK: - Downloaded NOTAMs
/// NOTAMs downloaded from the API for the current airport
public private(set) var downloadedNOTAMs: [NOTAMResponse] = []
/// Whether NOTAMs are currently being loaded
public private(set) var isLoadingNOTAMs = false
/// Whether we have attempted to fetch NOTAMs for the current airport
public private(set) var hasAttemptedNOTAMFetch = false
// MARK: - Computed Properties
internal var configuration: Configuration {
.init(weight: weight, flapSetting: flapSetting)
}
public var notam: NOTAM? { runway?.notam }
// MARK: - Abstract Properties (must be overridden)
/// The Defaults key for the airport
open var airportDefaultsKey: Defaults.Key<String?> {
fatalError("Subclasses must override airportDefaultsKey")
}
/// The Defaults key for the runway
open var runwayDefaultsKey: Defaults.Key<String?> {
fatalError("Subclasses must override runwayDefaultsKey")
}
/// The Defaults key for the fuel amount
open var fuelDefaultsKey: Defaults.Key<Measurement<UnitVolume>> {
fatalError("Subclasses must override fuelDefaultsKey")
}
/// The default flap setting for this operation
open var defaultFlapSetting: FlapSetting {
fatalError("Subclasses must override defaultFlapSetting")
}
// MARK: - Initialization
public init(
container: ModelContainer,
calculationService: PerformanceCalculationService = DefaultPerformanceCalculationService.shared,
defaultFlapSetting: FlapSetting
) {
self.container = container
self.calculationService = calculationService
// temporary values, overwritten by recalculate()
model = nil
flapSetting = defaultFlapSetting
weight = .init(value: 3550, unit: .pounds)
runway = nil
conditions = .init()
model = initializeModel()
Task { recalculate() }
setupObservation()
}
// MARK: - Observation Setup
private func setupObservation() {
let airportKey = airportDefaultsKey
let runwayKey = runwayDefaultsKey
addTask(
Task.detached { [container] in
for await (airportID, runwayID) in Defaults.updates(airportKey, runwayKey)
where !Task.isCancelled {
do {
let context = ModelContext(container)
let (fetchedAirport, fetchedRunway) = try findAirportAndRunway(
airportID: airportID,
runwayID: runwayID,
in: context
)
let airportPersistentID = fetchedAirport?.persistentModelID
let runwayPersistentID = fetchedRunway?.persistentModelID
await MainActor.run {
let mainContext = container.mainContext
let airport = airportPersistentID.flatMap { mainContext.model(for: $0) as? Airport }
let runway = runwayPersistentID.flatMap { mainContext.model(for: $0) as? Runway }
if airport == nil { Defaults[airportKey] = nil }
if runway == nil { Defaults[runwayKey] = nil }
self.airport = airport
self.runway = runway
}
} catch {
await MainActor.run {
SentrySDK.capture(error: error) { scope in
scope.setFingerprint(["swiftData", "fetch"])
}
self.error = error
}
}
}
}
)
// Observe weight changes
addTask(
Task {
for await (emptyWeight, fuelDensity, payload, fuel) in Defaults.updates(
.emptyWeight,
.fuelDensity,
.payload,
fuelDefaultsKey
) where !Task.isCancelled {
weight = emptyWeight + payload + fuel * fuelDensity
}
}
)
// Observe aircraft type and model type changes
addTask(
Task {
for await _
in Defaults
.updates(.aircraftTypeSetting, .updatedThrustSchedule, .useRegressionModel)
where !Task.isCancelled {
model = initializeModel()
recalculate()
}
}
)
// Observe safety factor changes
addTask(
Task {
for await _ in Defaults.updates(.safetyFactorDry, .safetyFactorWet, .VREFAdditive)
where !Task.isCancelled {
recalculate()
}
}
)
}
private func addTask(_ task: Task<Void, Never>) {
cancellables.insert(task)
}
// MARK: - NOTAM Observation
private func setupRunwayNOTAMObservation() {
// Cancel any existing observation
runwayNOTAMObservationTask?.cancel()
runwayNOTAMObservationTask = nil
guard runway != nil else { return }
// Poll for changes to the NOTAM's snapshot
runwayNOTAMObservationTask = Task { @MainActor in
var lastSnapshot = notam.map { NOTAMInput(from: $0) }
while !Task.isCancelled {
try? await Task.sleep(for: .milliseconds(500))
// Access the current NOTAM (SwiftData should automatically fetch latest)
let currentSnapshot = notam.map { NOTAMInput(from: $0) }
// Compare snapshots to detect changes
if let last = lastSnapshot, let current = currentSnapshot {
if last != current {
lastSnapshot = currentSnapshot
model = initializeModel()
recalculate()
}
} else if lastSnapshot != nil || currentSnapshot != nil {
// NOTAM added or removed
lastSnapshot = currentSnapshot
model = initializeModel()
recalculate()
}
}
}
}
// MARK: - NOTAM Fetching
/// Fetches NOTAMs for the current airport from the API
/// - Parameter plannedTime: The planned time for the flight, used to filter relevant NOTAMs
public func fetchNOTAMs(plannedTime: Date = Date()) async {
guard let airport else {
downloadedNOTAMs = []
return
}
// NOTAM API uses 3-letter identifiers (e.g., FAI) not ICAO codes (e.g., PAFA)
// Try locationID first, fallback to ICAO_ID if locationID unavailable
let primaryIdentifier = airport.locationID
let fallbackIdentifier = airport.ICAO_ID
// Check cache first
if let cached = await NOTAMCache.shared.get(for: primaryIdentifier) {
downloadedNOTAMs = filterNOTAMs(cached, relativeTo: plannedTime)
hasAttemptedNOTAMFetch = true
return
}
// Fetch from API
isLoadingNOTAMs = true
do {
// Fetch NOTAMs from 7 days before planned time to 30 days after
let startDate = Calendar.current.date(byAdding: .day, value: -7, to: plannedTime)
let endDate = Calendar.current.date(byAdding: .day, value: 30, to: plannedTime)
// Try primary identifier first
var response = try await NOTAMLoader.shared.fetchNOTAMs(
for: primaryIdentifier,
startDate: startDate,
endDate: endDate
)
// If no results and we have a fallback identifier, try that
if response.data.isEmpty, let fallbackIdentifier, fallbackIdentifier != primaryIdentifier {
response = try await NOTAMLoader.shared.fetchNOTAMs(
for: fallbackIdentifier,
startDate: startDate,
endDate: endDate
)
}
// Invalidate old cache only after successfully downloading new NOTAMs
await NOTAMCache.shared.invalidate(for: primaryIdentifier)
// Cache the new results
await NOTAMCache.shared.set(response.data, for: primaryIdentifier)
// Filter for relevant NOTAMs
downloadedNOTAMs = filterNOTAMs(response.data, relativeTo: plannedTime)
// Mark that we've attempted to fetch NOTAMs
hasAttemptedNOTAMFetch = true
} catch {
// Log error but don't show to user - NOTAMs are supplementary
Self.logger.error("Failed to fetch NOTAMs: \(error)")
downloadedNOTAMs = []
hasAttemptedNOTAMFetch = true
}
isLoadingNOTAMs = false
}
/// Filters NOTAMs to show only currently active or upcoming ones
private func filterNOTAMs(_ notams: [NOTAMResponse], relativeTo date: Date) -> [NOTAMResponse] {
notams.filter { notam in
// Include if no end time (permanent) or end time is in the future
guard let endTime = notam.effectiveEnd else { return true }
return endTime > date
}
.sorted { lhs, rhs in
// Sort by effective start, earliest first
lhs.effectiveStart < rhs.effectiveStart
}
}
// MARK: - Model Initialization
internal func initializeModel() -> (any PerformanceModel)? {
guard let runway, let airport else { return nil }
let runwaySnapshot = RunwayInput(from: runway, airport: airport),
notamInput = notam.map { NOTAMInput(from: $0) },
aircraftType = Defaults.Keys.aircraftType
return if Defaults[.useRegressionModel] {
RegressionPerformanceModel(
conditions: conditions,
configuration: configuration,
runway: runwaySnapshot,
notam: notamInput,
aircraftType: aircraftType
)
} else {
TabularPerformanceModel(
conditions: conditions,
configuration: configuration,
runway: runwaySnapshot,
notam: notamInput,
aircraftType: aircraftType
)
}
}
// MARK: - Input-Validation Notes
/// Generates notes for input-validation issues common to both takeoff and landing.
///
/// Checks wind exceedances (crosswind, tailwind), weight/fuel limits, contamination,
/// and VREF additive applicability. Subclass view models call this, then append their
/// own result-dependent notes.
public func generateInputNotes(
for operation: Operation,
VREFAdditiveKts: Double = 0
) -> [PerformanceNote] {
var notes: [PerformanceNote] = []
let limits = Defaults.Keys.aircraftType.limitations
// Wind exceedances
if let runway, let windDirection = conditions.windDirection,
let windSpeed = conditions.windSpeed
{
let headingRad =
runway.trueHeading.converted(to: .degrees).value * .pi / 180
let windRad = windDirection.converted(to: .degrees).value * .pi / 180
let angleDiff = windRad - headingRad
let crosswindKts = abs(sin(angleDiff) * windSpeed.converted(to: .knots).value)
let headwindKts = cos(angleDiff) * windSpeed.converted(to: .knots).value
let crosswindLimit: Measurement<UnitSpeed> =
flapSetting == .flaps100
? limits.maxCrosswind_flaps100 : limits.maxCrosswind_flaps50
let crosswind = Measurement<UnitSpeed>(value: crosswindKts, unit: .knots)
if crosswind > crosswindLimit {
notes.append(.crosswindExceedance(crosswind: crosswind, limit: crosswindLimit))
}
if headwindKts < 0 {
let tailwind = Measurement<UnitSpeed>(value: -headwindKts, unit: .knots)
if tailwind > limits.maxTailwind {
notes.append(
.tailwindExceedance(tailwind: tailwind, limit: limits.maxTailwind)
)
}
}
}
// Weight exceedances
let maxWeight =
operation == .takeoff
? limits.maxTakeoffWeight : limits.maxLandingWeight
if weight > maxWeight {
notes.append(.weightAboveMax(weight: weight, limit: maxWeight))
}
let zeroFuelWeight = Defaults[.emptyWeight] + Defaults[.payload]
if zeroFuelWeight > limits.maxZeroFuelWeight {
notes.append(
.zeroFuelWeightExceeded(weight: zeroFuelWeight, limit: limits.maxZeroFuelWeight)
)
}
let fuel = Defaults[fuelDefaultsKey]
if fuel > limits.maxFuel {
notes.append(.fuelExceedsCapacity(fuel: fuel, limit: limits.maxFuel))
}
// Contamination notes (landing only)
if operation == .landing, let contamination = notam?.contamination {
if case .rwyCC = contamination {
if Defaults[.safetyFactorDry] != 1.0 || Defaults[.safetyFactorWet] != 1.0 {
notes.append(.rwyCCSafetyFactorNotApplied)
}
}
notes.append(.contaminationSupplemental)
}
// VREF additive note
if VREFAdditiveKts > 0 {
notes.append(.VREFAdditiveApproximate)
}
return notes
}
// MARK: - Abstract Methods (must be overridden)
/// Recalculates performance values. Must be overridden by subclasses.
open func recalculate() { // swiftlint:disable:this unavailable_function
fatalError("Subclasses must override recalculate()")
}
}