ReviewOS

also looking at this

craft-native/craft

feat(ios): location recording moves to the manager it always shared — ratchet 23 -> 18

#125
Merged glennmichael123 wants to merge feat/ios-location-recording into main
6 files +1653 -170

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

packages/ios/templates/CraftApp.swiftmodified+30-1
Changes to packages/ios/templates/CraftApp.swift
@@ -457,7 +457,12 @@ struct CraftWebView: UIViewRepresentable {
457457 locationManager?.desiredAccuracy = kCLLocationAccuracyBest
458458 locationManager?.activityType = .fitness
459459 locationManager?.pausesLocationUpdatesAutomatically = false
460 restoreLocationRecordingState()
460 // Zig owns the recorder when it is linked, including the
461 // relaunch restore — see `CraftZigRuntime.adoptLocationRecording`.
462 // Running both would leave two managers appending to one track.
463 if !CraftZigRuntime.adoptLocationRecording() {
464 restoreLocationRecordingState()
465 }
461466 }
462467 if config.enableContacts {
463468 contactStore = CNContactStore()
@@ -5465,6 +5470,7 @@ final class CraftZigRuntime: NSObject {
54655470 private typealias HandleActionFn = @convention(c) (
54665471 UnsafePointer<CChar>, UInt, UnsafePointer<CChar>, UInt, Int64
54675472 ) -> Bool
5473 private typealias AdoptLocationRecordingFn = @convention(c) () -> Bool
54685474
54695475 private static let image: UnsafeMutableRawPointer? = dlopen(nil, RTLD_NOW)
54705476
@@ -5483,6 +5489,11 @@ final class CraftZigRuntime: NSObject {
54835489 return unsafeBitCast(sym, to: HandleActionFn.self)
54845490 }()
54855491
5492 private static let adoptLocationRecordingFn: AdoptLocationRecordingFn? = {
5493 guard let sym = dlsym(image, "craft_ios_adopt_location_recording") else { return nil }
5494 return unsafeBitCast(sym, to: AdoptLocationRecordingFn.self)
5495 }()
5496
54865497 /// Whether this build has a Zig runtime at all. Read by nothing here; kept
54875498 /// because "is Zig linked" is the first question to ask when an action
54885499 /// behaves like the Swift one after a migration was supposed to move it.
@@ -5497,6 +5508,24 @@ final class CraftZigRuntime: NSObject {
54975508 setWebViewFn?(Unmanaged.passUnretained(webView).toOpaque())
54985509 }
54995510
5511 /// Hand a recording that outlived the last launch to whichever runtime owns
5512 /// the recorder.
5513 ///
5514 /// Returns true when Zig took it, and the caller must then *not* run
5515 /// `restoreLocationRecordingState()`. This is the one place the usual
5516 /// "offer it to Zig, fall back on false" pattern cannot be used through
5517 /// `handleAction`: there is no page message at launch, and the restore
5518 /// happens in `Coordinator.init` — before `attach`, because SwiftUI builds
5519 /// the coordinator before the view. Whoever restores first owns the
5520 /// `CLLocationManager` for the rest of the launch, so the decision has to
5521 /// be made here rather than at the first `stopLocationRecording`.
5522 ///
5523 /// False in a build with no Zig runtime, which is exactly when Swift's own
5524 /// restore is still the right thing to run.
5525 static func adoptLocationRecording() -> Bool {
5526 adoptLocationRecordingFn?() ?? false
5527 }
5528
55005529 /// Forget this webview, if Zig is still holding it.
55015530 ///
55025531 /// `attach` hands over an unretained pointer, so the moment SwiftUI