Skip to content

Commit a3ffe32

Browse files
[google_maps_flutter] Adopts new async/await Swift Pigeon support (flutter#12860)
Switches from the callback-based Pigeon API to the new async/await-based API, which is: - more idiomatic modern Swift - safer, both since it uses Pigeon-generated actor annotations to ensure proper threading, and because it prevents the error case of calling the callback some number of times other than exactly once (which violates the engine API contract) - the syntax used by the new FFI backend that we plan to switch to in a follow-up I verified that with this version of Pigeon we are no longer seeing thread violation logging in manual testing (see flutter#192199) There is some extra `Task` boilerplate in many callbacks still because we are implementing APIs that are defined by the Google Maps API, and those are not yet using `async` and actor annotations, so we have to bridge them in the plugin layer. Part of flutter#192720 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 469b97b commit a3ffe32

40 files changed

Lines changed: 1395 additions & 1510 deletions

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.18.13
2+
3+
* Adopts new Pigeon async Swift support.
4+
15
## 2.18.12
26

37
* Converts remaining Objective-C code to Swift.

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/TestUtils/TestMapEventHandler.swift‎

Lines changed: 33 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,50 @@ import Foundation
88

99
/// Fake implementation of MapEventDelegate for unit tests.
1010
class TestMapEventHandler: MapEventDelegate {
11-
func didStartCameraMove(completion: @escaping (Result<Void, PigeonError>) -> Void) {}
12-
13-
func didMoveCamera(
14-
to cameraPositionArg: PlatformCameraPosition,
15-
completion:
16-
@escaping (
17-
Result<Void, PigeonError>
18-
) -> Void
19-
) {}
20-
21-
func didIdleCamera(completion: @escaping (Result<Void, PigeonError>) -> Void) {}
22-
23-
func didTap(
24-
at positionArg: PlatformLatLng,
25-
completion:
26-
@escaping (
27-
Result<Void, PigeonError>
28-
) -> Void
29-
) {}
30-
31-
func didLongPress(
32-
at positionArg: PlatformLatLng,
33-
completion:
34-
@escaping (
35-
Result<Void, PigeonError>
36-
) -> Void
37-
) {}
38-
39-
func didTapMarker(
40-
withIdentifier markerIdArg: String,
41-
completion: @escaping (Result<Void, PigeonError>) -> Void
42-
) {}
11+
func didStartCameraMove() async throws {}
12+
13+
func didMoveCamera(to cameraPositionArg: PlatformCameraPosition) async throws {}
14+
15+
func didIdleCamera() async throws {}
16+
17+
func didTap(at positionArg: PlatformLatLng) async throws {}
18+
19+
func didLongPress(at positionArg: PlatformLatLng) async throws {}
20+
21+
func didTapMarker(withIdentifier markerIdArg: String) async throws {}
4322

4423
func didStartDragForMarker(
4524
withIdentifier markerIdArg: String,
46-
at positionArg: PlatformLatLng,
47-
completion: @escaping (Result<Void, PigeonError>) -> Void
48-
) {}
25+
at positionArg: PlatformLatLng
26+
) async throws {}
4927

5028
func didDragMarker(
5129
withIdentifier markerIdArg: String,
52-
at positionArg: PlatformLatLng,
53-
completion: @escaping (Result<Void, PigeonError>) -> Void
54-
) {}
30+
at positionArg: PlatformLatLng
31+
) async throws {}
5532

5633
func didEndDragForMarker(
5734
withIdentifier markerIdArg: String,
58-
at positionArg: PlatformLatLng,
59-
completion:
60-
@escaping (
61-
Result<Void, PigeonError>
62-
) -> Void
63-
) {}
64-
65-
func didTapInfoWindowOfMarker(
66-
withIdentifier markerIdArg: String,
67-
completion:
68-
@escaping (
69-
Result<Void, PigeonError>
70-
) -> Void
71-
) {}
72-
73-
func didTapCircle(
74-
withIdentifier circleIdArg: String,
75-
completion: @escaping (Result<Void, PigeonError>) -> Void
76-
) {}
77-
78-
func didTapCluster(
79-
_ clusterArg: PlatformCluster,
80-
completion:
81-
@escaping (
82-
Result<Void, PigeonError>
83-
) -> Void
84-
) {}
85-
86-
func didTapPolygon(
87-
withIdentifier polygonIdArg: String,
88-
completion:
89-
@escaping (
90-
Result<Void, PigeonError>
91-
) -> Void
92-
) {}
93-
94-
func didTapPolyline(
95-
withIdentifier polylineIdArg: String,
96-
completion: @escaping (Result<Void, PigeonError>) -> Void
97-
) {}
98-
99-
func didTapGroundOverlay(
100-
withIdentifier groundOverlayIdArg: String,
101-
completion:
102-
@escaping (
103-
Result<Void, PigeonError>
104-
) -> Void
105-
) {}
35+
at positionArg: PlatformLatLng
36+
) async throws {}
37+
38+
func didTapInfoWindowOfMarker(withIdentifier markerIdArg: String) async throws {}
39+
40+
func didTapCircle(withIdentifier circleIdArg: String) async throws {}
41+
42+
func didTapCluster(_ clusterArg: PlatformCluster) async throws {}
43+
44+
func didTapPolygon(withIdentifier polygonIdArg: String) async throws {}
45+
46+
func didTapPolyline(withIdentifier polylineIdArg: String) async throws {}
47+
48+
func didTapGroundOverlay(withIdentifier groundOverlayIdArg: String) async throws {}
10649

10750
func tile(
10851
withOverlayIdentifier tileOverlayIdArg: String,
10952
location locationArg: PlatformPoint,
110-
zoom zoomArg: Int64,
111-
completion:
112-
@escaping (
113-
Result<PlatformTile, PigeonError>
114-
) -> Void
115-
) {}
53+
zoom zoomArg: Int64
54+
) async throws -> PlatformTile {
55+
return PlatformTile(width: 0, height: 0)
56+
}
11657
}

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/TileProviderControllerTests.swift‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ class TestTileProvider: TileProviderDelegate {
2424
self.onTileCalled = onTileCalled
2525
}
2626

27-
func tile(
27+
@MainActor func tile(
2828
withOverlayIdentifier tileOverlayId: String,
2929
location: PlatformPoint,
3030
zoom: Int64,
31-
completion: @escaping (Result<PlatformTile, PigeonError>) -> Void
32-
) {
33-
#expect(Thread.isMainThread)
31+
) async throws -> PlatformTile {
3432
onTileCalled()
33+
return PlatformTile(width: 0, height: 0)
3534
}
3635
}
3736

3837
@MainActor struct TileProviderControllerTests {
3938

40-
@Test func callChannelOnPlatformThread() async {
39+
@Test func tileProviderCallsFlutterApi() async {
4140
var continuationToResume: CheckedContinuation<Void, Never>?
4241

4342
let tileProvider = TestTileProvider {

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/CircleController.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class CirclesController {
8686

8787
func didTapCircle(withIdentifier identifier: String) {
8888
if hasCircle(withIdentifier: identifier) {
89-
eventDelegate?.didTapCircle(withIdentifier: identifier) { _ in }
89+
Task {
90+
try await eventDelegate?.didTapCircle(withIdentifier: identifier)
91+
}
9092
}
9193
}
9294
}

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/ClusterManagersController.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ class ClusterManagersController {
7373
guard let clusterManagerId = clusterManagerIdentifier(for: cluster) else { return }
7474
let platformCluster = PlatformCluster.make(
7575
from: cluster, clusterManagerIdentifier: clusterManagerId)
76-
eventDelegate?.didTapCluster(platformCluster) { _ in }
76+
Task {
77+
try await eventDelegate?.didTapCluster(platformCluster)
78+
}
7779
}
7880

7981
/// Returns the cluster manager identifier for given cluster.

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/GoogleMapController.swift‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,23 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
313313
// MARK: - GMSMapViewDelegate methods
314314

315315
public func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
316-
dartCallbackHandler.didStartCameraMove { _ in }
316+
Task {
317+
try await dartCallbackHandler.didStartCameraMove()
318+
}
317319
}
318320

319321
public func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
320322
if trackCameraPosition {
321-
dartCallbackHandler.didMoveCamera(to: PlatformCameraPosition.make(from: position)) { _ in }
323+
Task {
324+
try await dartCallbackHandler.didMoveCamera(to: PlatformCameraPosition.make(from: position))
325+
}
322326
}
323327
}
324328

325329
public func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
326-
dartCallbackHandler.didIdleCamera { _ in }
330+
Task {
331+
try await dartCallbackHandler.didIdleCamera()
332+
}
327333
}
328334

329335
public func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
@@ -379,11 +385,15 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
379385
}
380386

381387
public func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
382-
dartCallbackHandler.didTap(at: PlatformLatLng.make(from: coordinate)) { _ in }
388+
Task {
389+
try await dartCallbackHandler.didTap(at: PlatformLatLng.make(from: coordinate))
390+
}
383391
}
384392

385393
public func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
386-
dartCallbackHandler.didLongPress(at: PlatformLatLng.make(from: coordinate)) { _ in }
394+
Task {
395+
try await dartCallbackHandler.didLongPress(at: PlatformLatLng.make(from: coordinate))
396+
}
387397
}
388398

389399
func interpretMapConfiguration(_ config: PlatformMapConfiguration) {

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/GroundOverlayController.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ class GroundOverlaysController {
197197

198198
func didTapGroundOverlay(withIdentifier identifier: String) {
199199
if hasGroundOverlays(withIdentifier: identifier) {
200-
eventDelegate?.didTapGroundOverlay(withIdentifier: identifier) { _ in }
200+
Task {
201+
try await eventDelegate?.didTapGroundOverlay(withIdentifier: identifier)
202+
}
201203
}
202204
}
203205

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/MarkerController.swift‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,37 +217,47 @@ class MarkersController {
217217

218218
func didTapMarker(withIdentifier identifier: String) -> Bool {
219219
guard let controller = markerIdentifierToController[identifier] else { return false }
220-
eventDelegate?.didTapMarker(withIdentifier: identifier) { _ in }
220+
Task {
221+
try await eventDelegate?.didTapMarker(withIdentifier: identifier)
222+
}
221223
return controller.consumeTapEvents
222224
}
223225

224226
func didStartDraggingMarker(withIdentifier identifier: String, location: CLLocationCoordinate2D) {
225227
guard markerIdentifierToController[identifier] != nil else { return }
226-
eventDelegate?.didStartDragForMarker(
227-
withIdentifier: identifier,
228-
at: PlatformLatLng.make(from: location)
229-
) { _ in }
228+
Task {
229+
try await eventDelegate?.didStartDragForMarker(
230+
withIdentifier: identifier,
231+
at: PlatformLatLng.make(from: location)
232+
)
233+
}
230234
}
231235

232236
func didDragMarker(withIdentifier identifier: String, location: CLLocationCoordinate2D) {
233237
guard markerIdentifierToController[identifier] != nil else { return }
234-
eventDelegate?.didDragMarker(
235-
withIdentifier: identifier,
236-
at: PlatformLatLng.make(from: location)
237-
) { _ in }
238+
Task {
239+
try await eventDelegate?.didDragMarker(
240+
withIdentifier: identifier,
241+
at: PlatformLatLng.make(from: location)
242+
)
243+
}
238244
}
239245

240246
func didEndDraggingMarker(withIdentifier identifier: String, location: CLLocationCoordinate2D) {
241247
guard markerIdentifierToController[identifier] != nil else { return }
242-
eventDelegate?.didEndDragForMarker(
243-
withIdentifier: identifier,
244-
at: PlatformLatLng.make(from: location)
245-
) { _ in }
248+
Task {
249+
try await eventDelegate?.didEndDragForMarker(
250+
withIdentifier: identifier,
251+
at: PlatformLatLng.make(from: location)
252+
)
253+
}
246254
}
247255

248256
func didTapInfoWindowOfMarker(withIdentifier identifier: String) {
249257
if markerIdentifierToController[identifier] != nil {
250-
eventDelegate?.didTapInfoWindowOfMarker(withIdentifier: identifier) { _ in }
258+
Task {
259+
try await eventDelegate?.didTapInfoWindowOfMarker(withIdentifier: identifier)
260+
}
251261
}
252262
}
253263

0 commit comments

Comments
 (0)