Skip to content

Fix/camera state stalls #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 2, 2025

Conversation

Archdoog
Copy link
Collaborator

@Archdoog Archdoog commented Apr 30, 2025

Issue/Motivation

  • Corrects race conditions between camera updates by leveraging a modern concurrency based camera. This camera will wait for the map to hit idle unless cancelled by another MapViewCamera update.

Tasklist

  • Include tests (if applicable) and examples (new or edits)
  • If there are any visual changes as a result, include before/after screenshots and/or videos
  • Add #fixes with the issue number that this PR addresses
  • Update any documentation for affected APIs

@Archdoog Archdoog requested review from Copilot, ianthetechie and hactar and removed request for ianthetechie April 30, 2025 04:19
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a camera state stalling issue by addressing race conditions between camera updates through modern concurrency. Key changes include:

  • Converting test methods to async to better simulate asynchronous camera updates.
  • Replacing the old updateCamera API with the new applyCameraChangeFromStateUpdate method.
  • Updating camera-related models and dependencies (e.g., MapViewCamera, Package.swift).

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Tests/MapLibreSwiftUITests/MapViewCoordinator/MapViewCoordinatorCameraTests.swift Converted tests to async and updated camera update calls.
Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift Added Equatable and Sendable conformances to MapViewCamera.
Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift Added Equatable and Sendable conformances to CameraState.
Sources/MapLibreSwiftUI/MapViewCoordinatorCamera.swift Added new concurrency-based camera update methods, including handling gestures asynchronously.
Sources/MapLibreSwiftUI/MapViewCoordinator.swift Updated region change handling to use the new gesture-based API and removed old camera update code.
Sources/MapLibreSwiftUI/MapView.swift Replaced updateCamera with applyCameraChangeFromStateUpdate; added a debug print for initial camera state.
Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift Added new properties for centerCoordinate and zoomLevel.
Package.swift Updated swift-snapshot-testing dependency from 1.17.7 to 1.18.3.
Comments suppressed due to low confidence (1)

Package.swift:33

  • Ensure that updating the snapshot-testing dependency to version 1.18.3 does not introduce breaking changes in the snapshot tests.
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.3"),

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses race conditions in camera updates by leveraging modern concurrency constructs and refactoring camera update handling throughout the codebase. Key changes include:

  • Transition to async/await in camera update tests and coordinator methods.
  • Replacement of the legacy updateCamera API with applyCameraChangeFromStateUpdate.
  • Updates to protocol definitions and dependency versions to support the new concurrency model.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/MapLibreSwiftUITests/MapViewCoordinator/MapViewCoordinatorCameraTests.swift Updated tests to use async/await and simulateCameraUpdateAndWait for verifying camera updates.
Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift Added Equatable and Sendable conformances to support concurrent usage.
Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift Added Equatable and Sendable conformances.
Sources/MapLibreSwiftUI/MapViewCoordinatorCamera.swift Introduced an async approach in applyCameraChangeFromStateUpdate and gesture-based update handling.
Sources/MapLibreSwiftUI/MapViewCoordinator.swift Removed the legacy updateCamera method and refactored regionDidChangeWith to trigger gesture-based updates.
Sources/MapLibreSwiftUI/MapView.swift Refactored to call the new applyCameraChangeFromStateUpdate method.
Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift Extended the protocol with centerCoordinate and zoomLevel properties for better camera state control.
Package.swift Updated the snapshot-testing dependency version.
Comments suppressed due to low confidence (2)

Sources/MapLibreSwiftUI/MapViewCoordinator.swift:180

  • Only gesture-driven camera changes invoke applyCameraChangeFromGesture; non-gesture (programmatic) camera updates are no longer propagated to the parent binding. Please confirm that this behavior is intentional and does not omit necessary state synchronization.
switch changeReason {

Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift:12

  • The new centerCoordinate and zoomLevel properties have been added to the protocol. It is recommended to add or verify documentation and implementation details to ensure consistent behavior across all conforming types.
@MainActor var centerCoordinate: CLLocationCoordinate2D { get set }

// This must be weak, the UIViewRepresentable owns the MLNMapView.
weak var mapView: MLNMapView?
var parent: MapView<T>

// Storage of variables as they were previously; these are snapshot
// every update cycle so we can avoid unnecessary updates
private var snapshotUserLayers: [StyleLayerDefinition] = []
private var snapshotCamera: MapViewCamera?
var snapshotCamera: MapViewCamera?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be private since it's internal state that we don't want to modify. I assume the change is due to a lot of this functionality moving to an extension, which is in another file.

IMO this is a minor language design flaw, but we could still keep things in a separate extension and just move the code to the same file if you want that organizational barrier. But I'm not really sure opening up write access to these variables to get smaller files is the right design.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Reverted.

I just very much enjoy scoped extensions especially working through these systems where tons of unrelated behaviors look similar.

Copy link
Collaborator

@hactar hactar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as Ian. If this works its a nice clean up. I'll test it in my apps once its merged.

Copy link
Collaborator

@ianthetechie ianthetechie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good to merge to me.

@ianthetechie ianthetechie merged commit 579c378 into maplibre:main May 2, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants