Refactor: extract exif function

This commit is contained in:
Ming Ming 2022-08-20 18:51:09 +08:00
parent 5e385097a0
commit 610bf688a0
2 changed files with 47 additions and 20 deletions

View file

@ -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<Rational> 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;
}

View file

@ -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<ViewerDetailPane> {
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<ViewerDetailPane> {
});
}
static double _gpsDmsToDouble(List<Rational> 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) {