Skip to content

Commit 61bf343

Browse files
authored
Map events race condition fix (#534)
* Subscribe to initial map events upon map widget initialization * Regenerate code
1 parent 728fc8d commit 61bf343

22 files changed

+140
-123
lines changed

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/EventHandler.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import java.util.Date
3636

3737
class MapboxEventHandler(
3838
private val eventProvider: Observable,
39-
binaryMessenger: BinaryMessenger
39+
binaryMessenger: BinaryMessenger,
40+
eventTypes: List<Int>
4041
) : MethodChannel.MethodCallHandler {
4142
private val channel: MethodChannel
4243
private val cancellables = HashSet<Cancelable>()
@@ -48,6 +49,11 @@ class MapboxEventHandler(
4849
init {
4950
channel = MethodChannel(binaryMessenger, "com.mapbox.maps.flutter.map_events")
5051
channel.setMethodCallHandler(this)
52+
53+
eventTypes
54+
.map { _MapEvent.ofRaw(it) }
55+
.filterNotNull()
56+
.forEach { subscribeToEvent(it) }
5157
}
5258

5359
override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/MapboxMapController.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class MapboxMapController(
3838
private val lifecycleProvider: MapboxMapsPlugin.LifecycleProvider,
3939
messenger: BinaryMessenger,
4040
channelSuffix: Int,
41-
pluginVersion: String
41+
pluginVersion: String,
42+
eventTypes: List<Int>
4243
) : PlatformView,
4344
DefaultLifecycleObserver,
4445
MethodChannel.MethodCallHandler {
@@ -122,7 +123,7 @@ class MapboxMapController(
122123
val mapboxMap = mapView.mapboxMap
123124
this.mapView = mapView
124125
this.mapboxMap = mapboxMap
125-
eventHandler = MapboxEventHandler(mapboxMap.styleManager, proxyBinaryMessenger)
126+
eventHandler = MapboxEventHandler(mapboxMap.styleManager, proxyBinaryMessenger, eventTypes)
126127
styleController = StyleController(context, mapboxMap)
127128
cameraController = CameraController(mapboxMap, context)
128129
projectionController = MapProjectionController(mapboxMap)

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/MapboxMapFactory.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class MapboxMapFactory(
116116
val textureView = params["textureView"] as? Boolean ?: false
117117
val styleUri = params["styleUri"] as? String ?: Style.MAPBOX_STREETS
118118
val pluginVersion = params["mapboxPluginVersion"] as String
119+
val eventTypes: List<Int> = params["eventTypes"] as List<Int>
120+
119121
val mapInitOptions = MapInitOptions(
120122
context = context,
121123
mapOptions = mapOptionsBuilder.build(),
@@ -130,6 +132,7 @@ class MapboxMapFactory(
130132
messenger,
131133
channelSuffix,
132134
pluginVersion,
135+
eventTypes
133136
)
134137
}
135138

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/pigeons/SnapshotterMessenger.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private object _SnapshotterInstanceManagerCodec : StandardMessageCodec() {
198198

199199
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
200200
interface _SnapshotterInstanceManager {
201-
fun setupSnapshotterForSuffix(suffix: String, options: MapSnapshotOptions)
201+
fun setupSnapshotterForSuffix(suffix: String, eventTypes: List<Long>, options: MapSnapshotOptions)
202202
fun tearDownSnapshotterForSuffix(suffix: String)
203203

204204
companion object {
@@ -216,10 +216,11 @@ interface _SnapshotterInstanceManager {
216216
channel.setMessageHandler { message, reply ->
217217
val args = message as List<Any?>
218218
val suffixArg = args[0] as String
219-
val optionsArg = args[1] as MapSnapshotOptions
219+
val eventTypesArg = args[1] as List<Long>
220+
val optionsArg = args[2] as MapSnapshotOptions
220221
var wrapped: List<Any?>
221222
try {
222-
api.setupSnapshotterForSuffix(suffixArg, optionsArg)
223+
api.setupSnapshotterForSuffix(suffixArg, eventTypesArg, optionsArg)
223224
wrapped = listOf<Any?>(null)
224225
} catch (exception: Throwable) {
225226
wrapped = wrapError(exception)

android/src/main/kotlin/com/mapbox/maps/mapbox_maps/snapshotter/SnapshotterInstanceManager.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ class SnapshotterInstanceManager(
2424
private var proxyMessengers = HashMap<String, ProxyBinaryMessenger>()
2525

2626
@SuppressLint("RestrictedApi")
27-
override fun setupSnapshotterForSuffix(suffix: String, options: MapSnapshotOptions) {
27+
override fun setupSnapshotterForSuffix(
28+
suffix: String,
29+
eventTypes: List<Long>,
30+
options: MapSnapshotOptions
31+
) {
2832
val snapshotter = Snapshotter(
2933
context,
3034
options = options.toSnapshotOptions(context),
3135
overlayOptions = options.toSnapshotOverlayOptions()
3236
)
3337
val proxyBinaryMessenger = ProxyBinaryMessenger(messenger, suffix)
3438
val styleManager: com.mapbox.maps.StyleManager = snapshotter.styleManager() // TODO: expose this on Android
35-
val eventHandler = MapboxEventHandler(styleManager, proxyBinaryMessenger)
39+
val eventHandler = MapboxEventHandler(styleManager, proxyBinaryMessenger, eventTypes.map { it.toInt() })
3640
val snapshotterController = SnapshotterController(context, snapshotter, eventHandler)
3741
val mapboxStyleManager = MapboxStyleManager(
3842
styleManager,

example/integration_test/snapshotter/snapshotter_test.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ void main() {
4646
pixelRatio: 1.5,
4747
);
4848
final snapshotter = await Snapshotter.create(
49-
options: options,
50-
onStyleLoadedListener: styleLoaded.complete,
51-
onStyleDataLoadedListener: styleDataLoaded.complete,
52-
);
49+
options: options,
50+
onStyleLoadedListener: styleLoaded.complete,
51+
onStyleDataLoadedListener: styleDataLoaded.complete,
52+
);
5353

5454
await snapshotter.style.setStyleURI(MapboxStyles.LIGHT);
5555

@@ -98,8 +98,10 @@ void main() {
9898
bearing: cameraOptions.bearing,
9999
pitch: cameraOptions.pitch,
100100
);
101-
expect(camera.center?.coordinates.lat, closeTo(cameraOptions.center!.coordinates.lat, 0.001));
102-
expect(camera.center?.coordinates.lng, closeTo(cameraOptions.center!.coordinates.lng, 0.001));
101+
expect(camera.center?.coordinates.lat,
102+
closeTo(cameraOptions.center!.coordinates.lat, 0.001));
103+
expect(camera.center?.coordinates.lng,
104+
closeTo(cameraOptions.center!.coordinates.lng, 0.001));
103105
expect(camera.bearing, closeTo(cameraOptions.bearing!, 0.001));
104106
expect(camera.pitch, closeTo(cameraOptions.pitch!, 0.001));
105107

example/ios/Podfile.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ PODS:
22
- Flutter (1.0.0)
33
- integration_test (0.0.1):
44
- Flutter
5-
- mapbox_maps_flutter (1.1.0):
5+
- mapbox_maps_flutter (2.0.0-beta.1):
66
- Flutter
77
- MapboxMaps (~> 11.4.0-beta.2)
88
- Turf (= 2.8.0)
9-
- MapboxCommon (24.4.0-beta.2)
9+
- MapboxCommon (24.4.0-beta.3)
1010
- MapboxCoreMaps (11.4.0-beta.2):
1111
- MapboxCommon (~> 24.4.0-beta)
12-
- MapboxMaps (11.4.0-beta.2):
13-
- MapboxCommon (= 24.4.0-beta.2)
12+
- MapboxMaps (11.4.0-beta.3):
13+
- MapboxCommon (= 24.4.0-beta.3)
1414
- MapboxCoreMaps (= 11.4.0-beta.2)
1515
- Turf (= 2.8.0)
1616
- permission_handler_apple (9.1.1):
@@ -43,10 +43,10 @@ EXTERNAL SOURCES:
4343
SPEC CHECKSUMS:
4444
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
4545
integration_test: 13825b8a9334a850581300559b8839134b124670
46-
mapbox_maps_flutter: a5b851d1c13a23e6232400375af26b63307223f4
47-
MapboxCommon: 7e0be4c0792c7f037e1a52faada7b9ffec6abdee
46+
mapbox_maps_flutter: f749f291a37f900866ee4981e2919b7223364095
47+
MapboxCommon: 163268386543eccc75daadf33251e06dad0df25c
4848
MapboxCoreMaps: 5120129b4c1b46eeccf19dd260f77c83e345e081
49-
MapboxMaps: 7111c4812c5a6b4692bd46913db0bcc1b4db8b9f
49+
MapboxMaps: 37c72ec739d5fa48b87eb2ba283f6937a61419f9
5050
permission_handler_apple: e76247795d700c14ea09e3a2d8855d41ee80a2e6
5151
Turf: aa2ede4298009639d10db36aba1a7ebaad072a5e
5252

example/lib/geojson_line.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class DrawGeoJsonLineWidgetState extends State<DrawGeoJsonLineWidget> {
3535
lineJoin: LineJoin.ROUND,
3636
lineCap: LineCap.ROUND,
3737
lineColor: Colors.red.value,
38-
lineWidth: 6.0));
38+
lineWidth: 6.0));
3939
}
4040

4141
@override
4242
Widget build(BuildContext context) {
4343
return new Scaffold(
44-
body: MapWidget(
44+
body: MapWidget(
4545
key: ValueKey("mapWidget"),
4646
styleUri: MapboxStyles.MAPBOX_STREETS,
4747
cameraOptions: CameraOptions(

example/lib/snapshotter.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ class SnapshotterPageBodyState extends State<SnapshotterPageBody> {
9191
color: Colors.amber,
9292
),
9393
),
94-
Expanded(key: _snapshotKey, child: snapshotImage ?? ColoredBox(color: Colors.grey)),
94+
Expanded(
95+
key: _snapshotKey,
96+
child: snapshotImage ?? ColoredBox(color: Colors.grey)),
9597
],
9698
),
9799
);

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ packages:
192192
path: ".."
193193
relative: true
194194
source: path
195-
version: "1.1.0"
195+
version: "2.0.0-beta.1"
196196
matcher:
197197
dependency: transitive
198198
description:

0 commit comments

Comments
 (0)