2023-06-04 18:29:03 +00:00
|
|
|
import "dart:async";
|
2023-06-05 03:12:43 +00:00
|
|
|
import "dart:math";
|
2023-06-04 18:29:03 +00:00
|
|
|
|
2023-06-05 03:12:43 +00:00
|
|
|
import "package:flutter/foundation.dart";
|
2023-06-04 18:29:03 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_map/flutter_map.dart';
|
|
|
|
import "package:latlong2/latlong.dart";
|
2023-06-04 23:22:21 +00:00
|
|
|
import "package:logging/logging.dart";
|
2023-06-04 18:29:03 +00:00
|
|
|
import "package:photos/models/file.dart";
|
2023-06-10 09:52:54 +00:00
|
|
|
import "package:photos/theme/ente_theme.dart";
|
2023-06-12 11:19:02 +00:00
|
|
|
import "package:photos/ui/common/loading_widget.dart";
|
2023-06-04 18:29:03 +00:00
|
|
|
import "package:photos/ui/map/image_marker.dart";
|
2023-06-05 11:47:38 +00:00
|
|
|
import 'package:photos/ui/map/image_tile.dart';
|
2023-06-04 18:29:03 +00:00
|
|
|
import "package:photos/ui/map/map_view.dart";
|
2023-06-05 13:11:39 +00:00
|
|
|
import "package:photos/utils/toast_util.dart";
|
2023-06-04 18:29:03 +00:00
|
|
|
|
|
|
|
class MapScreen extends StatefulWidget {
|
2023-06-05 03:40:25 +00:00
|
|
|
// Add a function parameter where the function returns a Future<List<File>>
|
|
|
|
|
|
|
|
final Future<List<File>> Function() filesFutureFn;
|
|
|
|
|
|
|
|
const MapScreen({
|
|
|
|
super.key,
|
|
|
|
required this.filesFutureFn,
|
|
|
|
});
|
2023-06-04 18:29:03 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _MapScreenState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MapScreenState extends State<MapScreen> {
|
|
|
|
List<ImageMarker> imageMarkers = [];
|
|
|
|
List<File> allImages = [];
|
|
|
|
List<File> visibleImages = [];
|
|
|
|
MapController mapController = MapController();
|
|
|
|
bool isLoading = true;
|
2023-06-05 03:40:25 +00:00
|
|
|
double initialZoom = 4.0;
|
2023-06-05 13:11:39 +00:00
|
|
|
double maxZoom = 19.0;
|
|
|
|
double minZoom = 0.0;
|
2023-06-05 11:47:38 +00:00
|
|
|
int debounceDuration = 200;
|
2023-06-05 13:11:39 +00:00
|
|
|
LatLng center = LatLng(46.7286, 4.8614);
|
2023-06-04 23:22:21 +00:00
|
|
|
final Logger _logger = Logger("_MapScreenState");
|
2023-06-04 18:29:03 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:22:21 +00:00
|
|
|
Future<void> initialize() async {
|
|
|
|
try {
|
2023-06-05 03:40:25 +00:00
|
|
|
allImages = await widget.filesFutureFn();
|
2023-06-04 23:22:21 +00:00
|
|
|
processFiles(allImages);
|
|
|
|
} catch (e, s) {
|
|
|
|
_logger.severe("Error initializing map screen", e, s);
|
|
|
|
}
|
2023-06-04 18:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void processFiles(List<File> files) {
|
2023-06-05 03:12:43 +00:00
|
|
|
late double minLat, maxLat, minLon, maxLon;
|
2023-06-04 18:29:03 +00:00
|
|
|
final List<ImageMarker> tempMarkers = [];
|
2023-06-05 03:12:43 +00:00
|
|
|
bool hasAnyLocation = false;
|
2023-06-04 18:29:03 +00:00
|
|
|
for (var file in files) {
|
2023-06-05 13:11:39 +00:00
|
|
|
if (file.hasLocation) {
|
2023-06-05 03:12:43 +00:00
|
|
|
if (!hasAnyLocation) {
|
|
|
|
minLat = file.location!.latitude!;
|
|
|
|
minLon = file.location!.longitude!;
|
|
|
|
maxLat = file.location!.latitude!;
|
|
|
|
maxLon = file.location!.longitude!;
|
|
|
|
hasAnyLocation = true;
|
|
|
|
} else {
|
|
|
|
minLat = min(minLat, file.location!.latitude!);
|
|
|
|
minLon = min(minLon, file.location!.longitude!);
|
|
|
|
maxLat = max(maxLat, file.location!.latitude!);
|
|
|
|
maxLon = max(maxLon, file.location!.longitude!);
|
|
|
|
}
|
2023-06-04 23:21:51 +00:00
|
|
|
tempMarkers.add(
|
|
|
|
ImageMarker(
|
|
|
|
latitude: file.location!.latitude!,
|
|
|
|
longitude: file.location!.longitude!,
|
|
|
|
imageFile: file,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-06-04 18:29:03 +00:00
|
|
|
}
|
2023-06-05 03:12:43 +00:00
|
|
|
if (hasAnyLocation) {
|
|
|
|
center = LatLng(
|
|
|
|
minLat + (maxLat - minLat) / 2,
|
|
|
|
minLon + (maxLon - minLon) / 2,
|
|
|
|
);
|
2023-06-05 11:47:38 +00:00
|
|
|
final latRange = maxLat - minLat;
|
|
|
|
final lonRange = maxLon - minLon;
|
|
|
|
|
|
|
|
final latZoom = log(360.0 / latRange) / log(2);
|
|
|
|
final lonZoom = log(180.0 / lonRange) / log(2);
|
|
|
|
|
2023-06-05 03:12:43 +00:00
|
|
|
initialZoom = min(latZoom, lonZoom);
|
2023-06-05 13:11:39 +00:00
|
|
|
if (initialZoom <= minZoom) initialZoom = minZoom + 1;
|
|
|
|
if (initialZoom >= (maxZoom - 1)) initialZoom = maxZoom - 1;
|
2023-06-05 03:12:43 +00:00
|
|
|
if (kDebugMode) {
|
|
|
|
debugPrint("Info for map: center $center, initialZoom $initialZoom");
|
|
|
|
debugPrint("Info for map: minLat $minLat, maxLat $maxLat");
|
|
|
|
debugPrint("Info for map: minLon $minLon, maxLon $maxLon");
|
|
|
|
}
|
2023-06-05 13:11:39 +00:00
|
|
|
} else {
|
2023-06-06 03:45:47 +00:00
|
|
|
showShortToast(context, "No images with location");
|
2023-06-05 03:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 18:29:03 +00:00
|
|
|
setState(() {
|
|
|
|
imageMarkers = tempMarkers;
|
|
|
|
});
|
2023-06-05 11:47:38 +00:00
|
|
|
|
|
|
|
mapController.move(
|
|
|
|
center,
|
|
|
|
initialZoom,
|
|
|
|
);
|
|
|
|
|
|
|
|
Timer(Duration(milliseconds: debounceDuration), () {
|
|
|
|
updateVisibleImages(mapController.bounds!);
|
|
|
|
setState(() {
|
|
|
|
isLoading = false;
|
|
|
|
});
|
|
|
|
});
|
2023-06-04 18:29:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 11:47:38 +00:00
|
|
|
void updateVisibleImages(LatLngBounds bounds) {
|
2023-06-04 18:29:03 +00:00
|
|
|
final images = imageMarkers
|
|
|
|
.where((imageMarker) {
|
|
|
|
final point = LatLng(imageMarker.latitude, imageMarker.longitude);
|
|
|
|
return bounds.contains(point);
|
|
|
|
})
|
|
|
|
.map((imageMarker) => imageMarker.imageFile)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
visibleImages = images;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-06-12 10:06:39 +00:00
|
|
|
final textTheme = getEnteTextTheme(context);
|
2023-06-12 11:19:02 +00:00
|
|
|
final colorScheme = getEnteColorScheme(context);
|
2023-06-10 09:52:54 +00:00
|
|
|
return Container(
|
2023-06-12 11:19:02 +00:00
|
|
|
color: colorScheme.backgroundBase,
|
2023-06-10 09:52:54 +00:00
|
|
|
child: SafeArea(
|
|
|
|
top: false,
|
|
|
|
child: Scaffold(
|
|
|
|
body: Stack(
|
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
2023-06-10 11:15:32 +00:00
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.only(
|
|
|
|
bottomLeft: Radius.circular(6),
|
|
|
|
bottomRight: Radius.circular(6),
|
|
|
|
),
|
|
|
|
child: MapView(
|
|
|
|
controller: mapController,
|
|
|
|
imageMarkers: imageMarkers,
|
|
|
|
updateVisibleImages: updateVisibleImages,
|
|
|
|
center: center,
|
|
|
|
initialZoom: initialZoom,
|
|
|
|
minZoom: minZoom,
|
|
|
|
maxZoom: maxZoom,
|
|
|
|
debounceDuration: debounceDuration,
|
|
|
|
),
|
2023-06-04 18:29:03 +00:00
|
|
|
),
|
|
|
|
),
|
2023-06-10 11:15:32 +00:00
|
|
|
const SizedBox(height: 4),
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.only(
|
|
|
|
topLeft: Radius.circular(2),
|
|
|
|
topRight: Radius.circular(2),
|
|
|
|
),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 116,
|
2023-06-12 10:06:39 +00:00
|
|
|
child: AnimatedSwitcher(
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
switchInCurve: Curves.easeInOutExpo,
|
|
|
|
switchOutCurve: Curves.easeInOutExpo,
|
|
|
|
child: visibleImages.isNotEmpty
|
|
|
|
? ListView.builder(
|
|
|
|
itemCount: visibleImages.length,
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 2),
|
|
|
|
physics: const BouncingScrollPhysics(),
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final image = visibleImages[index];
|
|
|
|
return ImageTile(
|
|
|
|
image: image,
|
|
|
|
allImages: allImages,
|
|
|
|
visibleImages: visibleImages,
|
|
|
|
index: index,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
"No photos found here",
|
|
|
|
style: textTheme.large,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 4),
|
|
|
|
Text(
|
|
|
|
"Zoom out to see photos",
|
|
|
|
style: textTheme.smallFaint,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2023-06-10 09:52:54 +00:00
|
|
|
),
|
2023-06-04 18:29:03 +00:00
|
|
|
),
|
|
|
|
)
|
2023-06-10 09:52:54 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
isLoading
|
2023-06-12 11:19:02 +00:00
|
|
|
? EnteLoadingWidget(
|
|
|
|
size: 28,
|
|
|
|
color: getEnteColorScheme(context).primary700,
|
2023-06-10 09:52:54 +00:00
|
|
|
)
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
],
|
|
|
|
),
|
2023-06-04 18:29:03 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|