Skip to content

Commit 5a23f82

Browse files
[google_maps_flutter] Convert overlay controllers to Swift (flutter#12638)
This converts GroundOverlayController and TileOverlayController to Swift in the `_sdk*` packages. The remaining Obj-C code will be migrated in a series of follow-up PRs to keep the scope of each PR reasonable for review. The conversion process was: - Initial conversion via Gemini, with explicit instruction to keep the structure the same. - Side-by-side manual review of the old and new versions of the files. - Manual fixes and improvements. This includes test changes beyond what we would normally expect for a language conversion PR (e.g., name changes from dropping the prefix) because the conversion to Swift exposed a latent issue in the tests. Several tests had a pattern where a helper method was creating a local `mapView` instance, using it to construct the subcontroller under test, and then returning that subcontroller—at which point the map view goes out of scope. The subcontrollers have weak references to the map view, so it can be deallocated before the test actually runs, and doing so causes the tests to fail (as many operations in the subcontroller no-op when the map view is nil). In cases where the helper is only used once, it can be inlined; in cases where it's used repeatedly we can instead return multiple values and keep the map view alive. Part of flutter#119108 ## 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 f1a4d4b commit 5a23f82

46 files changed

Lines changed: 1435 additions & 1802 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎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.8
2+
3+
* Converts overlay controllers to Swift.
4+
15
## 2.18.7
26

37
* Converts circle, polygon, and polyline controllers to Swift.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class StubPluginRegistrar: NSObject, FlutterPluginRegistrar {
8787
}
8888

8989
@Test func handleResultTileDownsamplesWideGamutImages() throws {
90-
let controller = FGMTileProviderController()
90+
let controller = TileProviderController(
91+
tileOverlayIdentifier: "test",
92+
tileProvider: TestTileProvider(onTileCalled: {})
93+
)
9194

9295
let bundle = Bundle(for: MockCATransaction.self)
9396
let imagePath = try #require(

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import Testing
99

1010
@MainActor struct GroundOverlayControllerTests {
1111

12-
/// Returns a GroundOverlayController object instantiated with position and a mocked map
12+
/// Returns a GroundOverlayController object instantiated with position, and its mocked map
1313
/// instance.
14+
///
15+
/// The map must be retained for the life of the test.
1416
static func groundOverlayControllerWithPositionWithMockedMap() throws
15-
-> FGMGroundOverlayController
17+
-> (GroundOverlayController, GMSMapView)
1618
{
1719
let bundle = Bundle(for: PropertyOrderValidatingGroundOverlay.self)
1820
let imagePath = try #require(
@@ -26,17 +28,23 @@ import Testing
2628

2729
let mapView = GroundOverlayControllerTests.mapView()
2830

29-
return FGMGroundOverlayController(
30-
groundOverlay: groundOverlay,
31-
identifier: "id_1",
32-
mapView: mapView,
33-
isCreatedWithBounds: false
31+
return (
32+
GroundOverlayController(
33+
groundOverlay: groundOverlay,
34+
identifier: "id_1",
35+
mapView: mapView,
36+
isCreatedWithBounds: false
37+
),
38+
mapView
3439
)
3540
}
3641

37-
/// Returns a GroundOverlayController object instantiated with bounds and a mocked map
42+
/// Returns a GroundOverlayController object instantiated with bounds, and its mocked map
3843
/// instance.
39-
static func groundOverlayControllerWithBoundsWithMockedMap() throws -> FGMGroundOverlayController
44+
///
45+
/// The map must be retained for the life of the test.
46+
static func groundOverlayControllerWithBoundsWithMockedMap() throws
47+
-> (GroundOverlayController, GMSMapView)
4048
{
4149
let bundle = Bundle(for: PropertyOrderValidatingGroundOverlay.self)
4250
let imagePath = try #require(
@@ -52,16 +60,19 @@ import Testing
5260

5361
let mapView = GroundOverlayControllerTests.mapView()
5462

55-
return FGMGroundOverlayController(
56-
groundOverlay: groundOverlay,
57-
identifier: "id_1",
58-
mapView: mapView,
59-
isCreatedWithBounds: true
63+
return (
64+
GroundOverlayController(
65+
groundOverlay: groundOverlay,
66+
identifier: "id_1",
67+
mapView: mapView,
68+
isCreatedWithBounds: true
69+
),
70+
mapView
6071
)
6172
}
6273

6374
@Test func updatingGroundOverlayWithPosition() throws {
64-
let groundOverlayController =
75+
let (groundOverlayController, mapView) =
6576
try GroundOverlayControllerTests.groundOverlayControllerWithPositionWithMockedMap()
6677

6778
let position = FGMPlatformLatLng.make(withLatitude: 52.4816, longitude: 3.1791)
@@ -122,10 +133,11 @@ import Testing
122133
#expect(abs(convertedPlatformGroundOverlay.anchor!.x - 0.5) <= Double.ulpOfOne)
123134
#expect(abs(convertedPlatformGroundOverlay.anchor!.y - 0.5) <= Double.ulpOfOne)
124135
#expect(convertedPlatformGroundOverlay.zIndex == platformGroundOverlay.zIndex)
136+
withExtendedLifetime(mapView) {}
125137
}
126138

127139
@Test func updatingGroundOverlayWithBounds() throws {
128-
let groundOverlayController =
140+
let (groundOverlayController, mapView) =
129141
try GroundOverlayControllerTests.groundOverlayControllerWithBoundsWithMockedMap()
130142

131143
let bounds = FGMPlatformLatLngBounds.make(
@@ -194,11 +206,12 @@ import Testing
194206
#expect(abs(convertedPlatformGroundOverlay.anchor!.x - 0.5) <= Double.ulpOfOne)
195207
#expect(abs(convertedPlatformGroundOverlay.anchor!.y - 0.5) <= Double.ulpOfOne)
196208
#expect(convertedPlatformGroundOverlay.zIndex == platformGroundOverlay.zIndex)
209+
withExtendedLifetime(mapView) {}
197210
}
198211

199212
@Test func updateGroundOverlaySetsVisibilityLast() {
200213
let groundOverlay = PropertyOrderValidatingGroundOverlay()
201-
FGMGroundOverlayController.update(
214+
GroundOverlayController.update(
202215
groundOverlay,
203216
from: FGMPlatformGroundOverlay.make(
204217
withGroundOverlayId: "groundOverlay",
@@ -218,7 +231,7 @@ import Testing
218231
clickable: true,
219232
zoomLevel: nil
220233
),
221-
with: GroundOverlayControllerTests.mapView(),
234+
mapView: GroundOverlayControllerTests.mapView(),
222235
assetProvider: TestAssetProvider(),
223236
screenScale: 1.0,
224237
usingBounds: true
@@ -227,12 +240,12 @@ import Testing
227240
}
228241

229242
@Test func assetProviderIsRetained() {
230-
var groundOverlayController: FGMGroundOverlaysController?
243+
var groundOverlayController: GroundOverlaysController?
231244
weak var weakAssetProvider: TestAssetProvider?
232245
autoreleasepool {
233246
let assetProvider = TestAssetProvider()
234247
weakAssetProvider = assetProvider
235-
groundOverlayController = FGMGroundOverlaysController(
248+
groundOverlayController = GroundOverlaysController(
236249
mapView: GroundOverlayControllerTests.mapView(),
237250
eventDelegate: TestMapEventHandler(),
238251
assetProvider: assetProvider

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/RunnerTests-Bridging-Header.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,5 @@
33
// found in the LICENSE file.
44

55
// Import private _Test.h headers from the plugin framework
6-
#import <google_maps_flutter_ios_sdk10_objc/FGMGroundOverlayController_Test.h>
76
#import <google_maps_flutter_ios_sdk10_objc/FGMHeatmapController_Test.h>
87
#import <google_maps_flutter_ios_sdk10_objc/FGMMarkerController_Test.h>
9-
#import <google_maps_flutter_ios_sdk10_objc/FGMTileOverlayController_Test.h>
10-
11-
@interface FGMTileProviderController (Testing)
12-
- (UIImage *)handleResultTile:(nullable UIImage *)tileImage;
13-
@end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Testing
1111

1212
@Test func updateTileOverlaySetsVisibilityLast() {
1313
let tileLayer = PropertyOrderValidatingTileLayer()
14-
FGMTileOverlayController.update(
14+
TileOverlayController.update(
1515
tileLayer,
1616
from: FGMPlatformTileOverlay.make(
1717
withTileOverlayId: "overlay",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StubTileReceiver: NSObject, GMSTileReceiver {
1717
// A tile provider that expects a single call to
1818
// tileWithOverlayIdentifier:location:zoom:completion: on the main thread,
1919
// and then confirms it.
20-
class TestTileProvider: NSObject, FGMTileProviderDelegate {
20+
class TestTileProvider: NSObject, TileProviderDelegate {
2121
var onTileCalled: () -> Void
2222

2323
init(onTileCalled: @escaping () -> Void) {
@@ -44,7 +44,7 @@ class TestTileProvider: NSObject, FGMTileProviderDelegate {
4444
let tileProvider = TestTileProvider {
4545
continuationToResume?.resume()
4646
}
47-
let controller = FGMTileProviderController(
47+
let controller = TileProviderController(
4848
tileOverlayIdentifier: "foo",
4949
tileProvider: tileProvider
5050
)

‎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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
128128
let polylinesController: PolylinesController
129129
let circlesController: CirclesController
130130
let heatmapsController: FGMHeatmapsController
131-
let tileOverlaysController: FGMTileOverlaysController
132-
let groundOverlaysController: FGMGroundOverlaysController
131+
let tileOverlaysController: TileOverlaysController
132+
let groundOverlaysController: GroundOverlaysController
133133

134134
// The resulting error message, if any, from the last attempt to set the map style.
135135
// This is used to provide access to errors after the fact, since the map style is generally set at
@@ -223,11 +223,11 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
223223
)
224224
heatmapsController = FGMHeatmapsController(mapView: mapView)
225225
tileProvider = ConcreteTileProvider(dartCallbackHandler: dartCallbackHandler)
226-
tileOverlaysController = FGMTileOverlaysController(
226+
tileOverlaysController = TileOverlaysController(
227227
mapView: mapView,
228228
tileProvider: tileProvider
229229
)
230-
groundOverlaysController = FGMGroundOverlaysController(
230+
groundOverlaysController = GroundOverlaysController(
231231
mapView: mapView,
232232
eventDelegate: mapEventHandler,
233233
assetProvider: assetProvider
@@ -517,7 +517,7 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
517517
// TODO(stuartmorgan): Remove this in favor of an extension to add FGMTileProviderDelegate to
518518
// the Pigeon Flutter API object once this plugin has switched to Swift Pigeon generation
519519
// (adjusting the protocol to match the Swift version of the signature).
520-
private class ConcreteTileProvider: NSObject, FGMTileProviderDelegate {
520+
private class ConcreteTileProvider: NSObject, TileProviderDelegate {
521521
let handler: FGMMapsCallbackApi
522522

523523
init(dartCallbackHandler: FGMMapsCallbackApi) {

0 commit comments

Comments
 (0)