Skip to content

Commit

Permalink
[google_maps_flutter_android] Convert PlatformCameraUpdate to pigeo…
Browse files Browse the repository at this point in the history
…n. (#7507)

Because this also converts the platform interface Camera type, this will
need to be a federated change, unless we come up with a better idea of
how to convert that class.

flutter/flutter#152928

## 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.
- [x] 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 `///`).
- [x] 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 12, 2024
1 parent 91caa7a commit 11b339e
Show file tree
Hide file tree
Showing 10 changed files with 2,362 additions and 1,239 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.14.6

* Converts 'PlatformCameraUpdate' to pigeon.

## 2.14.5

* Converts `JointType` to enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -316,10 +315,6 @@ public static BitmapDescriptor getBitmapFromAsset(
return bitmapDescriptorFactory.fromAsset(assetKey);
}

private static boolean toBoolean(Object o) {
return (Boolean) o;
}

static @NonNull CameraPosition cameraPositionFromPigeon(
@NonNull Messages.PlatformCameraPosition position) {
final CameraPosition.Builder builder = CameraPosition.builder();
Expand All @@ -330,47 +325,57 @@ private static boolean toBoolean(Object o) {
return builder.build();
}

static CameraPosition toCameraPosition(Object o) {
final Map<?, ?> data = toMap(o);
final CameraPosition.Builder builder = CameraPosition.builder();
builder.bearing(toFloat(data.get("bearing")));
builder.target(toLatLng(data.get("target")));
builder.tilt(toFloat(data.get("tilt")));
builder.zoom(toFloat(data.get("zoom")));
return builder.build();
}

static CameraUpdate toCameraUpdate(Object o, float density) {
final List<?> data = toList(o);
switch (toString(data.get(0))) {
case "newCameraPosition":
return CameraUpdateFactory.newCameraPosition(toCameraPosition(data.get(1)));
case "newLatLng":
return CameraUpdateFactory.newLatLng(toLatLng(data.get(1)));
case "newLatLngBounds":
return CameraUpdateFactory.newLatLngBounds(
toLatLngBounds(data.get(1)), toPixels(data.get(2), density));
case "newLatLngZoom":
return CameraUpdateFactory.newLatLngZoom(toLatLng(data.get(1)), toFloat(data.get(2)));
case "scrollBy":
return CameraUpdateFactory.scrollBy( //
toFractionalPixels(data.get(1), density), //
toFractionalPixels(data.get(2), density));
case "zoomBy":
if (data.size() == 2) {
return CameraUpdateFactory.zoomBy(toFloat(data.get(1)));
} else {
return CameraUpdateFactory.zoomBy(toFloat(data.get(1)), toPoint(data.get(2), density));
}
case "zoomIn":
return CameraUpdateFactory.zoomIn();
case "zoomOut":
return CameraUpdateFactory.zoomOut();
case "zoomTo":
return CameraUpdateFactory.zoomTo(toFloat(data.get(1)));
default:
throw new IllegalArgumentException("Cannot interpret " + o + " as CameraUpdate");
}
static CameraUpdate cameraUpdateFromPigeon(Messages.PlatformCameraUpdate update, float density) {
Object cameraUpdate = update.getCameraUpdate();
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewCameraPosition) {
Messages.PlatformCameraUpdateNewCameraPosition newCameraPosition =
(Messages.PlatformCameraUpdateNewCameraPosition) cameraUpdate;
return CameraUpdateFactory.newCameraPosition(
cameraPositionFromPigeon(newCameraPosition.getCameraPosition()));
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLng) {
Messages.PlatformCameraUpdateNewLatLng newLatLng =
(Messages.PlatformCameraUpdateNewLatLng) cameraUpdate;
return CameraUpdateFactory.newLatLng(latLngFromPigeon(newLatLng.getLatLng()));
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLngZoom) {
Messages.PlatformCameraUpdateNewLatLngZoom newLatLngZoom =
(Messages.PlatformCameraUpdateNewLatLngZoom) cameraUpdate;
return CameraUpdateFactory.newLatLngZoom(
latLngFromPigeon(newLatLngZoom.getLatLng()), newLatLngZoom.getZoom().floatValue());
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLngBounds) {
Messages.PlatformCameraUpdateNewLatLngBounds newLatLngBounds =
(Messages.PlatformCameraUpdateNewLatLngBounds) cameraUpdate;
return CameraUpdateFactory.newLatLngBounds(
latLngBoundsFromPigeon(newLatLngBounds.getBounds()),
(int) (newLatLngBounds.getPadding() * density));
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateScrollBy) {
Messages.PlatformCameraUpdateScrollBy scrollBy =
(Messages.PlatformCameraUpdateScrollBy) cameraUpdate;
return CameraUpdateFactory.scrollBy(
scrollBy.getDx().floatValue() * density, scrollBy.getDy().floatValue() * density);
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoomBy) {
Messages.PlatformCameraUpdateZoomBy zoomBy =
(Messages.PlatformCameraUpdateZoomBy) cameraUpdate;
final Point focus = pointFromPigeon(zoomBy.getFocus(), density);
return (focus != null)
? CameraUpdateFactory.zoomBy(zoomBy.getAmount().floatValue(), focus)
: CameraUpdateFactory.zoomBy(zoomBy.getAmount().floatValue());
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoomTo) {
Messages.PlatformCameraUpdateZoomTo zoomTo =
(Messages.PlatformCameraUpdateZoomTo) cameraUpdate;
return CameraUpdateFactory.zoomTo(zoomTo.getZoom().floatValue());
}
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoom) {
Messages.PlatformCameraUpdateZoom zoom = (Messages.PlatformCameraUpdateZoom) cameraUpdate;
return (zoom.getOut()) ? CameraUpdateFactory.zoomOut() : CameraUpdateFactory.zoomIn();
}
throw new IllegalArgumentException(
"PlatformCameraUpdate's cameraUpdate field must be one of the PlatformCameraUpdate... case classes.");
}

private static double toDouble(Object o) {
Expand Down Expand Up @@ -494,16 +499,16 @@ static Point pointFromPigeon(Messages.PlatformPoint point) {
return new Point(point.getX().intValue(), point.getY().intValue());
}

static Messages.PlatformPoint pointToPigeon(Point point) {
return new Messages.PlatformPoint.Builder().setX((long) point.x).setY((long) point.y).build();
}

private static LatLngBounds toLatLngBounds(Object o) {
if (o == null) {
@Nullable
static Point pointFromPigeon(@Nullable Messages.PlatformOffset point, float density) {
if (point == null) {
return null;
}
final List<?> data = toList(o);
return new LatLngBounds(toLatLng(data.get(0)), toLatLng(data.get(1)));
return new Point((int) (point.getDx() * density), (int) (point.getDy() * density));
}

static Messages.PlatformPoint pointToPigeon(Point point) {
return new Messages.PlatformPoint.Builder().setX((long) point.x).setY((long) point.y).build();
}

private static List<?> toList(Object o) {
Expand All @@ -514,26 +519,6 @@ private static List<?> toList(Object o) {
return (Map<?, ?>) o;
}

private static Map<String, Object> toObjectMap(Object o) {
Map<String, Object> hashMap = new HashMap<>();
Map<?, ?> map = (Map<?, ?>) o;
for (Object key : map.keySet()) {
Object object = map.get(key);
if (object != null) {
hashMap.put((String) key, object);
}
}
return hashMap;
}

private static float toFractionalPixels(Object o, float density) {
return toFloat(o) * density;
}

private static int toPixels(Object o, float density) {
return (int) toFractionalPixels(o, density);
}

private static Bitmap toBitmap(Object o) {
byte[] bmpData = (byte[]) o;
Bitmap bitmap = BitmapFactory.decodeByteArray(bmpData, 0, bmpData.length);
Expand Down Expand Up @@ -563,11 +548,6 @@ private static Bitmap toScaledBitmap(Bitmap bitmap, int width, int height) {
return bitmap;
}

private static Point toPoint(Object o, float density) {
final List<?> data = toList(o);
return new Point(toPixels(data.get(0), density), toPixels(data.get(1), density));
}

private static String toString(Object o) {
return (String) o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ public void moveCamera(@NonNull Messages.PlatformCameraUpdate cameraUpdate) {
throw new FlutterError(
"GoogleMap uninitialized", "moveCamera called prior to map initialization", null);
}
googleMap.moveCamera(Convert.toCameraUpdate(cameraUpdate.getJson(), density));
googleMap.moveCamera(Convert.cameraUpdateFromPigeon(cameraUpdate, density));
}

@Override
Expand All @@ -942,7 +942,7 @@ public void animateCamera(@NonNull Messages.PlatformCameraUpdate cameraUpdate) {
throw new FlutterError(
"GoogleMap uninitialized", "animateCamera called prior to map initialization", null);
}
googleMap.animateCamera(Convert.toCameraUpdate(cameraUpdate.getJson(), density));
googleMap.animateCamera(Convert.cameraUpdateFromPigeon(cameraUpdate, density));
}

@Override
Expand Down
Loading

0 comments on commit 11b339e

Please sign in to comment.