Skip to content

Commit

Permalink
[google_maps_flutter_android] Convert JointType to an enum (#7558)
Browse files Browse the repository at this point in the history
Replace the raw integers used in `PlatformPolyline` to an enum.

flutter/flutter#153718 (comment)

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] page, which explains my
responsibilities.
- [x] I read and followed the [relevant style guides] and ran the
auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages
repo does use `dart format`.)
- [ ] I signed the [CLA].
- [x] The title of the PR starts with the name of the package surrounded
by square brackets, e.g. `[shared_preferences]`
- [x] I [linked to at least one issue that this PR fixes] in the
description above.
- [ ] I updated `pubspec.yaml` with an appropriate new version according
to the [pub versioning philosophy], or this PR is [exempt from version
changes].
- [x] I updated `CHANGELOG.md` to add a description of the change,
[following repository CHANGELOG style], or this PR is [exempt from
CHANGELOG changes].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[CLA]: https://cla.developers.google.com/
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[pub versioning philosophy]: https://dart.dev/tools/pub/versioning
[exempt from version changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version
[following repository CHANGELOG style]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[exempt from CHANGELOG changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog
[test-exempt]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
  • Loading branch information
yaakovschectman authored Sep 4, 2024
1 parent f408578 commit e93995a
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.14.5

* Converts `JointType` to enum.

## 2.14.4

* Converts 'PlatformTileOverlay' to pigeon.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.android.gms.maps.model.Dash;
import com.google.android.gms.maps.model.Dot;
import com.google.android.gms.maps.model.Gap;
import com.google.android.gms.maps.model.JointType;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.PatternItem;
Expand Down Expand Up @@ -703,6 +704,18 @@ static String interpretPolygonOptions(Messages.PlatformPolygon polygon, PolygonO
return polygon.getPolygonId();
}

static int jointTypeFromPigeon(Messages.PlatformJointType jointType) {
switch (jointType) {
case MITERED:
return JointType.DEFAULT;
case BEVEL:
return JointType.BEVEL;
case ROUND:
return JointType.ROUND;
}
return JointType.DEFAULT;
}

static String interpretPolylineOptions(
Messages.PlatformPolyline polyline,
PolylineOptionsSink sink,
Expand All @@ -713,7 +726,7 @@ static String interpretPolylineOptions(
sink.setEndCap(toCap(polyline.getEndCap(), assetManager, density));
sink.setStartCap(toCap(polyline.getStartCap(), assetManager, density));
sink.setGeodesic(polyline.getGeodesic());
sink.setJointType(polyline.getJointType().intValue());
sink.setJointType(jointTypeFromPigeon(polyline.getJointType()));
sink.setVisible(polyline.getVisible());
sink.setWidth(polyline.getWidth());
sink.setZIndex(polyline.getZIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ private PlatformRendererType(final int index) {
}
}

public enum PlatformJointType {
MITERED(0),
BEVEL(1),
ROUND(2);

final int index;

private PlatformJointType(final int index) {
this.index = index;
}
}

/**
* Pigeon representatation of a CameraPosition.
*
Expand Down Expand Up @@ -1760,19 +1772,29 @@ public void setGeodesic(@NonNull Boolean setterArg) {
this.geodesic = setterArg;
}

private @NonNull Long jointType;
/**
* The joint type as an integer. This must be a value corresponding to one of the values defined
* in the platform interface package's JointType enum. The integer values specified in this enum
* must match those used by the native SDK.
*/
private @NonNull PlatformJointType jointType;

public @NonNull Long getJointType() {
public @NonNull PlatformJointType getJointType() {
return jointType;
}

public void setJointType(@NonNull Long setterArg) {
public void setJointType(@NonNull PlatformJointType setterArg) {
if (setterArg == null) {
throw new IllegalStateException("Nonnull field \"jointType\" is null.");
}
this.jointType = setterArg;
}

/**
* The pattern data, as JSON. Each element in this list should be set only from
* PatternItem.toJson, and the native code must interpret it according to the internal
* implementation details of that method.
*/
private @NonNull List<Object> patterns;

public @NonNull List<Object> getPatterns() {
Expand All @@ -1799,6 +1821,10 @@ public void setPoints(@NonNull List<PlatformLatLng> setterArg) {
this.points = setterArg;
}

/**
* The start and end cap data, as JSON. These should be set only from Cap.toJson, and the native
* code must interpret it according to the internal implementation details of that method.
*/
private @NonNull Object startCap;

public @NonNull Object getStartCap() {
Expand Down Expand Up @@ -1941,10 +1967,10 @@ public static final class Builder {
return this;
}

private @Nullable Long jointType;
private @Nullable PlatformJointType jointType;

@CanIgnoreReturnValue
public @NonNull Builder setJointType(@NonNull Long setterArg) {
public @NonNull Builder setJointType(@NonNull PlatformJointType setterArg) {
this.jointType = setterArg;
return this;
}
Expand Down Expand Up @@ -2053,10 +2079,7 @@ ArrayList<Object> toList() {
Object geodesic = __pigeon_list.get(3);
pigeonResult.setGeodesic((Boolean) geodesic);
Object jointType = __pigeon_list.get(4);
pigeonResult.setJointType(
(jointType == null)
? null
: ((jointType instanceof Integer) ? (Integer) jointType : (Long) jointType));
pigeonResult.setJointType((PlatformJointType) jointType);
Object patterns = __pigeon_list.get(5);
pigeonResult.setPatterns((List<Object>) patterns);
Object points = __pigeon_list.get(6);
Expand Down Expand Up @@ -4177,6 +4200,11 @@ protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
Object value = readValue(buffer);
return value == null ? null : PlatformRendererType.values()[(int) value];
}
case (byte) 153:
{
Object value = readValue(buffer);
return value == null ? null : PlatformJointType.values()[(int) value];
}
default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -4256,6 +4284,9 @@ protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
} else if (value instanceof PlatformRendererType) {
stream.write(152);
writeValue(stream, value == null ? null : ((PlatformRendererType) value).index);
} else if (value instanceof PlatformJointType) {
stream.write(153);
writeValue(stream, value == null ? null : ((PlatformJointType) value).index);
} else {
super.writeValue(stream, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ class GoogleMapsFlutterAndroid extends GoogleMapsFlutterPlatform {
width: polyline.width,
zIndex: polyline.zIndex,
points: points,
jointType: polyline.jointType.value,
jointType: platformJointTypeFromJointType(polyline.jointType),
patterns: pattern,
);
}
Expand Down Expand Up @@ -1146,6 +1146,26 @@ PlatformZoomRange? _platformZoomRangeFromMinMaxZoomPreferenceJson(
return PlatformZoomRange(min: minMaxZoom[0], max: minMaxZoom[1]);
}

/// Converts platform interface's JointType to Pigeon's PlatformJointType.
@visibleForTesting
PlatformJointType platformJointTypeFromJointType(JointType jointType) {
switch (jointType) {
case JointType.mitered:
return PlatformJointType.mitered;
case JointType.bevel:
return PlatformJointType.bevel;
case JointType.round:
return PlatformJointType.round;
}
// The enum comes from a different package, which could get a new value at
// any time, so provide a fallback that ensures this won't break when used
// with a version that contains new values. This is deliberately outside
// the switch rather than a `default` so that the linter will flag the
// switch as needing an update.
// ignore: dead_code
return PlatformJointType.mitered;
}

/// Update specification for a set of [TileOverlay]s.
// TODO(stuartmorgan): Fix the missing export of this class in the platform
// interface, and remove this copy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ enum PlatformRendererType {
latest,
}

enum PlatformJointType {
mitered,
bevel,
round,
}

/// Pigeon representatation of a CameraPosition.
class PlatformCameraPosition {
PlatformCameraPosition({
Expand Down Expand Up @@ -448,12 +454,15 @@ class PlatformPolyline {

bool geodesic;

int jointType;
/// The joint type as an integer. This must be a value corresponding to one of the values defined in the platform interface package's JointType enum. The integer values specified in this enum must match those used by the native SDK.
PlatformJointType jointType;

/// The pattern data, as JSON. Each element in this list should be set only from PatternItem.toJson, and the native code must interpret it according to the internal implementation details of that method.
List<Object?> patterns;

List<PlatformLatLng?> points;

/// The start and end cap data, as JSON. These should be set only from Cap.toJson, and the native code must interpret it according to the internal implementation details of that method.
Object startCap;

Object endCap;
Expand Down Expand Up @@ -488,7 +497,7 @@ class PlatformPolyline {
consumesTapEvents: result[1]! as bool,
color: result[2]! as int,
geodesic: result[3]! as bool,
jointType: result[4]! as int,
jointType: result[4]! as PlatformJointType,
patterns: (result[5] as List<Object?>?)!.cast<Object?>(),
points: (result[6] as List<Object?>?)!.cast<PlatformLatLng?>(),
startCap: result[7]!,
Expand Down Expand Up @@ -1081,6 +1090,9 @@ class _PigeonCodec extends StandardMessageCodec {
} else if (value is PlatformRendererType) {
buffer.putUint8(152);
writeValue(buffer, value.index);
} else if (value is PlatformJointType) {
buffer.putUint8(153);
writeValue(buffer, value.index);
} else {
super.writeValue(buffer, value);
}
Expand Down Expand Up @@ -1139,6 +1151,9 @@ class _PigeonCodec extends StandardMessageCodec {
case 152:
final int? value = readValue(buffer) as int?;
return value == null ? null : PlatformRendererType.values[value];
case 153:
final int? value = readValue(buffer) as int?;
return value == null ? null : PlatformJointType.values[value];
default:
return super.readValueOfType(type, buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class PlatformPolygon {
final int zIndex;
}

/// Join types for polyline joints.
enum PlatformJointType {
mitered,
bevel,
round,
}

/// Pigeon equivalent of the Polyline class.
class PlatformPolyline {
PlatformPolyline({
Expand All @@ -203,10 +210,8 @@ class PlatformPolyline {
final int color;
final bool geodesic;

/// The joint type as an integer. This must be a value corresponding to one of the values defined in the platform interface package's JointType enum. The integer values specified in this enum must match those used by the native SDK.
// TODO(schectman): Convert field to enum.
// https://github.com/flutter/flutter/issues/153718
final int jointType;
/// The joint type.
final PlatformJointType jointType;

/// The pattern data, as JSON. Each element in this list should be set only from PatternItem.toJson, and the native code must interpret it according to the internal implementation details of that method.
// TODO(schectman): Convert field to structured data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_maps_flutter_android
description: Android implementation of the google_maps_flutter plugin.
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
version: 2.14.4
version: 2.14.5

environment:
sdk: ^3.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ void main() {
expected.consumeTapEvents,
expected.color.value,
expected.geodesic,
expected.jointType.value,
platformJointTypeFromJointType(expected.jointType),
]);
expect(encoded.sublist(9), <Object?>[
expected.visible,
Expand Down

0 comments on commit e93995a

Please sign in to comment.