Skip to content

Commit edb38e0

Browse files
[google_maps_flutter] Semi-convert remaining iOS host API calls to Pigeon (flutter#7079)
Does a "shallow" Pigeon conversion of the remaining host API calls in the iOS implementation. All of the actual method calls are now Pigeon, but for the most part the data structures are not yet converted, and the Pigeon representations of the map objects are instead placeholders that currently just wrap the existing JSON serialization. This is done for a few reasons: - Keeps the incremental PR relatively small and easy to understand. - Quickly gets us to a state where any new APIs added will automatically use Pigeon, reducing further accumulation of technical debt. - Avoids duplication of handling code until flutter#150631 is resolved. As noted in a TODO added in this PR, almost all of the data structures that are passed in these methods are also passed through the PlatformView factory constructor, and Pigeon doesn't yet support using the Pigeon codec in non-Pigeon-generated code, so we would need to have both the structured *and* JSON handler for all of these objects if we converted them now. Future PRs will be able to incrementally convert each map object to a structured form. Converting the Flutter APIs (Java->Dart) will also be done in a follow-up, to limit the scope of this PR. This is the iOS equivalent of flutter/packages#6980, and the Dart code is largely the same. Part of flutter#117907
1 parent 47a92db commit edb38e0

24 files changed

+3610
-515
lines changed

packages/google_maps_flutter/google_maps_flutter_ios/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.9.0
2+
3+
* Converts additional platform calls to Pigeon.
4+
15
## 2.8.2
26

37
* Converts inspector interface platform calls to Pigeon.

packages/google_maps_flutter/google_maps_flutter_ios/example/ios14/ios/RunnerTests/FLTGoogleMapJSONConversionsConversionTests.m

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,26 @@ - (void)testDictionaryFromPosition {
113113
XCTAssertEqualObjects(dictionary[@"tilt"], @75.0);
114114
}
115115

116-
- (void)testDictionaryFromPoint {
116+
- (void)testPigeonPointForGCPoint {
117117
CGPoint point = CGPointMake(10, 20);
118-
NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromPoint:point];
119-
const CGFloat accuracy = 0.0001;
120-
XCTAssertEqualWithAccuracy([dictionary[@"x"] floatValue], point.x, accuracy);
121-
XCTAssertEqualWithAccuracy([dictionary[@"y"] floatValue], point.y, accuracy);
118+
FGMPlatformPoint *pigeonPoint = FGMGetPigeonPointForCGPoint(point);
119+
XCTAssertEqualWithAccuracy(pigeonPoint.x, point.x, DBL_EPSILON);
120+
XCTAssertEqualWithAccuracy(pigeonPoint.y, point.y, DBL_EPSILON);
122121
}
123122

124-
- (void)testDictionaryFromCoordinateBounds {
125-
XCTAssertNil([FLTGoogleMapJSONConversions dictionaryFromCoordinateBounds:nil]);
126-
123+
- (void)testPigeonLatLngBoundsForCoordinateBounds {
127124
GMSCoordinateBounds *bounds =
128125
[[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(10, 20)
129126
coordinate:CLLocationCoordinate2DMake(30, 40)];
130-
NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromCoordinateBounds:bounds];
131-
NSArray *southwest = @[ @10, @20 ];
132-
NSArray *northeast = @[ @30, @40 ];
133-
XCTAssertEqualObjects(dictionary[@"southwest"], southwest);
134-
XCTAssertEqualObjects(dictionary[@"northeast"], northeast);
127+
FGMPlatformLatLngBounds *pigeonBounds = FGMGetPigeonLatLngBoundsForCoordinateBounds(bounds);
128+
XCTAssertEqualWithAccuracy(pigeonBounds.southwest.latitude, bounds.southWest.latitude,
129+
DBL_EPSILON);
130+
XCTAssertEqualWithAccuracy(pigeonBounds.southwest.longitude, bounds.southWest.longitude,
131+
DBL_EPSILON);
132+
XCTAssertEqualWithAccuracy(pigeonBounds.northeast.latitude, bounds.northEast.latitude,
133+
DBL_EPSILON);
134+
XCTAssertEqualWithAccuracy(pigeonBounds.northeast.longitude, bounds.northEast.longitude,
135+
DBL_EPSILON);
135136
}
136137

137138
- (void)testCameraPostionFromDictionary {
@@ -151,19 +152,13 @@ - (void)testCameraPostionFromDictionary {
151152
XCTAssertEqualWithAccuracy(cameraPosition.viewingAngle, 5, accuracy);
152153
}
153154

154-
- (void)testPointFromDictionary {
155-
XCTAssertNil([FLTGoogleMapJSONConversions cameraPostionFromDictionary:nil]);
155+
- (void)testCGPointForPigeonPoint {
156+
FGMPlatformPoint *pigeonPoint = [FGMPlatformPoint makeWithX:1.0 y:2.0];
156157

157-
NSDictionary *dictionary = @{
158-
@"x" : @1,
159-
@"y" : @2,
160-
};
158+
CGPoint point = FGMGetCGPointForPigeonPoint(pigeonPoint);
161159

162-
CGPoint point = [FLTGoogleMapJSONConversions pointFromDictionary:dictionary];
163-
164-
const CGFloat accuracy = 0.001;
165-
XCTAssertEqualWithAccuracy(point.x, 1, accuracy);
166-
XCTAssertEqualWithAccuracy(point.y, 2, accuracy);
160+
XCTAssertEqualWithAccuracy(pigeonPoint.x, point.x, DBL_EPSILON);
161+
XCTAssertEqualWithAccuracy(pigeonPoint.y, point.y, DBL_EPSILON);
167162
}
168163

169164
- (void)testCoordinateBoundsFromLatLongs {
@@ -188,18 +183,18 @@ - (void)testMapViewTypeFromTypeValue {
188183
XCTAssertEqual(kGMSTypeNone, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@5]);
189184
}
190185

191-
- (void)testCameraUpdateFromChannelValueNewCameraPosition {
186+
- (void)testCameraUpdateFromArrayNewCameraPosition {
192187
NSArray *channelValue = @[
193188
@"newCameraPosition", @{@"target" : @[ @1, @2 ], @"zoom" : @3, @"bearing" : @4, @"tilt" : @5}
194189
];
195190
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
196-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
191+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValue];
197192
[[classMockCameraUpdate expect]
198193
setCamera:[FLTGoogleMapJSONConversions cameraPostionFromDictionary:channelValue[1]]];
199194
[classMockCameraUpdate stopMocking];
200195
}
201196

202-
// TODO(cyanglaz): Fix the test for CameraUpdateFromChannelValue with the "NewLatlng" key.
197+
// TODO(cyanglaz): Fix the test for cameraUpdateFromArray with the "NewLatlng" key.
203198
// 2 approaches have been tried and neither worked for the tests.
204199
//
205200
// 1. Use OCMock to vefiry that [GMSCameraUpdate setTarget:] is triggered with the correct value.
@@ -213,10 +208,10 @@ - (void)testCameraUpdateFromChannelValueNewCameraPosition {
213208
// verified.
214209
//
215210
// The code in below test uses the 2nd approach.
216-
- (void)skip_testCameraUpdateFromChannelValueNewLatLong {
211+
- (void)skip_testCameraUpdateFromArrayNewLatLong {
217212
NSArray *channelValue = @[ @"newLatLng", @[ @1, @2 ] ];
218213

219-
GMSCameraUpdate *update = [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
214+
GMSCameraUpdate *update = [FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValue];
220215

221216
GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
222217
options.frame = CGRectZero;
@@ -230,81 +225,81 @@ - (void)skip_testCameraUpdateFromChannelValueNewLatLong {
230225
accuracy); // mapView.camera.target.longitude is still 6.
231226
}
232227

233-
- (void)testCameraUpdateFromChannelValueNewLatLngBounds {
228+
- (void)testCameraUpdateFromArrayNewLatLngBounds {
234229
NSArray<NSNumber *> *latlong1 = @[ @1, @2 ];
235230
NSArray<NSNumber *> *latlong2 = @[ @(3), @(4) ];
236231
GMSCoordinateBounds *bounds =
237232
[FLTGoogleMapJSONConversions coordinateBoundsFromLatLongs:@[ latlong1, latlong2 ]];
238233

239234
NSArray *channelValue = @[ @"newLatLngBounds", @[ latlong1, latlong2 ], @20 ];
240235
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
241-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
236+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValue];
242237

243238
[[classMockCameraUpdate expect] fitBounds:bounds withPadding:20];
244239
[classMockCameraUpdate stopMocking];
245240
}
246241

247-
- (void)testCameraUpdateFromChannelValueNewLatLngZoom {
242+
- (void)testCameraUpdateFromArrayNewLatLngZoom {
248243
NSArray *channelValue = @[ @"newLatLngZoom", @[ @1, @2 ], @3 ];
249244

250245
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
251-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
246+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValue];
252247

253248
[[classMockCameraUpdate expect] setTarget:CLLocationCoordinate2DMake(1, 2) zoom:3];
254249
[classMockCameraUpdate stopMocking];
255250
}
256251

257-
- (void)testCameraUpdateFromChannelValueScrollBy {
252+
- (void)testCameraUpdateFromArrayScrollBy {
258253
NSArray *channelValue = @[ @"scrollBy", @1, @2 ];
259254

260255
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
261-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
256+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValue];
262257

263258
[[classMockCameraUpdate expect] scrollByX:1 Y:2];
264259
[classMockCameraUpdate stopMocking];
265260
}
266261

267-
- (void)testCameraUpdateFromChannelValueZoomBy {
262+
- (void)testCameraUpdateFromArrayZoomBy {
268263
NSArray *channelValueNoPoint = @[ @"zoomBy", @1 ];
269264

270265
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
271-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
266+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValueNoPoint];
272267

273268
[[classMockCameraUpdate expect] zoomBy:1];
274269

275270
NSArray *channelValueWithPoint = @[ @"zoomBy", @1, @[ @2, @3 ] ];
276271

277-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueWithPoint];
272+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValueWithPoint];
278273

279274
[[classMockCameraUpdate expect] zoomBy:1 atPoint:CGPointMake(2, 3)];
280275
[classMockCameraUpdate stopMocking];
281276
}
282277

283-
- (void)testCameraUpdateFromChannelValueZoomIn {
278+
- (void)testCameraUpdateFromArrayZoomIn {
284279
NSArray *channelValueNoPoint = @[ @"zoomIn" ];
285280

286281
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
287-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
282+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValueNoPoint];
288283

289284
[[classMockCameraUpdate expect] zoomIn];
290285
[classMockCameraUpdate stopMocking];
291286
}
292287

293-
- (void)testCameraUpdateFromChannelValueZoomOut {
288+
- (void)testCameraUpdateFromArrayZoomOut {
294289
NSArray *channelValueNoPoint = @[ @"zoomOut" ];
295290

296291
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
297-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
292+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValueNoPoint];
298293

299294
[[classMockCameraUpdate expect] zoomOut];
300295
[classMockCameraUpdate stopMocking];
301296
}
302297

303-
- (void)testCameraUpdateFromChannelValueZoomTo {
298+
- (void)testCameraUpdateFromArrayZoomTo {
304299
NSArray *channelValueNoPoint = @[ @"zoomTo", @1 ];
305300

306301
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
307-
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
302+
[FLTGoogleMapJSONConversions cameraUpdateFromArray:channelValueNoPoint];
308303

309304
[[classMockCameraUpdate expect] zoomTo:1];
310305
[classMockCameraUpdate stopMocking];

packages/google_maps_flutter/google_maps_flutter_ios/ios/Classes/FLTGoogleMapJSONConversions.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@
55
#import <Flutter/Flutter.h>
66
#import <GoogleMaps/GoogleMaps.h>
77

8+
#import "messages.g.h"
9+
810
NS_ASSUME_NONNULL_BEGIN
911

1012
/// Returns dict[key], or nil if dict[key] is NSNull.
1113
extern id _Nullable FGMGetValueOrNilFromDict(NSDictionary *dict, NSString *key);
1214

15+
/// Creates a CGPoint from its Pigeon equivalent.
16+
extern CGPoint FGMGetCGPointForPigeonPoint(FGMPlatformPoint *point);
17+
18+
/// Converts a CGPoint to its Pigeon equivalent.
19+
extern FGMPlatformPoint *FGMGetPigeonPointForCGPoint(CGPoint point);
20+
21+
/// Creates a CLLocationCoordinate2D from its Pigeon representation.
22+
extern CLLocationCoordinate2D FGMGetCoordinateForPigeonLatLng(FGMPlatformLatLng *latLng);
23+
24+
/// Converts a CLLocationCoordinate2D to its Pigeon representation.
25+
extern FGMPlatformLatLng *FGMGetPigeonLatLngForCoordinate(CLLocationCoordinate2D coord);
26+
27+
/// Converts a GMSCoordinateBounds to its Pigeon representation.
28+
extern FGMPlatformLatLngBounds *FGMGetPigeonLatLngBoundsForCoordinateBounds(
29+
GMSCoordinateBounds *bounds);
30+
1331
@interface FLTGoogleMapJSONConversions : NSObject
1432

1533
+ (CLLocationCoordinate2D)locationFromLatLong:(NSArray *)latlong;
@@ -20,13 +38,10 @@ extern id _Nullable FGMGetValueOrNilFromDict(NSDictionary *dict, NSString *key);
2038
+ (NSArray<NSArray<CLLocation *> *> *)holesFromPointsArray:(NSArray *)data;
2139
+ (nullable NSDictionary<NSString *, id> *)dictionaryFromPosition:
2240
(nullable GMSCameraPosition *)position;
23-
+ (NSDictionary<NSString *, NSNumber *> *)dictionaryFromPoint:(CGPoint)point;
24-
+ (nullable NSDictionary *)dictionaryFromCoordinateBounds:(nullable GMSCoordinateBounds *)bounds;
2541
+ (nullable GMSCameraPosition *)cameraPostionFromDictionary:(nullable NSDictionary *)channelValue;
26-
+ (CGPoint)pointFromDictionary:(NSDictionary *)dictionary;
2742
+ (GMSCoordinateBounds *)coordinateBoundsFromLatLongs:(NSArray *)latlongs;
2843
+ (GMSMapViewType)mapViewTypeFromTypeValue:(NSNumber *)value;
29-
+ (nullable GMSCameraUpdate *)cameraUpdateFromChannelValue:(NSArray *)channelValue;
44+
+ (nullable GMSCameraUpdate *)cameraUpdateFromArray:(NSArray *)channelValue;
3045

3146
/// Return GMS strokestyle object array populated using the patterns and stroke colors passed in.
3247
///

packages/google_maps_flutter/google_maps_flutter_ios/ios/Classes/FLTGoogleMapJSONConversions.m

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ id FGMGetValueOrNilFromDict(NSDictionary *dict, NSString *key) {
1010
return value == [NSNull null] ? nil : value;
1111
}
1212

13+
CGPoint FGMGetCGPointForPigeonPoint(FGMPlatformPoint *point) {
14+
return CGPointMake(point.x, point.y);
15+
}
16+
17+
FGMPlatformPoint *FGMGetPigeonPointForCGPoint(CGPoint point) {
18+
return [FGMPlatformPoint makeWithX:point.x y:point.y];
19+
}
20+
21+
CLLocationCoordinate2D FGMGetCoordinateForPigeonLatLng(FGMPlatformLatLng *latLng) {
22+
return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude);
23+
}
24+
25+
FGMPlatformLatLng *FGMGetPigeonLatLngForCoordinate(CLLocationCoordinate2D coord) {
26+
return [FGMPlatformLatLng makeWithLatitude:coord.latitude longitude:coord.longitude];
27+
}
28+
29+
FGMPlatformLatLngBounds *FGMGetPigeonLatLngBoundsForCoordinateBounds(GMSCoordinateBounds *bounds) {
30+
return
31+
[FGMPlatformLatLngBounds makeWithNortheast:FGMGetPigeonLatLngForCoordinate(bounds.northEast)
32+
southwest:FGMGetPigeonLatLngForCoordinate(bounds.southWest)];
33+
}
34+
1335
@implementation FLTGoogleMapJSONConversions
1436

1537
+ (CLLocationCoordinate2D)locationFromLatLong:(NSArray *)latlong {
@@ -67,23 +89,6 @@ + (UIColor *)colorFromRGBA:(NSNumber *)numberColor {
6789
};
6890
}
6991

70-
+ (NSDictionary<NSString *, NSNumber *> *)dictionaryFromPoint:(CGPoint)point {
71-
return @{
72-
@"x" : @(lroundf(point.x)),
73-
@"y" : @(lroundf(point.y)),
74-
};
75-
}
76-
77-
+ (nullable NSDictionary *)dictionaryFromCoordinateBounds:(GMSCoordinateBounds *)bounds {
78-
if (!bounds) {
79-
return nil;
80-
}
81-
return @{
82-
@"southwest" : [FLTGoogleMapJSONConversions arrayFromLocation:[bounds southWest]],
83-
@"northeast" : [FLTGoogleMapJSONConversions arrayFromLocation:[bounds northEast]],
84-
};
85-
}
86-
8792
+ (nullable GMSCameraPosition *)cameraPostionFromDictionary:(nullable NSDictionary *)data {
8893
if (!data) {
8994
return nil;
@@ -95,12 +100,6 @@ + (nullable GMSCameraPosition *)cameraPostionFromDictionary:(nullable NSDictiona
95100
viewingAngle:[data[@"tilt"] doubleValue]];
96101
}
97102

98-
+ (CGPoint)pointFromDictionary:(NSDictionary *)dictionary {
99-
double x = [dictionary[@"x"] doubleValue];
100-
double y = [dictionary[@"y"] doubleValue];
101-
return CGPointMake(x, y);
102-
}
103-
104103
+ (GMSCoordinateBounds *)coordinateBoundsFromLatLongs:(NSArray *)latlongs {
105104
return [[GMSCoordinateBounds alloc]
106105
initWithCoordinate:[FLTGoogleMapJSONConversions locationFromLatLong:latlongs[0]]
@@ -112,7 +111,7 @@ + (GMSMapViewType)mapViewTypeFromTypeValue:(NSNumber *)typeValue {
112111
return (GMSMapViewType)(value == 0 ? 5 : value);
113112
}
114113

115-
+ (nullable GMSCameraUpdate *)cameraUpdateFromChannelValue:(NSArray *)channelValue {
114+
+ (nullable GMSCameraUpdate *)cameraUpdateFromArray:(NSArray *)channelValue {
116115
NSString *update = channelValue[0];
117116
if ([update isEqualToString:@"newCameraPosition"]) {
118117
return [GMSCameraUpdate

packages/google_maps_flutter/google_maps_flutter_ios/ios/Classes/FLTGoogleMapTileOverlayController.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#import <Flutter/Flutter.h>
66
#import <GoogleMaps/GoogleMaps.h>
77

8+
#import "messages.g.h"
9+
810
NS_ASSUME_NONNULL_BEGIN
911

1012
@interface FLTGoogleMapTileOverlayController : NSObject
@@ -28,9 +30,10 @@ NS_ASSUME_NONNULL_BEGIN
2830
- (instancetype)init:(FlutterMethodChannel *)methodChannel
2931
mapView:(GMSMapView *)mapView
3032
registrar:(NSObject<FlutterPluginRegistrar> *)registrar;
31-
- (void)addTileOverlays:(NSArray *)tileOverlaysToAdd;
32-
- (void)changeTileOverlays:(NSArray *)tileOverlaysToChange;
33-
- (void)removeTileOverlayWithIdentifiers:(NSArray *)identifiers;
33+
- (void)addJSONTileOverlays:(NSArray<NSDictionary<NSString *, id> *> *)tileOverlaysToAdd;
34+
- (void)addTileOverlays:(NSArray<FGMPlatformTileOverlay *> *)tileOverlaysToAdd;
35+
- (void)changeTileOverlays:(NSArray<FGMPlatformTileOverlay *> *)tileOverlaysToChange;
36+
- (void)removeTileOverlayWithIdentifiers:(NSArray<NSString *> *)identifiers;
3437
- (void)clearTileCacheWithIdentifier:(NSString *)identifier;
3538
- (nullable FLTGoogleMapTileOverlayController *)tileOverlayWithIdentifier:(NSString *)identifier;
3639
@end

0 commit comments

Comments
 (0)