From 610bf688a05817b9b3d7bf4244548df931a0cfb6 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 20 Aug 2022 18:51:09 +0800 Subject: [PATCH] Refactor: extract exif function --- app/lib/entity/exif_extension.dart | 43 ++++++++++++++++++++++++++ app/lib/widget/viewer_detail_pane.dart | 24 +++----------- 2 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 app/lib/entity/exif_extension.dart diff --git a/app/lib/entity/exif_extension.dart b/app/lib/entity/exif_extension.dart new file mode 100644 index 00000000..f92bf7ac --- /dev/null +++ b/app/lib/entity/exif_extension.dart @@ -0,0 +1,43 @@ +import 'package:exifdart/exifdart.dart'; +import 'package:nc_photos/entity/exif.dart'; + +extension ExifExtension on Exif { + double? get gpsLatitudeDeg { + if (gpsLatitude == null || gpsLatitudeRef == null) { + return null; + } else if (gpsLatitudeRef != "N" && gpsLatitudeRef != "S") { + // invalid value + return null; + } else if (gpsLatitude!.any((e) => e.denominator == 0)) { + // invalid value + return null; + } else { + return _gpsDmsToDouble(gpsLatitude!) * (gpsLatitudeRef == "S" ? -1 : 1); + } + } + + double? get gpsLongitudeDeg { + if (gpsLongitude == null || gpsLongitudeRef == null) { + return null; + } else if (gpsLongitudeRef != "E" && gpsLongitudeRef != "W") { + // invalid value + return null; + } else if (gpsLongitude!.any((e) => e.denominator == 0)) { + // invalid value + return null; + } else { + return _gpsDmsToDouble(gpsLongitude!) * (gpsLongitudeRef == "W" ? -1 : 1); + } + } +} + +double _gpsDmsToDouble(List dms) { + double product = dms[0].toDouble(); + if (dms.length > 1) { + product += dms[1].toDouble() / 60; + } + if (dms.length > 2) { + product += dms[2].toDouble() / 3600; + } + return product; +} diff --git a/app/lib/widget/viewer_detail_pane.dart b/app/lib/widget/viewer_detail_pane.dart index e6f1568f..1d24a88f 100644 --- a/app/lib/widget/viewer_detail_pane.dart +++ b/app/lib/widget/viewer_detail_pane.dart @@ -1,7 +1,6 @@ import 'dart:async'; import 'package:android_intent_plus/android_intent.dart'; -import 'package:exifdart/exifdart.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:kiwi/kiwi.dart'; @@ -15,6 +14,7 @@ import 'package:nc_photos/entity/album.dart'; import 'package:nc_photos/entity/album/cover_provider.dart'; import 'package:nc_photos/entity/album/item.dart'; import 'package:nc_photos/entity/album/provider.dart'; +import 'package:nc_photos/entity/exif_extension.dart'; import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/notified_action.dart'; @@ -334,14 +334,9 @@ class _ViewerDetailPaneState extends State { if (exif.isoSpeedRatings != null) { _isoSpeedRatings = exif.isoSpeedRatings!; } - if (exif.gpsLatitudeRef != null && - exif.gpsLatitude != null && - exif.gpsLongitudeRef != null && - exif.gpsLongitude != null) { - final lat = _gpsDmsToDouble(exif.gpsLatitude!) * - (exif.gpsLatitudeRef == "S" ? -1 : 1); - final lng = _gpsDmsToDouble(exif.gpsLongitude!) * - (exif.gpsLongitudeRef == "W" ? -1 : 1); + final lat = exif.gpsLatitudeDeg; + final lng = exif.gpsLongitudeDeg; + if (lat != null && lng != null) { _log.fine("GPS: ($lat, $lng)"); _gps = Tuple2(lat, lng); } @@ -494,17 +489,6 @@ class _ViewerDetailPaneState extends State { }); } - static double _gpsDmsToDouble(List dms) { - double product = dms[0].toDouble(); - if (dms.length > 1) { - product += dms[1].toDouble() / 60; - } - if (dms.length > 2) { - product += dms[2].toDouble() / 3600; - } - return product; - } - bool _checkCanRemoveFromAlbum() { if (widget.album == null || widget.album!.provider is! AlbumStaticProvider) {