commit 365f2f2a066dd500858641053d5f8ac4fe7dd9e5
parent 7be0828759916e1cb52c671d743eb35f598fc956
Author: Michael Camilleri <[email protected]>
Date: Wed, 21 Oct 2026 23:27:58 +0900
Select previously registered location automatically
Answering 'Where are you?' took a search or a tap even when the user was
extremely likely to be at a previously registered location.
This commit chooses the place for the user if the fix is accurate to 50
m and exactly one of their places is within 50 m of it. Selection
applies to new reports only. Instant reports and answered reports being
edited are not covered by this behaviour.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Notes/Design.md | | | 10 | +++++++--- |
| M | Reportage/Services/Places/PlaceRanking.swift | | | 17 | +++++++++++++++++ |
| M | Reportage/Views/Survey/PlacePicker.swift | | | 9 | +++++++++ |
| M | Reportage/Views/Survey/SurveyModel.swift | | | 4 | +++- |
| M | Tests/Unit/PlacePickerTests.swift | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | Tests/Unit/SurveyModelTests.swift | | | 2 | ++ |
6 files changed, 88 insertions(+), 4 deletions(-)
diff --git a/Notes/Design.md b/Notes/Design.md
@@ -367,6 +367,13 @@ In the survey (`PlacePicker`): search lists the user's matching places, saved or venues, at any distance, then MapKit's results in MapKit's order, biased to a 20 km region around the location. +- **Pre-filling.** When the fix arrives and it's accurate to 50 m, the + user's place within 50 m of it is chosen already, but only if it's the + only one that close: home and the café next door can't be told apart, so + neither is chosen. It applies to new reports only, not to reports being + edited or Instant Reports being filled in, and not once the user has + chosen or started searching. A kept pre-fill is stored like any other + answer. - **Merging.** A MapKit place that's one of the user's places is shown as theirs, at MapKit's coordinate: the same MapKit ID, or a place without one with the same name within 200 m. @@ -1076,9 +1083,6 @@ the locale uses for weather. Sleep isn't charted. since Reporter lost venue lookup (2026-09-01) could be filled in from each report's location context. Deferred: the app comes first, and Reporter data is worked on later. -- **Pre-filling places.** If the report's location is inside a saved place's - area, should that place be pre-selected? It would save many taps, but a - confirmed default may be weaker data than a deliberate choice. - **Multi-select choice questions.** Reporter stores choice answers as arrays, but every answer in the archive has a single value. The survey takes one answer for now.
diff --git a/Reportage/Services/Places/PlaceRanking.swift b/Reportage/Services/Places/PlaceRanking.swift
@@ -22,6 +22,9 @@ enum PlaceRanking {
/// A directory place this close to one of the user's places with the same
/// name, in metres, is taken to be that place.
static let matchDistance: Double = 200
+ /// A place is chosen for the user when it's the only one of their places
+ /// within this distance, in metres, of a fix at least this accurate.
+ static let prefillDistance: Double = 50
/// The index of the band `distance` falls in. Places with no known
/// distance come last.
@@ -48,6 +51,20 @@ enum PlaceRanking {
}
}
+ /// The place to choose for the user at `location`, if there's no doubt
+ /// which it is: the fix is accurate to `prefillDistance` and exactly
+ /// one of `entries` is that close. Two places that close, such as home
+ /// and the café next door, can't be told apart, so neither is chosen.
+ static func prefill(_ entries: [VocabularyEntry], at location: LocationContext) -> VocabularyEntry? {
+ guard let accuracy = location.horizontalAccuracy, accuracy >= 0, accuracy <= prefillDistance
+ else { return nil }
+ let close = entries.filter { entry in
+ guard let coordinate = entry.coordinate else { return false }
+ return coordinate.distance(to: location.coordinate) <= prefillDistance
+ }
+ return close.count == 1 ? close[0] : nil
+ }
+
/// The user's place that `place` is, if any: one with the same MapKit
/// ID, or a place from before MapKit (with no MapKit ID) with the same
/// name nearby.
diff --git a/Reportage/Views/Survey/PlacePicker.swift b/Reportage/Views/Survey/PlacePicker.swift
@@ -73,6 +73,10 @@ final class PlacePicker {
private(set) var selection: PlaceCandidate?
/// Whether a directory search is under way.
private(set) var isSearching = false
+ /// Whether the place the report is at may be chosen when the location
+ /// arrives: see `PlaceRanking.prefill`. Off when editing a report,
+ /// Instant Reports included.
+ var prefills = true
private var entries: [VocabularyEntry]
private var nearby: [DirectoryPlace] = []
@@ -233,6 +237,11 @@ final class PlacePicker {
}
locationState = .found(location)
update()
+ if prefills, selection == nil, query.isEmpty,
+ let entry = PlaceRanking.prefill(entries, at: location)
+ {
+ preselect(entry.id, entry: nil)
+ }
nearbyTask = Task {
let places = (try? await directory.places(near: location.coordinate)) ?? []
nearby = places.sorted {
diff --git a/Reportage/Views/Survey/SurveyModel.swift b/Reportage/Views/Survey/SurveyModel.swift
@@ -111,11 +111,13 @@ final class SurveyModel {
entries: vocabulary[question.id] ?? []
)
case .location:
- placePickers[question.id] = PlacePicker(
+ let picker = PlacePicker(
questionID: question.id,
entries: vocabulary[question.id] ?? [],
directory: directory
)
+ picker.prefills = existing == nil
+ placePickers[question.id] = picker
case .yesNo, .choice, .number, .note:
continue
}
diff --git a/Tests/Unit/PlacePickerTests.swift b/Tests/Unit/PlacePickerTests.swift
@@ -301,6 +301,56 @@ struct PlacePickerTests {
#expect(picker.candidate(id: "cafe")?.detail == nil)
}
+ private static func fix(accuracy: Double?) -> LocationContext {
+ LocationContext(coordinate: here, horizontalAccuracy: accuracy)
+ }
+
+ @Test("The only place within 50 m of an accurate fix is chosen")
+ func prefill() {
+ let near = [
+ Self.entry("home", "Home", uses: 900, at: Self.north(30)),
+ Self.entry("park", "Park", uses: 10, at: Self.north(300)),
+ ]
+ let picker = makePicker(entries: near)
+ picker.setLocation(Self.fix(accuracy: 20))
+ #expect(picker.selection?.id == "home")
+ #expect(picker.selectedTokenID == "home")
+ }
+
+ @Test("Nothing is chosen for a rough fix, two close places, or none within 50 m")
+ func noPrefill() {
+ let close = [
+ Self.entry("home", "Home", uses: 900, at: Self.north(20)),
+ Self.entry("cafe", "Café", uses: 5, at: Self.north(45)),
+ ]
+ let cases: [(entries: [VocabularyEntry], accuracy: Double?)] = [
+ ([close[0]], 80),
+ ([close[0]], nil),
+ ([close[0]], -1),
+ (close, 10),
+ (Self.entries, 10),
+ ]
+ for (entries, accuracy) in cases {
+ let picker = makePicker(entries: entries)
+ picker.setLocation(Self.fix(accuracy: accuracy))
+ #expect(picker.selection == nil)
+ }
+ }
+
+ @Test("Nothing is chosen when pre-filling is off or a search is under way")
+ func prefillOff() {
+ let home = [Self.entry("home", "Home", uses: 900, at: Self.north(10))]
+ let off = makePicker(entries: home)
+ off.prefills = false
+ off.setLocation(Self.fix(accuracy: 10))
+ #expect(off.selection == nil)
+
+ let searching = makePicker(entries: home)
+ searching.query = "Ho"
+ searching.setLocation(Self.fix(accuracy: 10))
+ #expect(searching.selection == nil)
+ }
+
@Test("Clearing removes the choice and the search")
func clear() throws {
let picker = makePicker()
diff --git a/Tests/Unit/SurveyModelTests.swift b/Tests/Unit/SurveyModelTests.swift
@@ -192,6 +192,8 @@ struct SurveyModelTests {
#expect(places.location == context.location)
model.startCapture()
+ // The logged location is at Home, but a logged report isn't pre-filled.
+ #expect(places.selection == nil)
places.toggle(try #require(places.candidate(id: "P1")))
model[optionFor: "yes"] = ResponseValue.yesOption
let report = model.makeReport(now: Date(timeIntervalSince1970: 300), context: context).report