Skip to content

Commit

Permalink
Cherry-pick upstream#775 (ios onstyleready reliability) (#79)
Browse files Browse the repository at this point in the history
* Cherry-pick upstream#775 (Feature/771 ios onstyleready reliability)

https: //github.com/flutter-mapbox-gl/maps/pull/775

Co-Authored-By: Anton Averin <1481332+AAverin@users.noreply.github.com>
  • Loading branch information
m0nac0 and AAverin authored May 15, 2022
1 parent 1e3f2dd commit 592bda1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
27 changes: 23 additions & 4 deletions example/lib/full_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,39 @@ class FullMap extends StatefulWidget {

class FullMapState extends State<FullMap> {
MaplibreMapController? mapController;
var isLight = true;

void _onMapCreated(MaplibreMapController controller) {
_onMapCreated(MaplibreMapController controller) {
mapController = controller;
}

_onStyleLoadedCallback() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Style loaded :)"),
backgroundColor: Theme.of(context).primaryColor,
duration: Duration(seconds: 1),
));
}

@override
Widget build(BuildContext context) {
return new Scaffold(
// TODO: commented out when cherry-picking https://github.com/flutter-mapbox-gl/maps/pull/775
// needs different dark and light styles in this repo
// floatingActionButton: Padding(
// padding: const EdgeInsets.all(32.0),
// child: FloatingActionButton(
// child: Icon(Icons.swap_horiz),
// onPressed: () => setState(
// () => isLight = !isLight,
// ),
// ),
// ),
body: MaplibreMap(
// TODO: styleString: isLight ? MapboxStyles.LIGHT : MapboxStyles.DARK,
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
onStyleLoadedCallback: onStyleLoadedCallback,
onStyleLoadedCallback: _onStyleLoadedCallback,
));
}

void onStyleLoadedCallback() {}
}
13 changes: 10 additions & 3 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var mapView: MGLMapView
private var isMapReady = false
private var isStyleReady = false
private var isStyleReadyNotified = false
private var mapReadyResult: FlutterResult?

private var initialTilt: CGFloat?
Expand Down Expand Up @@ -85,9 +86,10 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
result(nil)
//Style could happen to been ready before the map was ready due to the order of methods invoked
//We should invoke onStyleLoaded
if isStyleReady {
if isStyleReady && !isStyleReadyNotified {
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
isStyleReadyNotified = true
}
}
} else {
Expand Down Expand Up @@ -901,6 +903,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
* MGLMapViewDelegate
*/
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
isStyleReadyNotified = false
isMapReady = true
updateMyLocationEnabled()

Expand Down Expand Up @@ -937,9 +940,13 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
//If not, we will have to call it when map#waitForMap is done
if let mapReadyResult = mapReadyResult {
mapReadyResult(nil)
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
if (!isStyleReadyNotified) {
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
isStyleReadyNotified = true
}
}

}

isStyleReady = true
Expand Down

0 comments on commit 592bda1

Please sign in to comment.