Browse Source

Fix: Updation of location via fileInfo

Neeraj Gupta 2 năm trước cách đây
mục cha
commit
b9b6830a07

+ 23 - 27
lib/ui/viewer/file/file_details_widget.dart

@@ -1,5 +1,6 @@
 import "package:exif/exif.dart";
 import "package:exif/exif.dart";
 import "package:flutter/material.dart";
 import "package:flutter/material.dart";
+import "package:logging/logging.dart";
 import "package:photos/core/configuration.dart";
 import "package:photos/core/configuration.dart";
 import "package:photos/models/file.dart";
 import "package:photos/models/file.dart";
 import "package:photos/models/file_type.dart";
 import "package:photos/models/file_type.dart";
@@ -52,6 +53,7 @@ class _FileDetailsWidgetState extends State<FileDetailsWidget> {
   late int _currentUserID;
   late int _currentUserID;
   bool showExifListTile = false;
   bool showExifListTile = false;
   final hasLocationDataNotifer = ValueNotifier(false);
   final hasLocationDataNotifer = ValueNotifier(false);
+  final Logger _logger = Logger("_FileDetailsWidgetState");
 
 
   @override
   @override
   void initState() {
   void initState() {
@@ -60,8 +62,8 @@ class _FileDetailsWidgetState extends State<FileDetailsWidget> {
     _isImage = widget.file.fileType == FileType.image ||
     _isImage = widget.file.fileType == FileType.image ||
         widget.file.fileType == FileType.livePhoto;
         widget.file.fileType == FileType.livePhoto;
     _exifNotifier.addListener(() {
     _exifNotifier.addListener(() {
-      if (_exifNotifier.value != null) {
-        hasLocationDataNotifer.value = _hasLocationData();
+      if (_exifNotifier.value != null && !widget.file.hasLocation) {
+        _updateLocationFromExif(_exifNotifier.value!).ignore();
       }
       }
     });
     });
     if (_isImage) {
     if (_isImage) {
@@ -231,32 +233,26 @@ class _FileDetailsWidgetState extends State<FileDetailsWidget> {
     );
     );
   }
   }
 
 
-  bool _hasLocationData() {
-    //This code is for updating the location of files in which location data is
-    //missing and the EXIF has location data. This is only happens for a
-    //certain specific minority of devices.
-    if (!widget.file.hasLocation) {
-      return _setLocationDataFromExif();
-    }
-    return true;
-  }
-
-  // _setLocationDataFromExif returns true if the location data is set from exif
-  bool _setLocationDataFromExif() {
-    final locationDataFromExif = locationFromExif(_exifNotifier.value!);
-    if (locationDataFromExif?.latitude != null &&
-        locationDataFromExif?.longitude != null) {
-      widget.file.location = locationDataFromExif;
-
-      FileMagicService.instance.updatePublicMagicMetadata([
-        widget.file
-      ], {
-        pubMagicKeyLat: locationDataFromExif!.latitude,
-        pubMagicKeyLong: locationDataFromExif.longitude
-      });
-      return true;
+  //This code is for updating the location of files in which location data is
+  //missing and the EXIF has location data. This is only happens for a
+  //certain specific minority of devices.
+  Future<void> _updateLocationFromExif(Map<String, IfdTag> exif) async {
+    try {
+      final locationDataFromExif = locationFromExif(exif);
+      if (locationDataFromExif?.latitude != null &&
+          locationDataFromExif?.longitude != null) {
+        widget.file.location = locationDataFromExif;
+        await FileMagicService.instance.updatePublicMagicMetadata([
+          widget.file
+        ], {
+          pubMagicKeyLat: locationDataFromExif!.latitude,
+          pubMagicKeyLong: locationDataFromExif.longitude
+        });
+        hasLocationDataNotifer.value = true;
+      }
+    } catch (e, s) {
+      _logger.severe("Error while updating location from EXIF", e, s);
     }
     }
-    return false;
   }
   }
 
 
   _generateExifForDetails(Map<String, IfdTag> exif) {
   _generateExifForDetails(Map<String, IfdTag> exif) {