Skip to content

[google_maps_flutter] Fix Obj-C type handling #7010

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 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.8.1

* Improves Objective-C type handling.

## 2.8.0

* Adds compatibility with SDK version 9.x for apps targetting iOS 15+.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ @interface FLTGoogleMapJSONConversionsTests : XCTestCase

@implementation FLTGoogleMapJSONConversionsTests

- (void)testGetValueOrNilWithValue {
NSString *key = @"key";
NSString *value = @"value";
NSDictionary<NSString *, id> *dict = @{key : value};

XCTAssertEqual(FGMGetValueOrNilFromDict(dict, key), value);
}

- (void)testGetValueOrNilWithNoEntry {
NSString *key = @"key";
NSDictionary<NSString *, id> *dict = @{};

XCTAssertNil(FGMGetValueOrNilFromDict(dict, key));
}

- (void)testGetValueOrNilWithNSNull {
NSString *key = @"key";
NSDictionary<NSString *, id> *dict = @{key : [NSNull null]};

XCTAssertNil(FGMGetValueOrNilFromDict(dict, key));
}

- (void)testLocationFromLatLong {
NSArray<NSNumber *> *latlong = @[ @1, @2 ];
CLLocationCoordinate2D location = [FLTGoogleMapJSONConversions locationFromLatLong:latlong];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

NS_ASSUME_NONNULL_BEGIN

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

@interface FLTGoogleMapJSONConversions : NSObject

+ (CLLocationCoordinate2D)locationFromLatLong:(NSArray *)latlong;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

#import "FLTGoogleMapJSONConversions.h"

/// Returns dict[key], or nil if dict[key] is NSNull.
id FGMGetValueOrNilFromDict(NSDictionary *dict, NSString *key) {
id value = dict[key];
return value == [NSNull null] ? nil : value;
}

@implementation FLTGoogleMapJSONConversions

+ (CLLocationCoordinate2D)locationFromLatLong:(NSArray *)latlong {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,28 @@ - (void)interpretTileOverlayOptions:(NSDictionary *)data {
if (!data) {
return;
}
NSNumber *visible = data[@"visible"];
if (visible != nil && visible != (id)[NSNull null]) {
NSNumber *visible = FGMGetValueOrNilFromDict(data, @"visible");
if (visible) {
[self setVisible:visible.boolValue];
}

NSNumber *transparency = data[@"transparency"];
if (transparency != nil && transparency != (id)[NSNull null]) {
NSNumber *transparency = FGMGetValueOrNilFromDict(data, @"transparency");
if (transparency) {
[self setTransparency:transparency.floatValue];
}

NSNumber *zIndex = data[@"zIndex"];
if (zIndex != nil && zIndex != (id)[NSNull null]) {
NSNumber *zIndex = FGMGetValueOrNilFromDict(data, @"zIndex");
if (zIndex) {
[self setZIndex:zIndex.intValue];
}

NSNumber *fadeIn = data[@"fadeIn"];
if (fadeIn != nil && fadeIn != (id)[NSNull null]) {
NSNumber *fadeIn = FGMGetValueOrNilFromDict(data, @"fadeIn");
if (fadeIn) {
[self setFadeIn:fadeIn.boolValue];
}

NSNumber *tileSize = data[@"tileSize"];
if (tileSize != nil && tileSize != (id)[NSNull null]) {
NSNumber *tileSize = FGMGetValueOrNilFromDict(data, @"tileSize");
if (tileSize) {
[self setTileSize:tileSize.integerValue];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,43 @@ - (void)setFillColor:(UIColor *)color {
}

- (void)interpretCircleOptions:(NSDictionary *)data {
NSNumber *consumeTapEvents = data[@"consumeTapEvents"];
if (consumeTapEvents && consumeTapEvents != (id)[NSNull null]) {
NSNumber *consumeTapEvents = FGMGetValueOrNilFromDict(data, @"consumeTapEvents");
if (consumeTapEvents) {
[self setConsumeTapEvents:consumeTapEvents.boolValue];
}

NSNumber *visible = data[@"visible"];
if (visible && visible != (id)[NSNull null]) {
NSNumber *visible = FGMGetValueOrNilFromDict(data, @"visible");
if (visible) {
[self setVisible:[visible boolValue]];
}

NSNumber *zIndex = data[@"zIndex"];
if (zIndex && zIndex != (id)[NSNull null]) {
NSNumber *zIndex = FGMGetValueOrNilFromDict(data, @"zIndex");
if (zIndex) {
[self setZIndex:[zIndex intValue]];
}

NSArray *center = data[@"center"];
if (center && center != (id)[NSNull null]) {
NSArray *center = FGMGetValueOrNilFromDict(data, @"center");
if (center) {
[self setCenter:[FLTGoogleMapJSONConversions locationFromLatLong:center]];
}

NSNumber *radius = data[@"radius"];
if (radius && radius != (id)[NSNull null]) {
NSNumber *radius = FGMGetValueOrNilFromDict(data, @"radius");
if (radius) {
[self setRadius:[radius floatValue]];
}

NSNumber *strokeColor = data[@"strokeColor"];
if (strokeColor && strokeColor != (id)[NSNull null]) {
NSNumber *strokeColor = FGMGetValueOrNilFromDict(data, @"strokeColor");
if (strokeColor) {
[self setStrokeColor:[FLTGoogleMapJSONConversions colorFromRGBA:strokeColor]];
}

NSNumber *strokeWidth = data[@"strokeWidth"];
if (strokeWidth && strokeWidth != (id)[NSNull null]) {
NSNumber *strokeWidth = FGMGetValueOrNilFromDict(data, @"strokeWidth");
if (strokeWidth) {
[self setStrokeWidth:[strokeWidth intValue]];
}

NSNumber *fillColor = data[@"fillColor"];
if (fillColor && fillColor != (id)[NSNull null]) {
NSNumber *fillColor = FGMGetValueOrNilFromDict(data, @"fillColor");
if (fillColor) {
[self setFillColor:[FLTGoogleMapJSONConversions colorFromRGBA:fillColor]];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,41 +581,41 @@ - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordi
}

- (void)interpretMapOptions:(NSDictionary *)data {
NSArray *cameraTargetBounds = data[@"cameraTargetBounds"];
if (cameraTargetBounds && cameraTargetBounds != (id)[NSNull null]) {
NSArray *cameraTargetBounds = FGMGetValueOrNilFromDict(data, @"cameraTargetBounds");
if (cameraTargetBounds) {
[self
setCameraTargetBounds:cameraTargetBounds.count > 0 && cameraTargetBounds[0] != [NSNull null]
? [FLTGoogleMapJSONConversions
coordinateBoundsFromLatLongs:cameraTargetBounds.firstObject]
: nil];
}
NSNumber *compassEnabled = data[@"compassEnabled"];
if (compassEnabled && compassEnabled != (id)[NSNull null]) {
NSNumber *compassEnabled = FGMGetValueOrNilFromDict(data, @"compassEnabled");
if (compassEnabled) {
[self setCompassEnabled:[compassEnabled boolValue]];
}
id indoorEnabled = data[@"indoorEnabled"];
if (indoorEnabled && indoorEnabled != [NSNull null]) {
id indoorEnabled = FGMGetValueOrNilFromDict(data, @"indoorEnabled");
if (indoorEnabled) {
[self setIndoorEnabled:[indoorEnabled boolValue]];
}
id trafficEnabled = data[@"trafficEnabled"];
if (trafficEnabled && trafficEnabled != [NSNull null]) {
id trafficEnabled = FGMGetValueOrNilFromDict(data, @"trafficEnabled");
if (trafficEnabled) {
[self setTrafficEnabled:[trafficEnabled boolValue]];
}
id buildingsEnabled = data[@"buildingsEnabled"];
if (buildingsEnabled && buildingsEnabled != [NSNull null]) {
id buildingsEnabled = FGMGetValueOrNilFromDict(data, @"buildingsEnabled");
if (buildingsEnabled) {
[self setBuildingsEnabled:[buildingsEnabled boolValue]];
}
id mapType = data[@"mapType"];
if (mapType && mapType != [NSNull null]) {
id mapType = FGMGetValueOrNilFromDict(data, @"mapType");
if (mapType) {
[self setMapType:[FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:mapType]];
}
NSArray *zoomData = data[@"minMaxZoomPreference"];
if (zoomData && zoomData != (id)[NSNull null]) {
NSArray *zoomData = FGMGetValueOrNilFromDict(data, @"minMaxZoomPreference");
if (zoomData) {
float minZoom = (zoomData[0] == [NSNull null]) ? kGMSMinZoomLevel : [zoomData[0] floatValue];
float maxZoom = (zoomData[1] == [NSNull null]) ? kGMSMaxZoomLevel : [zoomData[1] floatValue];
[self setMinZoom:minZoom maxZoom:maxZoom];
}
NSArray *paddingData = data[@"padding"];
NSArray *paddingData = FGMGetValueOrNilFromDict(data, @"padding");
if (paddingData) {
float top = (paddingData[0] == [NSNull null]) ? 0 : [paddingData[0] floatValue];
float left = (paddingData[1] == [NSNull null]) ? 0 : [paddingData[1] floatValue];
Expand All @@ -624,35 +624,35 @@ - (void)interpretMapOptions:(NSDictionary *)data {
[self setPaddingTop:top left:left bottom:bottom right:right];
}

NSNumber *rotateGesturesEnabled = data[@"rotateGesturesEnabled"];
if (rotateGesturesEnabled && rotateGesturesEnabled != (id)[NSNull null]) {
NSNumber *rotateGesturesEnabled = FGMGetValueOrNilFromDict(data, @"rotateGesturesEnabled");
if (rotateGesturesEnabled) {
[self setRotateGesturesEnabled:[rotateGesturesEnabled boolValue]];
}
NSNumber *scrollGesturesEnabled = data[@"scrollGesturesEnabled"];
if (scrollGesturesEnabled && scrollGesturesEnabled != (id)[NSNull null]) {
NSNumber *scrollGesturesEnabled = FGMGetValueOrNilFromDict(data, @"scrollGesturesEnabled");
if (scrollGesturesEnabled) {
[self setScrollGesturesEnabled:[scrollGesturesEnabled boolValue]];
}
NSNumber *tiltGesturesEnabled = data[@"tiltGesturesEnabled"];
if (tiltGesturesEnabled && tiltGesturesEnabled != (id)[NSNull null]) {
NSNumber *tiltGesturesEnabled = FGMGetValueOrNilFromDict(data, @"tiltGesturesEnabled");
if (tiltGesturesEnabled) {
[self setTiltGesturesEnabled:[tiltGesturesEnabled boolValue]];
}
NSNumber *trackCameraPosition = data[@"trackCameraPosition"];
if (trackCameraPosition && trackCameraPosition != (id)[NSNull null]) {
NSNumber *trackCameraPosition = FGMGetValueOrNilFromDict(data, @"trackCameraPosition");
if (trackCameraPosition) {
[self setTrackCameraPosition:[trackCameraPosition boolValue]];
}
NSNumber *zoomGesturesEnabled = data[@"zoomGesturesEnabled"];
if (zoomGesturesEnabled && zoomGesturesEnabled != (id)[NSNull null]) {
NSNumber *zoomGesturesEnabled = FGMGetValueOrNilFromDict(data, @"zoomGesturesEnabled");
if (zoomGesturesEnabled) {
[self setZoomGesturesEnabled:[zoomGesturesEnabled boolValue]];
}
NSNumber *myLocationEnabled = data[@"myLocationEnabled"];
if (myLocationEnabled && myLocationEnabled != (id)[NSNull null]) {
NSNumber *myLocationEnabled = FGMGetValueOrNilFromDict(data, @"myLocationEnabled");
if (myLocationEnabled) {
[self setMyLocationEnabled:[myLocationEnabled boolValue]];
}
NSNumber *myLocationButtonEnabled = data[@"myLocationButtonEnabled"];
if (myLocationButtonEnabled && myLocationButtonEnabled != (id)[NSNull null]) {
NSNumber *myLocationButtonEnabled = FGMGetValueOrNilFromDict(data, @"myLocationButtonEnabled");
if (myLocationButtonEnabled) {
[self setMyLocationButtonEnabled:[myLocationButtonEnabled boolValue]];
}
NSString *style = data[@"style"];
NSString *style = FGMGetValueOrNilFromDict(data, @"style");
if (style) {
self.styleError = [self setMapStyle:style];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,60 +93,60 @@ - (void)setZIndex:(int)zIndex {
- (void)interpretMarkerOptions:(NSDictionary *)data
registrar:(NSObject<FlutterPluginRegistrar> *)registrar
screenScale:(CGFloat)screenScale {
NSNumber *alpha = data[@"alpha"];
if (alpha && alpha != (id)[NSNull null]) {
NSNumber *alpha = FGMGetValueOrNilFromDict(data, @"alpha");
if (alpha) {
[self setAlpha:[alpha floatValue]];
}
NSArray *anchor = data[@"anchor"];
if (anchor && anchor != (id)[NSNull null]) {
NSArray *anchor = FGMGetValueOrNilFromDict(data, @"anchor");
if (anchor) {
[self setAnchor:[FLTGoogleMapJSONConversions pointFromArray:anchor]];
}
NSNumber *draggable = data[@"draggable"];
if (draggable && draggable != (id)[NSNull null]) {
NSNumber *draggable = FGMGetValueOrNilFromDict(data, @"draggable");
if (draggable) {
[self setDraggable:[draggable boolValue]];
}
NSArray *icon = data[@"icon"];
if (icon && icon != (id)[NSNull null]) {
NSArray *icon = FGMGetValueOrNilFromDict(data, @"icon");
if (icon) {
UIImage *image = [self extractIconFromData:icon registrar:registrar screenScale:screenScale];
[self setIcon:image];
}
NSNumber *flat = data[@"flat"];
if (flat && flat != (id)[NSNull null]) {
NSNumber *flat = FGMGetValueOrNilFromDict(data, @"flat");
if (flat) {
[self setFlat:[flat boolValue]];
}
NSNumber *consumeTapEvents = data[@"consumeTapEvents"];
if (consumeTapEvents && consumeTapEvents != (id)[NSNull null]) {
NSNumber *consumeTapEvents = FGMGetValueOrNilFromDict(data, @"consumeTapEvents");
if (consumeTapEvents) {
[self setConsumeTapEvents:[consumeTapEvents boolValue]];
}
[self interpretInfoWindow:data];
NSArray *position = data[@"position"];
if (position && position != (id)[NSNull null]) {
NSArray *position = FGMGetValueOrNilFromDict(data, @"position");
if (position) {
[self setPosition:[FLTGoogleMapJSONConversions locationFromLatLong:position]];
}
NSNumber *rotation = data[@"rotation"];
if (rotation && rotation != (id)[NSNull null]) {
NSNumber *rotation = FGMGetValueOrNilFromDict(data, @"rotation");
if (rotation) {
[self setRotation:[rotation doubleValue]];
}
NSNumber *visible = data[@"visible"];
if (visible && visible != (id)[NSNull null]) {
NSNumber *visible = FGMGetValueOrNilFromDict(data, @"visible");
if (visible) {
[self setVisible:[visible boolValue]];
}
NSNumber *zIndex = data[@"zIndex"];
if (zIndex && zIndex != (id)[NSNull null]) {
NSNumber *zIndex = FGMGetValueOrNilFromDict(data, @"zIndex");
if (zIndex) {
[self setZIndex:[zIndex intValue]];
}
}

- (void)interpretInfoWindow:(NSDictionary *)data {
NSDictionary *infoWindow = data[@"infoWindow"];
if (infoWindow && infoWindow != (id)[NSNull null]) {
NSString *title = infoWindow[@"title"];
NSString *snippet = infoWindow[@"snippet"];
if (title && title != (id)[NSNull null]) {
NSDictionary *infoWindow = FGMGetValueOrNilFromDict(data, @"infoWindow");
if (infoWindow) {
NSString *title = FGMGetValueOrNilFromDict(infoWindow, @"title");
NSString *snippet = FGMGetValueOrNilFromDict(infoWindow, @"snippet");
if (title) {
[self setInfoWindowTitle:title snippet:snippet];
}
NSArray *infoWindowAnchor = infoWindow[@"infoWindowAnchor"];
if (infoWindowAnchor && infoWindowAnchor != (id)[NSNull null]) {
if (infoWindowAnchor) {
[self setInfoWindowAnchor:[FLTGoogleMapJSONConversions pointFromArray:infoWindowAnchor]];
}
}
Expand Down Expand Up @@ -221,15 +221,16 @@ - (UIImage *)extractIconFromData:(NSArray *)iconData
@throw exception;
}

NSString *assetName = assetData[@"assetName"];
NSString *scalingMode = assetData[@"bitmapScaling"];
NSString *assetName = FGMGetValueOrNilFromDict(assetData, @"assetName");
NSString *scalingMode = FGMGetValueOrNilFromDict(assetData, @"bitmapScaling");

image = [UIImage imageNamed:[registrar lookupKeyForAsset:assetName]];

if ([scalingMode isEqualToString:@"auto"]) {
NSNumber *width = assetData[@"width"];
NSNumber *height = assetData[@"height"];
CGFloat imagePixelRatio = [assetData[@"imagePixelRatio"] doubleValue];
NSNumber *width = FGMGetValueOrNilFromDict(assetData, @"width");
NSNumber *height = FGMGetValueOrNilFromDict(assetData, @"height");
CGFloat imagePixelRatio =
[FGMGetValueOrNilFromDict(assetData, @"imagePixelRatio") doubleValue];

if (width || height) {
image = [FLTGoogleMapMarkerController scaledImage:image withScale:screenScale];
Expand All @@ -252,15 +253,16 @@ - (UIImage *)extractIconFromData:(NSArray *)iconData
@throw exception;
}

FlutterStandardTypedData *bytes = byteData[@"byteData"];
NSString *scalingMode = byteData[@"bitmapScaling"];
FlutterStandardTypedData *bytes = FGMGetValueOrNilFromDict(byteData, @"byteData");
NSString *scalingMode = FGMGetValueOrNilFromDict(byteData, @"bitmapScaling");

@try {
image = [UIImage imageWithData:[bytes data] scale:screenScale];
if ([scalingMode isEqualToString:@"auto"]) {
NSNumber *width = byteData[@"width"];
NSNumber *height = byteData[@"height"];
CGFloat imagePixelRatio = [byteData[@"imagePixelRatio"] doubleValue];
NSNumber *width = FGMGetValueOrNilFromDict(byteData, @"width");
NSNumber *height = FGMGetValueOrNilFromDict(byteData, @"height");
CGFloat imagePixelRatio =
[FGMGetValueOrNilFromDict(byteData, @"imagePixelRatio") doubleValue];

if (width || height) {
// Before scaling the image, image must be in screenScale
Expand Down
Loading