From c72d7ad70a59d2b9de48406d45fab83405f87333 Mon Sep 17 00:00:00 2001 From: brice Date: Fri, 30 Oct 2026 20:56:49 +0000 Subject: [PATCH] New flight: fill responsible person and reason for visit Both rarely change between flights, but no import path set them, so they were re-entered in the flight editor after every creation. - Reason for visit defaults to "Based" when the destination is the aircraft's usual base, "Short Term Visit" otherwise; falls back to the copied flight's value when the aircraft has no base. - Responsible person defaults to the most recent one who is aboard, else the first crew member. - "Same as ..." and "Choose another flight..." carry the source flight's responsible person, reason and document choices. - Both shown as editable rows on the people step. - Repeating a previous flight now carries reason for visit and document choices; a route import only clears a responsible person an import set. Co-Authored-By: Claude Opus 5.5 --- .../flyfun-forms/Models/Flight.swift | 12 ++ .../flyfun-forms/Models/FlightDraft.swift | 4 + .../Services/PeopleSuggestion.swift | 29 +++- .../Views/CrewSourcePickerView.swift | 8 +- .../flyfun-forms/Views/NewFlightFlow.swift | 132 ++++++++++++++++-- .../flyfun-formsTests/FlightImportTests.swift | 92 ++++++++++++ .../flyfun_formsUITests.swift | 4 +- designs/flight-import.md | 36 ++++- 8 files changed, 296 insertions(+), 21 deletions(-) diff --git a/app/flyfun-forms/flyfun-forms/Models/Flight.swift b/app/flyfun-forms/flyfun-forms/Models/Flight.swift index 7aa389e..4b930bf 100644 --- a/app/flyfun-forms/flyfun-forms/Models/Flight.swift +++ b/app/flyfun-forms/flyfun-forms/Models/Flight.swift @@ -260,6 +260,18 @@ final class Flight { "Repair", ] + /// The reason for visit a new flight starts with: "Based" when it lands at + /// the aircraft's usual base, "Short Term Visit" anywhere else. Those two + /// cover nearly every flight; the rare ones stay a pick away. Nil when the + /// aircraft has no base on file or the flight no destination, since there + /// is then nothing to tell the two apart. + static func defaultReasonForVisit(destination: String, usualBase: String?) -> String? { + let destination = destination.trimmingCharacters(in: .whitespaces).uppercased() + let base = (usualBase ?? "").trimmingCharacters(in: .whitespaces).uppercased() + guard !destination.isEmpty, !base.isEmpty else { return nil } + return destination == base ? "Based" : "Short Term Visit" + } + /// Sets the responsible person, keeping `contact` in step for older builds /// that still read it. func setResponsiblePerson(_ person: Person?) { diff --git a/app/flyfun-forms/flyfun-forms/Models/FlightDraft.swift b/app/flyfun-forms/flyfun-forms/Models/FlightDraft.swift index 0d2953f..0f5255a 100644 --- a/app/flyfun-forms/flyfun-forms/Models/FlightDraft.swift +++ b/app/flyfun-forms/flyfun-forms/Models/FlightDraft.swift @@ -55,6 +55,8 @@ struct FlightDraft { var nature: String? var contact: String? var observations: String? + var reasonForVisit: String? + var chosenDocNumbers: [String] = [] var provenance: Provenance @@ -134,6 +136,8 @@ extension FlightDraft { self.nature = flight.nature self.contact = flight.contact self.observations = flight.observations + self.reasonForVisit = flight.reasonForVisit + self.chosenDocNumbers = flight.chosenDocNumberList self.provenance = .previousFlight( route: flight.displayName, departure: flight.departureDateTime ) diff --git a/app/flyfun-forms/flyfun-forms/Services/PeopleSuggestion.swift b/app/flyfun-forms/flyfun-forms/Services/PeopleSuggestion.swift index 5ad7a88..d15af23 100644 --- a/app/flyfun-forms/flyfun-forms/Services/PeopleSuggestion.swift +++ b/app/flyfun-forms/flyfun-forms/Services/PeopleSuggestion.swift @@ -12,6 +12,10 @@ struct PeopleSuggestion { let passengers: [Person] /// What the suggestion is drawn from, shown on the chip. let label: String + /// The flight the people were copied from, nil for the usual crew. Taking + /// the suggestion also takes its responsible person and document choices, + /// which go with the same people and rarely change between trips. + var source: Flight? = nil var isEmpty: Bool { crew.isEmpty && passengers.isEmpty } @@ -45,7 +49,8 @@ struct PeopleSuggestion { return PeopleSuggestion( crew: match.crewList, passengers: match.passengerList, - label: String(localized: "Same as \(match.displayName)") + label: String(localized: "Same as \(match.displayName)"), + source: match ) } @@ -53,7 +58,8 @@ struct PeopleSuggestion { return PeopleSuggestion( crew: latest.crewList, passengers: latest.passengerList, - label: String(localized: "Same as \(latest.displayName)") + label: String(localized: "Same as \(latest.displayName)"), + source: latest ) } @@ -88,6 +94,25 @@ struct PeopleSuggestion { } } + /// The responsible person a new flight starts with when nothing chose one: + /// the most recent flight's responsible person among those aboard this + /// one, else the first crew member. Nil while nobody is aboard. + /// + /// Only someone on board is reused, so a responsible person from a trip + /// with other people does not land on a form they have nothing to do with. + static func defaultResponsiblePerson( + crew: [Person], passengers: [Person], flights: [Flight] + ) -> Person? { + let aboard = Set((crew + passengers).map(ObjectIdentifier.init)) + guard !aboard.isEmpty else { return nil } + let recent = flights + .sorted { $0.departureDateTime > $1.departureDateTime } + .lazy + .compactMap(\.responsiblePerson) + .first { aboard.contains(ObjectIdentifier($0)) } + return recent ?? crew.first ?? passengers.first + } + /// A short "Anne, Bob and 2 more" for the chip's second line. var summary: String { let names = (crew + passengers).map(\.displayName) diff --git a/app/flyfun-forms/flyfun-forms/Views/CrewSourcePickerView.swift b/app/flyfun-forms/flyfun-forms/Views/CrewSourcePickerView.swift index 89f0123..985753d 100644 --- a/app/flyfun-forms/flyfun-forms/Views/CrewSourcePickerView.swift +++ b/app/flyfun-forms/flyfun-forms/Views/CrewSourcePickerView.swift @@ -14,9 +14,9 @@ struct CrewSourcePickerView: View { @Environment(\.dismiss) private var dismiss @Query(sort: \Flight.departureDate, order: .reverse) private var flights: [Flight] - /// Delivers the crew and passengers of the chosen flight. The sheet - /// dismisses itself afterward. - var onSelect: (_ crew: [Person], _ passengers: [Person]) -> Void + /// Delivers the chosen flight; the caller takes its people and the + /// details that go with them. The sheet dismisses itself afterward. + var onSelect: (Flight) -> Void private static let dateFormatter: DateFormatter = { let formatter = DateFormatter() @@ -56,7 +56,7 @@ struct CrewSourcePickerView: View { } else { List(candidates) { flight in Button { - onSelect(flight.crewList, flight.passengerList) + onSelect(flight) dismiss() } label: { row(flight) diff --git a/app/flyfun-forms/flyfun-forms/Views/NewFlightFlow.swift b/app/flyfun-forms/flyfun-forms/Views/NewFlightFlow.swift index 2d6ef70..1224dc6 100644 --- a/app/flyfun-forms/flyfun-forms/Views/NewFlightFlow.swift +++ b/app/flyfun-forms/flyfun-forms/Views/NewFlightFlow.swift @@ -24,13 +24,23 @@ struct NewFlightFlow: View { @State private var importedAircraft: Aircraft? @State private var selectedCrew: [Person] = [] @State private var selectedPassengers: [Person] = [] - @State private var selectedResponsiblePerson: Person? + /// Read through `effectiveResponsiblePerson`: while `responsibleSource` is + /// `.automatic` this is ignored and the default follows the people aboard. + @State private var responsiblePerson: Person? + @State private var responsibleSource: ResponsibleSource = .automatic + /// The pilot's pick on the people step, "" for none; nil leaves the + /// reason to `Flight.defaultReasonForVisit`, then to what was copied. + @State private var reasonForVisitChoice: String? + /// The reason on the flight an import or the crew copy came from. + @State private var copiedReasonForVisit: String? + @State private var chosenDocNumbers: [String] = [] @State private var nature: String? @State private var contact: String? @State private var observations: String? @State private var showAirportPicker = false @State private var showPeoplePicker = false @State private var showCrewSourcePicker = false + @State private var showResponsiblePersonPicker = false /// The import method whose picker is up, if any. One piece of state for all /// of them: only one import can be in flight at a time. @@ -60,6 +70,12 @@ struct NewFlightFlow: View { case route, people } + /// Who set the responsible person, so a later import replaces only what an + /// import wrote — the same rule as `peopleCameFromImport`. + enum ResponsibleSource { + case automatic, imported, chosen + } + var body: some View { NavigationStack { Form { @@ -104,10 +120,15 @@ struct NewFlightFlow: View { PeoplePickerView(selectedCrew: $selectedCrew, selectedPassengers: $selectedPassengers) } .sheet(isPresented: $showCrewSourcePicker) { - CrewSourcePickerView { crew, passengers in - applyPeople(crew: crew, passengers: passengers) + CrewSourcePickerView { flight in + applyPeople( + crew: flight.crewList, passengers: flight.passengerList, from: flight + ) } } + .sheet(isPresented: $showResponsiblePersonPicker) { + SinglePersonPickerView(selectedPerson: responsiblePersonBinding) + } .sheet(item: $activeImport) { method in importSheet(for: method) } @@ -223,7 +244,10 @@ struct NewFlightFlow: View { let suggestion = peopleSuggestion { Section { Button { - applyPeople(crew: suggestion.crew, passengers: suggestion.passengers) + applyPeople( + crew: suggestion.crew, passengers: suggestion.passengers, + from: suggestion.source + ) } label: { HStack(spacing: 12) { Image(systemName: "person.2.badge.plus") @@ -281,6 +305,68 @@ struct NewFlightFlow: View { Label("Select People", systemImage: "person.badge.plus") } } + + // Both rarely change between flights, so they arrive filled — from the + // crew copy or the defaults — and are here to check, not to type. + Section("Flight Details") { + Button { + showResponsiblePersonPicker = true + } label: { + HStack { + Text("Responsible Person") + .foregroundStyle(.primary) + Spacer() + Text(effectiveResponsiblePerson?.displayName ?? "—") + .foregroundStyle(.secondary) + } + } + .accessibilityIdentifier("newFlightResponsiblePersonButton") + + Picker("Reason for Visit", selection: reasonForVisitBinding) { + Text("—").tag("") + ForEach(Flight.reasonForVisitOptions, id: \.self) { reason in + Text(LocalizedStringKey(reason)).tag(reason) + } + } + .accessibilityIdentifier("newFlightReasonForVisitPicker") + } + } + + /// The pilot's or an import's choice, else the most recent responsible + /// person who is on board, else the first crew member. + private var effectiveResponsiblePerson: Person? { + guard responsibleSource == .automatic else { return responsiblePerson } + return PeopleSuggestion.defaultResponsiblePerson( + crew: selectedCrew, passengers: selectedPassengers, flights: allFlights + ) + } + + /// The pilot's pick, else "Based" at the aircraft's base and "Short Term + /// Visit" elsewhere, else whatever the copied flight had. + private var effectiveReasonForVisit: String? { + if let reasonForVisitChoice { + return reasonForVisitChoice.isEmpty ? nil : reasonForVisitChoice + } + return Flight.defaultReasonForVisit( + destination: destinationICAO, usualBase: selectedAircraft?.usualBase + ) ?? copiedReasonForVisit + } + + private var responsiblePersonBinding: Binding { + Binding( + get: { effectiveResponsiblePerson }, + set: { + responsiblePerson = $0 + responsibleSource = .chosen + } + ) + } + + private var reasonForVisitBinding: Binding { + Binding( + get: { effectiveReasonForVisit ?? "" }, + set: { reasonForVisitChoice = $0 } + ) } /// Who was on board last time, for the one-tap chip. The people step only @@ -324,10 +410,20 @@ struct NewFlightFlow: View { /// Take a crew the pilot chose — from the suggestion chip or the /// copy-from-a-flight picker. Chosen people are not import-owned, so a /// later import leaves them alone. - private func applyPeople(crew: [Person], passengers: [Person]) { + /// + /// The details that go with the same people come along from `source`: + /// its responsible person, its reason for visit (behind the base rule) + /// and the documents picked for them. + private func applyPeople(crew: [Person], passengers: [Person], from source: Flight?) { selectedCrew = crew selectedPassengers = passengers peopleCameFromImport = false + if let rp = source?.responsiblePerson { + responsiblePerson = rp + responsibleSource = .chosen + } + copiedReasonForVisit = source?.reasonForVisit + chosenDocNumbers = source?.chosenDocNumberList ?? [] } /// Begin an import. The clipboard resolves in place; the others present a @@ -430,16 +526,31 @@ struct NewFlightFlow: View { if draft.hasPeople { selectedCrew = draft.crew selectedPassengers = draft.passengers + chosenDocNumbers = draft.chosenDocNumbers peopleCameFromImport = true } else if peopleCameFromImport { selectedCrew = [] selectedPassengers = [] + chosenDocNumbers = [] peopleCameFromImport = false } + // The responsible person has an editor on the people step, so it + // follows the same ownership rule as the people. + if let rp = draft.responsiblePerson { + responsiblePerson = rp + responsibleSource = .imported + } else if responsibleSource == .imported { + responsiblePerson = nil + responsibleSource = .automatic + } + + // A pick on the people step still wins; this is only the fallback + // when the aircraft has no base to derive the reason from. + copiedReasonForVisit = draft.reasonForVisit + // These have no editor in this flow, so an import is their only // source and replacing unconditionally cannot discard a pilot's entry. - selectedResponsiblePerson = draft.responsiblePerson nature = draft.nature contact = draft.contact observations = draft.observations @@ -498,9 +609,14 @@ struct NewFlightFlow: View { flight.aircraft = selectedAircraft flight.crew = selectedCrew.isEmpty ? nil : selectedCrew flight.passengers = selectedPassengers.isEmpty ? nil : selectedPassengers - flight.responsiblePerson = selectedResponsiblePerson + flight.chosenDocNumbers = chosenDocNumbers.isEmpty ? nil : chosenDocNumbers + if let rp = effectiveResponsiblePerson { + flight.setResponsiblePerson(rp) + } else { + flight.contact = contact + } + flight.reasonForVisit = effectiveReasonForVisit if let nature { flight.nature = nature } - flight.contact = contact flight.observations = observations modelContext.insert(flight) diff --git a/app/flyfun-forms/flyfun-formsTests/FlightImportTests.swift b/app/flyfun-forms/flyfun-formsTests/FlightImportTests.swift index ca82bb5..615bf16 100644 --- a/app/flyfun-forms/flyfun-formsTests/FlightImportTests.swift +++ b/app/flyfun-forms/flyfun-formsTests/FlightImportTests.swift @@ -121,6 +121,9 @@ struct FlightDraftTests { flight.crew = [pilot] flight.passengers = [passenger] flight.observations = "Weekend trip" + flight.reasonForVisit = "Maintenance" + flight.chosenDocNumbers = ["TEST-DOC-1"] + flight.responsiblePerson = pilot context.insert(flight) let draft = FlightDraft(repeating: flight, now: utc(2026, 9, 16, 9, 0)) @@ -131,6 +134,9 @@ struct FlightDraftTests { #expect(draft.crew.count == 1) #expect(draft.passengers.count == 1) #expect(draft.observations == "Weekend trip") + #expect(draft.reasonForVisit == "Maintenance") + #expect(draft.chosenDocNumbers == ["TEST-DOC-1"]) + #expect(draft.responsiblePerson === pilot) #expect(draft.hasPeople) // 10:00 is past 09:00 today? No — still ahead, so today. #expect(draft.route.departureTime == utc(2026, 9, 16, 10, 0)) @@ -384,6 +390,7 @@ struct PeopleSuggestionTests { PeopleSuggestion.suggest(from: [newer, older], aircraft: piper, usualCrew: []) ) #expect(suggestion.crew.map(\.displayName) == [ola.displayName]) + #expect(suggestion.source === older) } @Test("falls back to the most recent flight when the aircraft is unknown") @@ -423,6 +430,62 @@ struct PeopleSuggestionTests { #expect(suggestion.crew.count == 1) } + @Test("the usual crew has no flight to copy details from") + func usualCrewHasNoSource() throws { + let container = try makeContainer() + let zara = Person(firstName: "Zara", lastName: "Kowalski") + container.mainContext.insert(zara) + let suggestion = try #require( + PeopleSuggestion.suggest(from: [], aircraft: nil, usualCrew: [zara]) + ) + #expect(suggestion.source == nil) + } + + @Test("the responsible person is the latest one who is aboard") + func responsibleIsLatestAboard() throws { + let container = try makeContainer() + let context = container.mainContext + + let zara = Person(firstName: "Zara", lastName: "Kowalski") + let ola = Person(firstName: "Ola", lastName: "Nowak") + let owner = Person(firstName: "Ida", lastName: "Owner") + [zara, ola, owner].forEach { context.insert($0) } + + let older = makeFlight( + context: context, departure: utc(2026, 1, 1, 10, 0), + aircraft: nil, crew: [zara], passengers: [ola] + ) + older.responsiblePerson = ola + // The most recent one named someone who is not on this flight, so it + // is passed over rather than put on a form they have no part in. + let newer = makeFlight( + context: context, departure: utc(2026, 2, 1, 10, 0), + aircraft: nil, crew: [zara] + ) + newer.responsiblePerson = owner + + let rp = PeopleSuggestion.defaultResponsiblePerson( + crew: [zara], passengers: [ola], flights: [newer, older] + ) + #expect(rp === ola) + } + + @Test("with no history the first crew member is responsible") + func responsibleFallsBackToCrew() throws { + let container = try makeContainer() + let context = container.mainContext + let zara = Person(firstName: "Zara", lastName: "Kowalski") + let ola = Person(firstName: "Ola", lastName: "Nowak") + [zara, ola].forEach { context.insert($0) } + + #expect(PeopleSuggestion.defaultResponsiblePerson( + crew: [zara], passengers: [ola], flights: [] + ) === zara) + #expect(PeopleSuggestion.defaultResponsiblePerson( + crew: [], passengers: [], flights: [] + ) == nil) + } + @Test("suggests nothing at all on a first run") func nothingToSuggest() { #expect(PeopleSuggestion.suggest(from: [], aircraft: nil, usualCrew: []) == nil) @@ -482,6 +545,35 @@ struct PeopleSuggestionTests { } } +// MARK: - Default reason for visit + +@Suite("Flight.defaultReasonForVisit") +struct DefaultReasonForVisitTests { + + @Test("landing at the aircraft's base is Based") + func basedAtHome() { + #expect(Flight.defaultReasonForVisit(destination: "EGTF", usualBase: "EGTF") == "Based") + #expect(Flight.defaultReasonForVisit(destination: "egtf", usualBase: " EGTF ") == "Based") + } + + @Test("anywhere else is a short term visit, including leaving the base") + func shortTermElsewhere() { + // Only the destination counts: inbound to the base is Based, and + // outbound from it (EGTF → LFRM) is a short term visit. + #expect( + Flight.defaultReasonForVisit(destination: "LFRM", usualBase: "EGTF") + == "Short Term Visit" + ) + } + + @Test("without a base or a destination there is nothing to go on") + func unknown() { + #expect(Flight.defaultReasonForVisit(destination: "EGKA", usualBase: nil) == nil) + #expect(Flight.defaultReasonForVisit(destination: "EGKA", usualBase: "") == nil) + #expect(Flight.defaultReasonForVisit(destination: "", usualBase: "EGTF") == nil) + } +} + // MARK: - Default aircraft @Suite("Aircraft.defaultForNewFlight") diff --git a/app/flyfun-forms/flyfun-formsUITests/flyfun_formsUITests.swift b/app/flyfun-forms/flyfun-formsUITests/flyfun_formsUITests.swift index f524c90..c93ab83 100644 --- a/app/flyfun-forms/flyfun-formsUITests/flyfun_formsUITests.swift +++ b/app/flyfun-forms/flyfun-formsUITests/flyfun_formsUITests.swift @@ -180,7 +180,9 @@ final class flyfun_formsUITests: XCTestCase { method.tap() XCTAssertTrue(element(app, "importSummary").waitForExistence(timeout: Self.uiTimeout)) element(app, "newFlightNextButton").tap() - element(app, "createFlightButton").tap() + let create = element(app, "createFlightButton") + XCTAssertTrue(create.waitForExistence(timeout: Self.uiTimeout)) + create.tap() XCTAssertTrue(app.navigationBars["LSGS > EGTF"].waitForExistence(timeout: Self.uiTimeout), "creating the flight should open it") openTab(app, "Aircraft") diff --git a/designs/flight-import.md b/designs/flight-import.md index 4e457b1..ab3db24 100644 --- a/designs/flight-import.md +++ b/designs/flight-import.md @@ -42,10 +42,10 @@ flight's crew in place — on a customs form, the wrong people on the document. The replacement is scoped by ownership, tracked in `peopleCameFromImport`: a selection an import wrote is replaced or cleared by the next import, and a selection the pilot made by hand (through the people picker, the suggestion chip, or copying a flight's crew) survives, so -re-importing a route to fix a typo does not wipe the crew they just chose. The -form-level fields (`responsiblePerson`, `nature`, `contact`, `observations`) have no -editor in this flow, so an import is their only source and they are replaced -unconditionally. +re-importing a route to fix a typo does not wipe the crew they just chose. The responsible +person follows the same rule (`ResponsibleSource`: automatic / imported / chosen), since it +has an editor on the people step. `nature`, `contact` and `observations` have no editor in +this flow, so an import is their only source and they are replaced unconditionally. Route fields keep their `if !isEmpty` guards: an empty departure in a draft is a parse defect, not an instruction to clear a good value. @@ -66,6 +66,8 @@ struct FlightDraft { var responsiblePerson: Person? var nature: String? var observations: String? + var reasonForVisit: String? + var chosenDocNumbers: [String] var provenance: Provenance } ``` @@ -117,8 +119,8 @@ rather than a wrong flight. `applyRoute` historically filled origin/destination/times/aircraft and stopped. The route step is four fields; the people step is the slow one. So: -- **Previous flight** copies crew, passengers, responsible person, nature and contact - outright (`Flight.copyCommon(to:)` semantics). +- **Previous flight** copies crew, passengers, responsible person, nature, contact, + reason for visit and document choices outright (`Flight.copyCommon(to:)` semantics). - **Every other method** offers a one-tap `PeopleSuggestion` on the people step: the crew and passengers of the most recent flight flown with the same aircraft, falling back to the most recent flight overall, falling back to `isUsualCrew`. @@ -126,6 +128,28 @@ step is four fields; the people step is the slow one. So: the same copy with the choice handed back, for a pilot who flies with different people on different trips. Rows are deduplicated by *the people*, not the route, so a weekly trip with the same four aboard is one row rather than eight that differ by date. +- **The chip and the picker carry what goes with those people**: the source flight's + responsible person, its document choices, and its reason for visit (as a fallback, see + below). `PeopleSuggestion.source` is that flight; nil for the usual-crew fallback. + +### Responsible person and reason for visit arrive filled + +These rarely change between flights, and the pilot used to set both in the flight editor +after every creation, because no import path filled them. They now sit on the people step +as two rows, already filled, so the step is a check rather than an entry. + +- **Responsible person**: whatever set it (the chip, the crew picker, a previous-flight + import, the pilot), else `PeopleSuggestion.defaultResponsiblePerson`: the most recent + flight's responsible person *who is aboard this one*, else the first crew member. Only + someone aboard is reused, so a person from an unrelated trip never lands on the form. + Computed, not stored, while automatic, so it follows the people as they change. +- **Reason for visit**: the pilot's pick on the people step, else + `Flight.defaultReasonForVisit(destination:usualBase:)` — "Based" when the destination is + the aircraft's usual base, "Short Term Visit" anywhere else — else the copied flight's + value (only reachable when the aircraft has no base on file). The base rule outranks the + copied value on purpose: those two answers cover nearly every flight, and the reason a + copied flight had belongs to *its* destination. The rare reasons (maintenance, repair, + import) stay one pick away. ### The aircraft starts filled in