Skip to content

Commit

Permalink
fix: change getCurrentPosition to getPositionStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Aline Bonnet authored and luifr10 committed Apr 25, 2024
1 parent 0528792 commit 71e98d4
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions lib/page/capture_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class _CapturePageState extends State<CapturePage> {
bool _isRearCameraSelected = true;
final List<File> _imgListCaptured = [];

Stream<Position>? _positionStream;
Position? _currentPosition;
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 2, //The minimum distance (in meters) to trigger an update
);

@override
void dispose() {
_cameraController.dispose();
Expand All @@ -25,11 +32,31 @@ class _CapturePageState extends State<CapturePage> {
void initState() {
super.initState();

startLocationUpdates();

if (widget.cameras?.isNotEmpty ?? false) {
initCamera(widget.cameras![0]);
}
}

void startLocationUpdates() async {
if (await PermissionHelper.isPermissionGranted()) {
_positionStream =
Geolocator.getPositionStream(locationSettings: locationSettings);

if (_positionStream != null) {
_positionStream!.listen((Position position) {
setState(() {
_currentPosition = position;
});
});
}
} else {
await PermissionHelper.askMissingPermission();
startLocationUpdates();
}
}

void goToCollectionCreationPage() {
context.push(Routes.newSequenceSend, extra: _imgListCaptured);
}
Expand All @@ -45,22 +72,16 @@ class _CapturePageState extends State<CapturePage> {
return null;
}
try {
if (await PermissionHelper.isPermissionGranted()) {
await Future.wait(
[
getPictureFromCamera(),
Geolocator.getCurrentPosition(),
],
).then((value) async {
final XFile rawImage = value[0] as XFile;
final Position currentLocation = value[1] as Position;
await addExifTags(rawImage, currentLocation);
addImageToList(rawImage);
});
} else {
await PermissionHelper.askMissingPermission();
takePicture();
}
await Future.wait(
[
getPictureFromCamera(),
],
).then((value) async {
final XFile rawImage = value[0] as XFile;
final Position currentLocation = _currentPosition!;
await addExifTags(rawImage, currentLocation);
addImageToList(rawImage);
});
} on CameraException catch (e) {
debugPrint('Error occurred while taking picture: $e');
return null;
Expand All @@ -82,6 +103,7 @@ class _CapturePageState extends State<CapturePage> {
}

Future<void> addExifTags(XFile rawImage, Position currentLocation) async {
print(currentLocation.latitude.toString() + " " + currentLocation.longitude.toString());
final exif = FlutterExif.fromPath(rawImage.path);
await exif.setLatLong(currentLocation.latitude, currentLocation.longitude);
await exif.setAltitude(currentLocation.altitude);
Expand Down Expand Up @@ -110,7 +132,7 @@ class _CapturePageState extends State<CapturePage> {
if (widget.cameras?.isEmpty ?? true) {
return Scaffold(
appBar: AppBar(),
body: Center(
body: Center(
child: Text(AppLocalizations.of(context)!.noCameraFoundError),
),
);
Expand Down Expand Up @@ -154,7 +176,10 @@ class _CapturePageState extends State<CapturePage> {
child: IconButton(
padding: EdgeInsets.zero,
iconSize: 30,
icon: Icon(_isRearCameraSelected ? CupertinoIcons.switch_camera : CupertinoIcons.switch_camera_solid,
icon: Icon(
_isRearCameraSelected
? CupertinoIcons.switch_camera
: CupertinoIcons.switch_camera_solid,
color: Colors.white),
onPressed: () {
setState(() => _isRearCameraSelected = !_isRearCameraSelected);
Expand All @@ -171,7 +196,8 @@ class _CapturePageState extends State<CapturePage> {
iconSize: 30,
icon: const Icon(Icons.send_outlined, color: Colors.white),
onPressed: goToCollectionCreationPage,
tooltip: AppLocalizations.of(context)!.createSequenceWithPicture_tooltip));
tooltip: AppLocalizations.of(context)!
.createSequenceWithPicture_tooltip));
}

Widget imageCart(IconButton cartIcon) {
Expand Down

0 comments on commit 71e98d4

Please sign in to comment.