Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Buffer photo map markers around viewport

Neeraj Gupta332eb23261a2
diff --git a/mobile/apps/photos/lib/ui/map/map_isolate.dart b/mobile/apps/photos/lib/ui/map/map_isolate.dart
index b0fce0af0e..2cf008da2f 100644
--- a/mobile/apps/photos/lib/ui/map/map_isolate.dart
+++ b/mobile/apps/photos/lib/ui/map/map_isolate.dart
@@ -2,7 +2,6 @@ import "dart:isolate";
import "dart:math";
import "package:flutter_map/flutter_map.dart";
-import "package:latlong2/latlong.dart";
class MapPoint {
final int imageIndex;
@@ -54,6 +53,18 @@ class _ProjectedMarkerGroup {
);
}
+class _ProjectedMapPoint {
+ final MapPoint point;
+ final double x;
+ final double y;
+
+ const _ProjectedMapPoint({
+ required this.point,
+ required this.x,
+ required this.y,
+ });
+}
+
class MapWorkerInit {
final List<MapPoint> points;
final SendPort sendPort;
@@ -65,11 +76,19 @@ class MapViewportRequest {
final int id;
final LatLngBounds bounds;
final double zoom;
+ final double markerMinX;
+ final double markerMinY;
+ final double markerMaxX;
+ final double markerMaxY;
const MapViewportRequest({
required this.id,
required this.bounds,
required this.zoom,
+ required this.markerMinX,
+ required this.markerMinY,
+ required this.markerMaxX,
+ required this.markerMaxY,
});
}
@@ -98,17 +117,22 @@ void mapWorker(MapWorkerInit init) {
}
final visibleIndexes = <int>[];
+ final visiblePoints = <_ProjectedMapPoint>[];
+ final bufferedPoints = <_ProjectedMapPoint>[];
final groupsByCell = <(int, int), List<_ProjectedMarkerGroup>>{};
final groups = <_ProjectedMarkerGroup>[];
final scale = _webMercatorTileSize * pow(2, message.zoom).toDouble();
for (final point in init.points) {
- final latLng = LatLng(point.latitude, point.longitude);
- if (!message.bounds.contains(latLng)) {
- continue;
+ final isVisible =
+ point.longitude >= message.bounds.west &&
+ point.longitude <= message.bounds.east &&
+ point.latitude >= message.bounds.south &&
+ point.latitude <= message.bounds.north;
+ if (isVisible) {
+ visibleIndexes.add(point.imageIndex);
}
- visibleIndexes.add(point.imageIndex);
final latitude = point.latitude.clamp(
-_mercatorLatitudeLimit,
_mercatorLatitudeLimit,
@@ -117,6 +141,21 @@ void mapWorker(MapWorkerInit init) {
final x = (point.longitude + 180) / 360 * scale;
final y =
(0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * pi)) * scale;
+ if (x < message.markerMinX ||
+ x > message.markerMaxX ||
+ y < message.markerMinY ||
+ y > message.markerMaxY) {
+ continue;
+ }
+
+ final projectedPoint = _ProjectedMapPoint(point: point, x: x, y: y);
+ (isVisible ? visiblePoints : bufferedPoints).add(projectedPoint);
+ }
+
+ void addToMarkerGroup(_ProjectedMapPoint projectedPoint) {
+ final point = projectedPoint.point;
+ final x = projectedPoint.x;
+ final y = projectedPoint.y;
final cell = (
(x / _minimumMarkerDistancePixels).floor(),
(y / _minimumMarkerDistancePixels).floor(),
@@ -150,7 +189,7 @@ void mapWorker(MapWorkerInit init) {
if (closestGroup != null) {
closestGroup.imageCount++;
- continue;
+ return;
}
final group = _ProjectedMarkerGroup(
@@ -164,6 +203,13 @@ void mapWorker(MapWorkerInit init) {
groupsByCell.putIfAbsent(cell, () => []).add(group);
}
+ for (final point in visiblePoints) {
+ addToMarkerGroup(point);
+ }
+ for (final point in bufferedPoints) {
+ addToMarkerGroup(point);
+ }
+
init.sendPort.send(
MapViewportResult(
id: message.id,
diff --git a/mobile/apps/photos/lib/ui/map/map_screen.dart b/mobile/apps/photos/lib/ui/map/map_screen.dart
index e4a4d280b8..dfd6a2dc13 100644
--- a/mobile/apps/photos/lib/ui/map/map_screen.dart
+++ b/mobile/apps/photos/lib/ui/map/map_screen.dart
@@ -56,6 +56,7 @@ class _MapScreenState extends State<MapScreen> {
static const bottomSheetDraggableAreaHeight = 32.0;
List<int>? _previousVisibleIndexes;
int _viewportRequestID = 0;
+ static const _markerViewportPaddingPixels = 256.0;
@override
void initState() {
@@ -129,10 +130,7 @@ class _MapScreenState extends State<MapScreen> {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
- _requestViewport(
- mapController.camera.visibleBounds,
- mapController.camera.zoom,
- );
+ _requestViewport(mapController.camera);
});
}
@@ -167,11 +165,20 @@ class _MapScreenState extends State<MapScreen> {
_mapWorkerIsolate = isolate;
}
- void _requestViewport(LatLngBounds bounds, double zoom) {
+ void _requestViewport(MapCamera camera) {
final sendPort = _mapWorkerSendPort;
if (sendPort == null) return;
+ final pixelBounds = camera.pixelBounds;
sendPort.send(
- MapViewportRequest(id: ++_viewportRequestID, bounds: bounds, zoom: zoom),
+ MapViewportRequest(
+ id: ++_viewportRequestID,
+ bounds: camera.visibleBounds,
+ zoom: camera.zoom,
+ markerMinX: pixelBounds.left - _markerViewportPaddingPixels,
+ markerMinY: pixelBounds.top - _markerViewportPaddingPixels,
+ markerMaxX: pixelBounds.right + _markerViewportPaddingPixels,
+ markerMaxY: pixelBounds.bottom + _markerViewportPaddingPixels,
+ ),
);
}
@@ -372,7 +379,7 @@ class _MapScreenState extends State<MapScreen> {
: MapView(
controller: mapController,
imageMarkers: imageMarkers,
- updateVisibleImages: _requestViewport,
+ updateViewport: _requestViewport,
center: initialCenter,
initialZoom: widget.initialZoom,
minZoom: minZoom,
diff --git a/mobile/apps/photos/lib/ui/map/map_view.dart b/mobile/apps/photos/lib/ui/map/map_view.dart
index e2ca1c85c3..0f774e29e1 100644
--- a/mobile/apps/photos/lib/ui/map/map_view.dart
+++ b/mobile/apps/photos/lib/ui/map/map_view.dart
@@ -36,7 +36,7 @@ Future<bool> _launchCoordinates(double latitude, double longitude) async {
class MapView extends StatefulWidget {
final List<ImageMarker> imageMarkers;
- final void Function(LatLngBounds bounds, double zoom) updateVisibleImages;
+ final void Function(MapCamera camera) updateViewport;
final MapController controller;
final LatLng center;
final double minZoom;
@@ -52,7 +52,7 @@ class MapView extends StatefulWidget {
const MapView({
super.key,
- required this.updateVisibleImages,
+ required this.updateViewport,
required this.imageMarkers,
required this.controller,
required this.center,
@@ -92,9 +92,9 @@ class _MapViewState extends State<MapView> {
}
}
- void onChange(LatLngBounds bounds, double zoom) {
+ void onChange(MapCamera camera) {
_debouncer.run(() async {
- widget.updateVisibleImages(bounds, zoom);
+ widget.updateViewport(camera);
});
}
@@ -126,7 +126,7 @@ class _MapViewState extends State<MapView> {
),
),
onPositionChanged: (position, hasGesture) {
- onChange(position.visibleBounds, position.zoom);
+ onChange(position);
},
),
children: [
diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/location_tags_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/location_tags_widget.dart
index b9f5f7adab..2a48b7bf21 100644
--- a/mobile/apps/photos/lib/ui/viewer/file_details/location_tags_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file_details/location_tags_widget.dart
@@ -208,7 +208,7 @@ class _InfoMapState extends State<InfoMap> {
key: ValueKey(_hasEnabledMap),
children: [
MapView(
- updateVisibleImages: (_, _) {},
+ updateViewport: (_) {},
imageMarkers: [
ImageMarker(
imageFile: widget.file,
@@ -253,7 +253,7 @@ class _InfoMapState extends State<InfoMap> {
clipBehavior: Clip.none,
children: [
MapView(
- updateVisibleImages: (_, _) {},
+ updateViewport: (_) {},
imageMarkers: const [],
controller: _mapController,
center: const LatLng(13.041599, 77.594566),