Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): debounce map layer update #6861

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mobile/lib/extensions/maplibrecontroller_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/services.dart';
Expand All @@ -6,6 +7,8 @@ import 'package:immich_mobile/modules/map/utils/map_utils.dart';
import 'package:maplibre_gl/maplibre_gl.dart';

extension MapMarkers on MaplibreMapController {
static var _completer = Completer()..complete();

Future<void> addGeoJSONSourceForMarkers(List<MapMarker> markers) async {
return addSource(
MapUtils.defaultSourceId,
Expand All @@ -16,6 +19,12 @@ extension MapMarkers on MaplibreMapController {
}

Future<void> reloadAllLayersForMarkers(List<MapMarker> markers) async {
// Wait for previous reload to complete
if (!_completer.isCompleted) {
return _completer.future;
}
_completer = Completer();

// !! Make sure to remove layers before sources else the native
// maplibre library would crash when removing the source saying that
// the source is still in use
Expand All @@ -36,6 +45,8 @@ extension MapMarkers on MaplibreMapController {
MapUtils.defaultHeatMapLayerId,
MapUtils.defaultHeatMapLayerProperties,
);

_completer.complete();
}

Future<Symbol?> addMarkerAtLatLng(LatLng centre) async {
Expand Down
5 changes: 4 additions & 1 deletion mobile/lib/modules/map/views/map_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MapPage extends HookConsumerWidget {
final bottomSheetStreamController = useStreamController<MapEvent>();
final selectedMarker = useValueNotifier<_AssetMarkerMeta?>(null);
final assetsDebouncer = useDebouncer();
final layerDebouncer = useDebouncer(interval: const Duration(seconds: 1));
final isLoading = useProcessingOverlay();
final scrollController = useScrollController();
final markerDebouncer =
Expand Down Expand Up @@ -77,7 +78,9 @@ class MapPage extends HookConsumerWidget {
// removes all sources and layers and re-adds them with the updated markers
Future<void> reloadLayers() async {
if (mapController.value != null) {
mapController.value!.reloadAllLayersForMarkers(markers.value);
layerDebouncer.run(
() => mapController.value!.reloadAllLayersForMarkers(markers.value),
);
}
}

Expand Down
Loading