Skip to content

Commit e4ade63

Browse files
[google_maps_flutter] Adopt Pigeon's Kotlin 'suspend' support (#12980)
Converts the Pigeon generation from the older callback system to the new Kotlin `suspend` support. While this is not any simpler while the code is Java, it has several benefits: - It makes future `pigeon` mass updates much simpler, since this is the new default generation mode. - It simplifies adoption of the JNI-based Pigeon backend, which always uses the `suspend` style. - It will simplify the tests when converting them to Kotlin. Part of flutter/flutter#192417 ## 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 bcc6dcf commit e4ade63

18 files changed

Lines changed: 397 additions & 364 deletions

‎packages/google_maps_flutter/google_maps_flutter_android/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.21.0
2+
3+
* Switches to Pigeon's new 'suspend' support for platform communication.
4+
15
## 2.20.0
26

37
* Adds support for tapping points of interest on the map.

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/CirclesController.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15-
import kotlin.Result;
16-
import kotlin.Unit;
1715

1816
class CirclesController {
1917
@VisibleForTesting final Map<String, CircleController> circleIdToController;
@@ -60,7 +58,7 @@ boolean onCircleTap(String googleCircleId) {
6058
if (circleId == null) {
6159
return false;
6260
}
63-
flutterApi.onCircleTap(circleId, (Result<Unit> result) -> Unit.INSTANCE);
61+
flutterApi.onCircleTap(circleId, ResultUtilsKt.emptyContinuation());
6462
CircleController circleController = circleIdToController.get(circleId);
6563
if (circleController != null) {
6664
return circleController.consumeTapEvents();

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/ClusterManagersController.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Set;
26-
import kotlin.Result;
27-
import kotlin.Unit;
2826

2927
/**
3028
* Controls cluster managers and exposes interfaces for adding and removing cluster items for
@@ -238,8 +236,7 @@ public boolean onClusterClick(Cluster<MarkerBuilder> cluster) {
238236
MarkerBuilder[] builders = cluster.getItems().toArray(new MarkerBuilder[0]);
239237
String clusterManagerId = builders[0].clusterManagerId();
240238
flutterApi.onClusterTap(
241-
Convert.clusterToPigeon(clusterManagerId, cluster),
242-
(Result<Unit> result) -> Unit.INSTANCE);
239+
Convert.clusterToPigeon(clusterManagerId, cluster), ResultUtilsKt.emptyContinuation());
243240
}
244241

245242
// Return false to allow the default behavior of the cluster click event to occur.

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java‎

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@
5353
import java.util.List;
5454
import java.util.Objects;
5555
import java.util.Set;
56-
import kotlin.Result;
5756
import kotlin.Unit;
58-
import kotlin.jvm.functions.Function1;
59-
import org.jetbrains.annotations.NotNull;
57+
import kotlin.coroutines.Continuation;
6058

6159
/** Controller of a single GoogleMaps MapView instance. */
6260
class GoogleMapController
@@ -88,8 +86,7 @@ class GoogleMapController
8886
private boolean buildingsEnabled = true;
8987
private boolean disposed = false;
9088
@VisibleForTesting final float density;
91-
private @Nullable Function1<? super @NotNull Result<@NotNull Unit>, @NotNull Unit>
92-
mapReadyCallback;
89+
private @Nullable Continuation<? super Unit> mapReadyContinuation;
9390
private final Context context;
9491
private final LifecycleProvider lifecycleProvider;
9592
private final MarkersController markersController;
@@ -207,9 +204,9 @@ public void onMapReady(@NonNull GoogleMap googleMap) {
207204
this.googleMap.setTrafficEnabled(this.trafficEnabled);
208205
this.googleMap.setBuildingsEnabled(this.buildingsEnabled);
209206
installInvalidator();
210-
if (mapReadyCallback != null) {
211-
ResultUtilsKt.completeWithUnitSuccess(mapReadyCallback);
212-
mapReadyCallback = null;
207+
if (mapReadyContinuation != null) {
208+
ResultUtilsKt.resumeWithUnitSuccess(mapReadyContinuation);
209+
mapReadyContinuation = null;
213210
}
214211
setGoogleMapListener(this);
215212
markerManager = new MarkerManager(googleMap);
@@ -317,17 +314,17 @@ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
317314

318315
@Override
319316
public void onMapClick(@NonNull LatLng latLng) {
320-
flutterApi.onTap(Convert.latLngToPigeon(latLng), (Result<Unit> result) -> Unit.INSTANCE);
317+
flutterApi.onTap(Convert.latLngToPigeon(latLng), ResultUtilsKt.emptyContinuation());
321318
}
322319

323320
@Override
324321
public void onMapLongClick(@NonNull LatLng latLng) {
325-
flutterApi.onLongPress(Convert.latLngToPigeon(latLng), (Result<Unit> result) -> Unit.INSTANCE);
322+
flutterApi.onLongPress(Convert.latLngToPigeon(latLng), ResultUtilsKt.emptyContinuation());
326323
}
327324

328325
@Override
329326
public void onCameraMoveStarted(int reason) {
330-
flutterApi.onCameraMoveStarted((Result<Unit> result) -> Unit.INSTANCE);
327+
flutterApi.onCameraMoveStarted(ResultUtilsKt.emptyContinuation());
331328
}
332329

333330
@Override
@@ -342,13 +339,13 @@ public void onCameraMove() {
342339
}
343340
flutterApi.onCameraMove(
344341
Convert.cameraPositionToPigeon(googleMap.getCameraPosition()),
345-
(Result<Unit> result) -> Unit.INSTANCE);
342+
ResultUtilsKt.emptyContinuation());
346343
}
347344

348345
@Override
349346
public void onCameraIdle() {
350347
clusterManagersController.onCameraIdle();
351-
flutterApi.onCameraIdle((Result<Unit> result) -> Unit.INSTANCE);
348+
flutterApi.onCameraIdle(ResultUtilsKt.emptyContinuation());
352349
}
353350

354351
@Override
@@ -389,8 +386,7 @@ public void onCircleClick(Circle circle) {
389386
@Override
390387
public void onPoiClick(PointOfInterest pointOfInterest) {
391388
if (pointOfInterest.placeId != null) {
392-
flutterApi.onPointOfInterestTap(
393-
pointOfInterest.placeId, (Result<Unit> result) -> Unit.INSTANCE);
389+
flutterApi.onPointOfInterestTap(pointOfInterest.placeId, ResultUtilsKt.emptyContinuation());
394390
}
395391
}
396392

@@ -872,13 +868,14 @@ private boolean updateMapStyle(String style) {
872868
}
873869

874870
/** MapsApi implementation */
871+
@Nullable
875872
@Override
876-
public void waitForMap(
877-
@NonNull Function1<? super @NotNull Result<@NotNull Unit>, @NotNull Unit> callback) {
873+
public Object waitForMap(@NonNull Continuation<? super Unit> continuation) {
878874
if (googleMap == null) {
879-
mapReadyCallback = callback;
875+
mapReadyContinuation = continuation;
876+
return ResultUtilsKt.coroutineSuspended();
880877
} else {
881-
ResultUtilsKt.completeWithUnitSuccess(callback);
878+
return Unit.INSTANCE;
882879
}
883880
}
884881

@@ -1073,27 +1070,27 @@ public void clearTileCache(@NonNull String tileOverlayId) {
10731070
tileOverlaysController.clearTileCache(tileOverlayId);
10741071
}
10751072

1073+
@Nullable
10761074
@Override
1077-
public void takeSnapshot(
1078-
@NonNull Function1<? super @NotNull Result<byte @NotNull []>, Unit> callback) {
1075+
public Object takeSnapshot(@NonNull Continuation<? super byte[]> continuation) {
10791076
if (googleMap == null) {
1080-
ResultUtilsKt.completeWithError(
1081-
callback, new FlutterError("GoogleMap uninitialized", "takeSnapshot", null));
1082-
} else {
1083-
googleMap.snapshot(
1084-
bitmap -> {
1085-
if (bitmap == null) {
1086-
ResultUtilsKt.completeWithError(
1087-
callback, new FlutterError("Snapshot failure", "Unable to take snapshot", null));
1088-
} else {
1089-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
1090-
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
1091-
byte[] byteArray = stream.toByteArray();
1092-
bitmap.recycle();
1093-
ResultUtilsKt.completeWithValue(callback, byteArray);
1094-
}
1095-
});
1096-
}
1077+
throw new FlutterError("GoogleMap uninitialized", "takeSnapshot", null);
1078+
}
1079+
googleMap.snapshot(
1080+
bitmap -> {
1081+
if (bitmap == null) {
1082+
ResultUtilsKt.resumeWithException(
1083+
continuation,
1084+
new FlutterError("Snapshot failure", "Unable to take snapshot", null));
1085+
} else {
1086+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
1087+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
1088+
byte[] byteArray = stream.toByteArray();
1089+
bitmap.recycle();
1090+
ResultUtilsKt.resumeWithValue(continuation, byteArray);
1091+
}
1092+
});
1093+
return ResultUtilsKt.coroutineSuspended();
10971094
}
10981095

10991096
/** MapsInspectorApi implementation */

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapInitializer.java‎

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
import com.google.android.gms.maps.MapsInitializer;
1414
import com.google.android.gms.maps.OnMapsSdkInitializedCallback;
1515
import io.flutter.plugin.common.BinaryMessenger;
16-
import kotlin.Result;
17-
import kotlin.Unit;
18-
import kotlin.jvm.functions.Function1;
19-
import org.jetbrains.annotations.NotNull;
16+
import kotlin.coroutines.Continuation;
2017

2118
/** GoogleMaps initializer used to initialize the Google Maps SDK with preferred settings. */
2219
final class GoogleMapInitializer implements OnMapsSdkInitializedCallback, MapsInitializerApi {
2320
private static final String TAG = "GoogleMapInitializer";
2421
private final Context context;
25-
private static @Nullable Function1<
26-
? super @NotNull Result<? extends @NotNull PlatformRendererType>, @NotNull Unit>
27-
initializationCallback;
22+
private static @Nullable Continuation<? super PlatformRendererType> initializationContinuation;
2823
private boolean rendererInitialized = false;
2924

3025
GoogleMapInitializer(Context context, BinaryMessenger binaryMessenger) {
@@ -33,22 +28,18 @@ final class GoogleMapInitializer implements OnMapsSdkInitializedCallback, MapsIn
3328
MapsInitializerApi.Companion.setUp(binaryMessenger, this);
3429
}
3530

31+
@Nullable
3632
@Override
37-
public void initializeWithPreferredRenderer(
33+
public Object initializeWithPreferredRenderer(
3834
@Nullable PlatformRendererType type,
39-
@NonNull
40-
Function1<? super @NotNull Result<? extends @NotNull PlatformRendererType>, @NotNull Unit>
41-
callback) {
42-
if (rendererInitialized || initializationCallback != null) {
43-
ResultUtilsKt.completeWithError(
44-
callback,
45-
new FlutterError(
46-
"Renderer already initialized",
47-
"Renderer initialization called multiple times",
48-
null));
35+
@NonNull Continuation<? super PlatformRendererType> continuation) {
36+
if (rendererInitialized || initializationContinuation != null) {
37+
throw new FlutterError(
38+
"Renderer already initialized", "Renderer initialization called multiple times", null);
4939
} else {
50-
initializationCallback = callback;
40+
initializationContinuation = continuation;
5141
initializeWithRendererRequest(Convert.toMapRendererType(type));
42+
return ResultUtilsKt.coroutineSuspended();
5243
}
5344
}
5445

@@ -85,23 +76,23 @@ public void initializeWithRendererRequest(@Nullable MapsInitializer.Renderer ren
8576
@Override
8677
public void onMapsSdkInitialized(@NonNull MapsInitializer.Renderer renderer) {
8778
rendererInitialized = true;
88-
if (initializationCallback != null) {
79+
if (initializationContinuation != null) {
8980
switch (renderer) {
9081
case LATEST:
91-
ResultUtilsKt.completeWithValue(initializationCallback, PlatformRendererType.LATEST);
82+
ResultUtilsKt.resumeWithValue(initializationContinuation, PlatformRendererType.LATEST);
9283
break;
9384
case LEGACY:
94-
ResultUtilsKt.completeWithValue(initializationCallback, PlatformRendererType.LEGACY);
85+
ResultUtilsKt.resumeWithValue(initializationContinuation, PlatformRendererType.LEGACY);
9586
break;
9687
default:
97-
ResultUtilsKt.completeWithError(
98-
initializationCallback,
88+
ResultUtilsKt.resumeWithException(
89+
initializationContinuation,
9990
new FlutterError(
10091
"Unknown renderer type",
10192
"Initialized with unknown renderer type",
10293
renderer.name()));
10394
}
104-
initializationCallback = null;
95+
initializationContinuation = null;
10596
}
10697
}
10798
}

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/GroundOverlaysController.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17-
import kotlin.Result;
18-
import kotlin.Unit;
1917

2018
class GroundOverlaysController {
2119
private final Map<String, GroundOverlayController> groundOverlayIdToController;
@@ -127,7 +125,7 @@ void onGroundOverlayTap(@NonNull String googleGroundOverlayId) {
127125
if (groundOverlayId == null) {
128126
return;
129127
}
130-
flutterApi.onGroundOverlayTap(groundOverlayId, (Result<Unit> result) -> Unit.INSTANCE);
128+
flutterApi.onGroundOverlayTap(groundOverlayId, ResultUtilsKt.emptyContinuation());
131129
}
132130

133131
boolean isCreatedWithBounds(@NonNull String groundOverlayId) {

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/MarkersController.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.Objects;
18-
import kotlin.Result;
19-
import kotlin.Unit;
2018

2119
class MarkersController {
2220
private final HashMap<String, MarkerBuilder> markerIdToMarkerBuilder;
@@ -267,7 +265,7 @@ boolean onMapsMarkerTap(String googleMarkerId) {
267265
}
268266

269267
boolean onMarkerTap(String markerId) {
270-
flutterApi.onMarkerTap(markerId, (Result<Unit> result) -> Unit.INSTANCE);
268+
flutterApi.onMarkerTap(markerId, ResultUtilsKt.emptyContinuation());
271269
MarkerController markerController = markerIdToController.get(markerId);
272270
if (markerController != null) {
273271
return markerController.consumeTapEvents();
@@ -281,7 +279,7 @@ void onMarkerDragStart(String googleMarkerId, LatLng latLng) {
281279
return;
282280
}
283281
flutterApi.onMarkerDragStart(
284-
markerId, Convert.latLngToPigeon(latLng), (Result<Unit> result) -> Unit.INSTANCE);
282+
markerId, Convert.latLngToPigeon(latLng), ResultUtilsKt.emptyContinuation());
285283
}
286284

287285
void onMarkerDrag(String googleMarkerId, LatLng latLng) {
@@ -290,7 +288,7 @@ void onMarkerDrag(String googleMarkerId, LatLng latLng) {
290288
return;
291289
}
292290
flutterApi.onMarkerDrag(
293-
markerId, Convert.latLngToPigeon(latLng), (Result<Unit> result) -> Unit.INSTANCE);
291+
markerId, Convert.latLngToPigeon(latLng), ResultUtilsKt.emptyContinuation());
294292
}
295293

296294
void onMarkerDragEnd(String googleMarkerId, LatLng latLng) {
@@ -299,23 +297,23 @@ void onMarkerDragEnd(String googleMarkerId, LatLng latLng) {
299297
return;
300298
}
301299
flutterApi.onMarkerDragEnd(
302-
markerId, Convert.latLngToPigeon(latLng), (Result<Unit> result) -> Unit.INSTANCE);
300+
markerId, Convert.latLngToPigeon(latLng), ResultUtilsKt.emptyContinuation());
303301
}
304302

305303
void onInfoWindowTap(String googleMarkerId) {
306304
String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId);
307305
if (markerId == null) {
308306
return;
309307
}
310-
flutterApi.onInfoWindowTap(markerId, (Result<Unit> result) -> Unit.INSTANCE);
308+
flutterApi.onInfoWindowTap(markerId, ResultUtilsKt.emptyContinuation());
311309
}
312310

313311
/**
314312
* Called when a cluster-managed marker's info window is tapped. Takes the Dart marker ID
315313
* directly.
316314
*/
317315
void onClusterItemInfoWindowTap(String markerId) {
318-
flutterApi.onInfoWindowTap(markerId, (Result<Unit> result) -> Unit.INSTANCE);
316+
flutterApi.onInfoWindowTap(markerId, ResultUtilsKt.emptyContinuation());
319317
}
320318

321319
/**

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/PolygonsController.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
14-
import kotlin.Result;
15-
import kotlin.Unit;
1614

1715
class PolygonsController {
1816

@@ -60,7 +58,7 @@ boolean onPolygonTap(String googlePolygonId) {
6058
if (polygonId == null) {
6159
return false;
6260
}
63-
flutterApi.onPolygonTap(polygonId, (Result<Unit> result) -> Unit.INSTANCE);
61+
flutterApi.onPolygonTap(polygonId, ResultUtilsKt.emptyContinuation());
6462
PolygonController polygonController = polygonIdToController.get(polygonId);
6563
if (polygonController != null) {
6664
return polygonController.consumeTapEvents();

‎packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/PolylinesController.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15-
import kotlin.Result;
16-
import kotlin.Unit;
1715

1816
class PolylinesController {
1917

@@ -64,7 +62,7 @@ boolean onPolylineTap(String googlePolylineId) {
6462
if (polylineId == null) {
6563
return false;
6664
}
67-
flutterApi.onPolylineTap(polylineId, (Result<Unit> result) -> Unit.INSTANCE);
65+
flutterApi.onPolylineTap(polylineId, ResultUtilsKt.emptyContinuation());
6866
PolylineController polylineController = polylineIdToController.get(polylineId);
6967
if (polylineController != null) {
7068
return polylineController.consumeTapEvents();

0 commit comments

Comments
 (0)