Reportage/Views/Survey/PlaceAnswerView.swift (8618B)
1import MapKit
2import SwiftUI
3
4
5
6struct PlaceAnswerView: View {
7 let picker: PlacePicker
8 let placeholder: String?
9
10 let isActive: Bool
11
12 @FocusState private var isSearchFocused: Bool
13
14 var body: some View {
15 @Bindable var picker = picker
16 VStack(spacing: Theme.blockGap) {
17
18 if showsMap && !isSearchFocused {
19 PlaceMap(
20 location: picker.location?.coordinate,
21 isLocating: picker.locationState == .locating,
22 selection: picker.selection
23 )
24 .transition(.opacity)
25 }
26 TokenSearchField(
27 query: $picker.query,
28 placeholder: placeholder ?? String(localized: "Search places"),
29 isFocused: $isSearchFocused,
30 onSubmit: {}
31 )
32 PlaceList(
33 picker: picker,
34 onToggle: { candidate in
35 picker.toggle(candidate)
36 isSearchFocused = false
37 },
38 onAdd: {
39 picker.addQuery()
40 isSearchFocused = false
41 }
42 )
43 }
44 .animation(.snappy, value: isSearchFocused)
45 .animation(.snappy, value: showsMap)
46 .onChange(of: isActive) { if !isActive { isSearchFocused = false } }
47 }
48
49
50 private var showsMap: Bool {
51 picker.locationState != .unavailable || picker.selection?.coordinate != nil
52 }
53}
54
55
56
57struct PlaceMap: View {
58 let location: Coordinate?
59 let isLocating: Bool
60 let selection: PlaceCandidate?
61
62 @State private var position: MapCameraPosition = .automatic
63
64 var body: some View {
65 ZStack {
66 if let focus {
67 Map(position: $position, interactionModes: [.pan, .zoom]) {
68 if let location {
69 Annotation(String(localized: "You are here"), coordinate: .init(location), anchor: .center) {
70 HereMarker()
71 }
72 .annotationTitles(.hidden)
73 }
74 if let selection, let coordinate = selection.coordinate {
75 Marker(selection.name, coordinate: .init(coordinate))
76 .tint(Theme.accent)
77 }
78 }
79 .mapStyle(.standard(elevation: .flat, emphasis: .muted, pointsOfInterest: .excludingAll))
80 .onAppear { position = camera(on: focus) }
81 .onChange(of: focus) {
82 withAnimation(.smooth) { position = camera(on: focus) }
83 }
84 } else if isLocating {
85 HStack(spacing: Theme.iconGap) {
86 ProgressView()
87 Text("Finding where you are…")
88 .font(.subheadline)
89 }
90 .foregroundStyle(Theme.muted)
91 }
92 }
93 .frame(maxWidth: .infinity)
94 .frame(height: 180)
95 .clipShape(.rect(cornerRadius: 12))
96 .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(Theme.rule))
97 .padding(.horizontal, Theme.margin)
98 }
99
100
101
102 private var focus: Coordinate? {
103 selection?.coordinate ?? location
104 }
105
106 private func camera(on focus: Coordinate) -> MapCameraPosition {
107 var span: CLLocationDistance = 500
108 if let location, focus != location {
109 span = min(max(span, focus.distance(to: location) * 2.4), 50_000)
110 }
111 return .region(
112 MKCoordinateRegion(center: .init(focus), latitudinalMeters: span, longitudinalMeters: span))
113 }
114}
115
116
117struct HereMarker: View {
118 var body: some View {
119 Circle()
120 .fill(Theme.accent)
121 .frame(width: 14, height: 14)
122 .padding(3)
123 .background(Theme.paper, in: .circle)
124 .shadow(color: .black.opacity(0.2), radius: 2, y: 1)
125 .accessibilityLabel(Text("You are here"))
126 }
127}
128
129struct PlaceList: View {
130 let picker: PlacePicker
131 let onToggle: (PlaceCandidate) -> Void
132 let onAdd: () -> Void
133
134 var body: some View {
135 List {
136 if picker.canAddQuery {
137 AddTokenRow(
138 text: picker.query.trimmingCharacters(in: .whitespacesAndNewlines),
139 detail: picker.location == nil ? nil : Text("A new place, where you are now"),
140 onAdd: onAdd
141 )
142 .listRowStyle()
143 }
144 ForEach(picker.sections) { section in
145 Section {
146 ForEach(section.candidates) { candidate in
147 PlaceRow(
148 candidate: candidate,
149 isSelected: picker.isSelected(candidate),
150 onToggle: { onToggle(candidate) }
151 )
152 .listRowStyle()
153 }
154 } header: {
155 PlaceSectionHeader(kind: section.kind)
156 }
157 }
158 if picker.isSearching {
159 ProgressView()
160 .frame(maxWidth: .infinity, minHeight: 52)
161 .listRowStyle()
162 .listRowSeparator(.hidden)
163 }
164 }
165 .listStyle(.plain)
166 .scrollContentBackground(.hidden)
167 .scrollDismissesKeyboard(.immediately)
168 }
169}
170
171struct PlaceSectionHeader: View {
172 let kind: PlaceSection.Kind
173
174 var body: some View {
175 title
176 .eyebrow()
177 .frame(maxWidth: .infinity, alignment: .leading)
178 .padding(.top, Theme.headingTop)
179 .padding(.bottom, 4)
180 .listRowInsets(EdgeInsets(top: 0, leading: Theme.margin, bottom: 0, trailing: Theme.margin))
181 .background(Theme.paper)
182 }
183
184 private var title: Text {
185 switch kind {
186 case .yours: Text("Your places", comment: "Heading for the user's own places")
187 case .nearby: Text("Nearby", comment: "Heading for places near the user from Apple Maps")
188 case .results: Text("More places", comment: "Heading for place search results from Apple Maps")
189 }
190 }
191}
192
193struct PlaceRow: View {
194 let candidate: PlaceCandidate
195 let isSelected: Bool
196 let onToggle: () -> Void
197
198 var body: some View {
199 Button(action: onToggle) {
200 HStack(spacing: Theme.itemGap) {
201 VStack(alignment: .leading, spacing: Theme.lineGap) {
202 Text(candidate.name)
203 .font(Theme.serif(.body))
204 .foregroundStyle(Theme.ink)
205 if let detail = candidate.detail {
206 Text(detail)
207 .font(.footnote)
208 .foregroundStyle(Theme.muted)
209 .lineLimit(1)
210 }
211 }
212 Spacer()
213 if isSelected {
214 Image(systemName: "checkmark")
215 .font(.body.weight(.semibold))
216 .foregroundStyle(Theme.accent)
217 } else if let distance = candidate.distance {
218 Text(Self.format(distance))
219 .font(.caption)
220 .monospacedDigit()
221 .foregroundStyle(Theme.muted)
222 }
223 }
224 .padding(.vertical, 6)
225 .frame(minHeight: 52)
226 .contentShape(.rect)
227 }
228 .buttonStyle(.plain)
229 .accessibilityAddTraits(isSelected ? .isSelected : [])
230 }
231
232 static func format(_ distance: Double) -> String {
233 Measurement(value: distance, unit: UnitLength.meters).formatted(
234 .measurement(
235 width: .abbreviated,
236 usage: .road,
237 numberFormatStyle: .number.precision(.significantDigits(1...2))
238 ))
239 }
240}