← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


commit 506b6cb55531ded8945a03b72a91af29da8f4507
parent 867d39ba17f4c607b3bef5585bb474dfb66a4251
Author: Michael Camilleri <[email protected]>
Date:   Mon, 19 Oct 2026 16:03:59 +0900

Hold an 'Always' location session for background captures

An Instant Report logged from a notification was saved without a
location or the weather, even with location set to 'Always'. The event
log showed Core Location sending a single update with no location and
flagging that a service session was required: the capture held a
'While Using' session, which doesn't cover an app launched in the
background for a notification action.

This commit has the capture hold an 'Always' session when 'Always' has
been granted, and a 'While Using' session otherwise, so a capture in
the foreground still asks for 'While Using' the first time. The event
log records which session was held.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Diffstat

MShared/Capture/ContextCapture.swift | 12+++++++++---

1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/Shared/Capture/ContextCapture.swift b/Shared/Capture/ContextCapture.swift

@@ -42,7 +42,7 @@ struct ContextCapture: Sendable {
     ) async -> Context {
         // The weather needs the fix, so the location capture hands it over
         // as soon as it has one, and the two lookups run side by side.
-        Self.log.info("Started capturing, within \(budget.formatted(.units(allowed: [.seconds])))")
+        Self.log.info("Started capturing, within \(budget.formatted(.units(allowed: [.seconds], width: .narrow)))")
         let start = ContinuousClock.now
         let (fixes, handOver) = AsyncStream.makeStream(of: CLLocation.self)
         async let location = captureLocation(onFix: { handOver.yield($0) }, onLocation: onLocation)
@@ -51,7 +51,7 @@ struct ContextCapture: Sendable {
         let captured = await location
         handOver.finish()
         let context = Context(location: captured, weather: await weather, device: await device)
-        let elapsed = start.duration(to: .now).formatted(.units(allowed: [.seconds], fractionalPart: .show(length: 1)))
+        let elapsed = start.duration(to: .now).formatted(.units(allowed: [.seconds], width: .narrow, fractionalPart: .show(length: 1)))
         Self.log.info(
             """
             Finished capturing after \(elapsed): location \(Self.found(context.location)), \
@@ -86,7 +86,13 @@ struct ContextCapture: Sendable {
     ) async -> LocationContext? {
         // Holding a session is what asks for authorisation. While the prompt
         // is on screen the budget hasn't started: the user is reading it.
-        let session = CLServiceSession(authorization: .whenInUse)
+        // With 'Always' granted, the session asks for it too: a 'While
+        // Using' session doesn't cover a capture in the background, such as
+        // an Instant Report from a notification, and Core Location holds
+        // its updates back.
+        let isAlways = CLLocationManager().authorizationStatus == .authorizedAlways
+        let session = CLServiceSession(authorization: isAlways ? .always : .whenInUse)
+        Self.log.info("Holding a location session for \(isAlways ? "'Always'" : "'While Using'")")
         defer { session.invalidate() }
         guard await Self.isAuthorised(session) else { return nil }
         guard let fix = await bestFix(within: budget - geocodingAllowance) else { return nil }