Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/flyfun-forms/flyfun-forms/Models/Flight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
4 changes: 4 additions & 0 deletions app/flyfun-forms/flyfun-forms/Models/FlightDraft.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct FlightDraft {
var nature: String?
var contact: String?
var observations: String?
var reasonForVisit: String?
var chosenDocNumbers: [String] = []

var provenance: Provenance

Expand Down Expand Up @@ -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
)
Expand Down
29 changes: 27 additions & 2 deletions app/flyfun-forms/flyfun-forms/Services/PeopleSuggestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -45,15 +49,17 @@ 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
)
}

if let latest = withPeople.first {
return PeopleSuggestion(
crew: latest.crewList,
passengers: latest.passengerList,
label: String(localized: "Same as \(latest.displayName)")
label: String(localized: "Same as \(latest.displayName)"),
source: latest
)
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -56,7 +56,7 @@ struct CrewSourcePickerView: View {
} else {
List(candidates) { flight in
Button {
onSelect(flight.crewList, flight.passengerList)
onSelect(flight)
dismiss()
} label: {
row(flight)
Expand Down
132 changes: 124 additions & 8 deletions app/flyfun-forms/flyfun-forms/Views/NewFlightFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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<Person?> {
Binding(
get: { effectiveResponsiblePerson },
set: {
responsiblePerson = $0
responsibleSource = .chosen
}
)
}

private var reasonForVisitBinding: Binding<String> {
Binding(
get: { effectiveReasonForVisit ?? "" },
set: { reasonForVisitChoice = $0 }
)
}

/// Who was on board last time, for the one-tap chip. The people step only
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading
Loading