diff --git a/mobile/lib/presentation/widgets/map/map.state.dart b/mobile/lib/presentation/widgets/map/map.state.dart index e68d80390e..ecc7b0177e 100644 --- a/mobile/lib/presentation/widgets/map/map.state.dart +++ b/mobile/lib/presentation/widgets/map/map.state.dart @@ -41,7 +41,7 @@ class MapStateNotifier extends Notifier { // This provider watches the markers from the map service and serves the markers. // It should be used only after the map service provider is overridden final mapMarkerProvider = - StreamProvider.family, LatLngBounds>( + StreamProvider.family, LatLngBounds?>( (ref, bounds) async* { final mapService = ref.watch(mapServiceProvider); yield* mapService.watchMarkers(bounds).map((markers) { diff --git a/mobile/lib/presentation/widgets/map/map.widget.dart b/mobile/lib/presentation/widgets/map/map.widget.dart index 31e130afcb..1da8c5be4a 100644 --- a/mobile/lib/presentation/widgets/map/map.widget.dart +++ b/mobile/lib/presentation/widgets/map/map.widget.dart @@ -24,7 +24,7 @@ class DriftMapWithMarker extends ConsumerStatefulWidget { class _DriftMapWithMarkerState extends ConsumerState { MapLibreMapController? mapController; - static const mapZoomToAssetLevel = 12.0; + bool loadAllMarkers = false; @override void initState() { @@ -51,14 +51,17 @@ class _DriftMapWithMarkerState extends ConsumerState { ref.read(mapStateProvider.notifier).setBounds(bounds); } - Future reloadMarkers(Map markers) async { - if (mapController == null) return; + Future reloadMarkers( + Map markers, { + bool isLoadAllMarkers = false, + }) async { + if (mapController == null || loadAllMarkers) return; // Wait for previous reload to complete - if (!MapUtils.completer.isCompleted) { - return MapUtils.completer.future; + if (!MapUtils.markerCompleter.isCompleted) { + return MapUtils.markerCompleter.future; } - MapUtils.completer = Completer(); + MapUtils.markerCompleter = Completer(); // !! Make sure to remove layers before sources else the native // maplibre library would crash when removing the source saying that @@ -92,7 +95,9 @@ class _DriftMapWithMarkerState extends ConsumerState { ); } - MapUtils.completer.complete(); + if (isLoadAllMarkers) loadAllMarkers = true; + + MapUtils.markerCompleter.complete(); } Future onZoomToLocation() async { @@ -114,7 +119,7 @@ class _DriftMapWithMarkerState extends ConsumerState { mapController!.animateCamera( CameraUpdate.newLatLngZoom( LatLng(location.latitude, location.longitude), - mapZoomToAssetLevel, + MapUtils.mapZoomToAssetLevel, ), duration: const Duration(milliseconds: 800), ); @@ -168,19 +173,23 @@ class _Map extends StatelessWidget { class _Markers extends ConsumerWidget { const _Markers({required this.reloadMarkers}); - final Function(Map) reloadMarkers; + final Function(Map, {bool isLoadAllMarkers}) reloadMarkers; @override Widget build(BuildContext context, WidgetRef ref) { final bounds = ref.watch(mapStateProvider.select((s) => s.bounds)); AsyncValue> markers = ref.watch(mapMarkerProvider(bounds)); + AsyncValue> allMarkers = + ref.watch(mapMarkerProvider(null)); ref.listen(mapStateProvider, (previous, next) async { - markers = ref.watch(mapMarkerProvider(next.bounds)); + markers = ref.watch(mapMarkerProvider(bounds)); }); markers.whenData((markers) => reloadMarkers(markers)); + allMarkers + .whenData((markers) => reloadMarkers(markers, isLoadAllMarkers: true)); return const MapBottomSheet(); } diff --git a/mobile/lib/presentation/widgets/map/map_utils.dart b/mobile/lib/presentation/widgets/map/map_utils.dart index b9b3945616..1b565c1f3e 100644 --- a/mobile/lib/presentation/widgets/map/map_utils.dart +++ b/mobile/lib/presentation/widgets/map/map_utils.dart @@ -10,9 +10,10 @@ import 'package:maplibre_gl/maplibre_gl.dart'; class MapUtils { static final Logger _logger = Logger("MapUtils"); + static const mapZoomToAssetLevel = 12.0; static const defaultSourceId = 'asset-map-markers'; static const defaultHeatMapLayerId = 'asset-heatmap-layer'; - static var completer = Completer()..complete(); + static var markerCompleter = Completer()..complete(); static const defaultCircleLayerLayerProperties = CircleLayerProperties( circleRadius: 10,