safer metadata retrieval

This commit is contained in:
Thibault Deckers 2019-08-09 22:35:28 +09:00
parent 6d8a3afa9d
commit b014041a58
5 changed files with 30 additions and 23 deletions

View file

@ -176,7 +176,7 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
XMPProperty item = xmpMeta.getArrayItem(XMP_DC_SCHEMA_NS, XMP_SUBJECT_PROP_NAME, i);
sb.append(" ").append(item.getValue());
}
metadataMap.put("keywords", sb.toString());
metadataMap.put("xmpSubjects", sb.toString());
}
} catch (XMPException e) {
e.printStackTrace();

View file

@ -106,20 +106,20 @@ class ImageEntry {
class CatalogMetadata {
final int contentId, dateMillis;
final String keywords;
final String xmpSubjects;
final double latitude, longitude;
Address address;
CatalogMetadata({this.contentId, this.dateMillis, this.keywords, double latitude, double longitude})
CatalogMetadata({this.contentId, this.dateMillis, this.xmpSubjects, double latitude, double longitude})
// Geocoder throws an IllegalArgumentException when a coordinate has a funky values like 1.7056881853375E7
: this.latitude = latitude < -90.0 || latitude > 90.0 ? null : latitude,
this.longitude = longitude < -180.0 || longitude > 180.0 ? null : longitude;
: this.latitude = latitude == null || latitude < -90.0 || latitude > 90.0 ? null : latitude,
this.longitude = longitude == null || longitude < -180.0 || longitude > 180.0 ? null : longitude;
factory CatalogMetadata.fromMap(Map map) {
return CatalogMetadata(
contentId: map['contentId'],
dateMillis: map['dateMillis'],
keywords: map['keywords'],
xmpSubjects: map['xmpSubjects'],
latitude: map['latitude'],
longitude: map['longitude'],
);
@ -128,13 +128,13 @@ class CatalogMetadata {
Map<String, dynamic> toMap() => {
'contentId': contentId,
'dateMillis': dateMillis,
'keywords': keywords,
'xmpSubjects': xmpSubjects,
'latitude': latitude,
'longitude': longitude,
};
@override
String toString() {
return 'CatalogMetadata{contentId: $contentId, dateMillis: $dateMillis, latitude: $latitude, longitude: $longitude, keywords=$keywords}';
return 'CatalogMetadata{contentId: $contentId, dateMillis: $dateMillis, latitude: $latitude, longitude: $longitude, xmpSubjects=$xmpSubjects}';
}
}

View file

@ -26,7 +26,7 @@ class MetadataService {
// 'dateMillis': date taken in milliseconds since Epoch (long)
// 'latitude': latitude (double)
// 'longitude': longitude (double)
// 'keywords': space separated XMP subjects (string)
// 'xmpSubjects': space separated XMP subjects (string)
final result = await platform.invokeMethod('getCatalogMetadata', <String, dynamic>{
'path': path,
}) as Map;

View file

@ -20,7 +20,7 @@ class MetadataDb {
await path,
onCreate: (db, version) {
return db.execute(
'CREATE TABLE $table(contentId INTEGER PRIMARY KEY, dateMillis INTEGER, keywords TEXT, latitude REAL, longitude REAL)',
'CREATE TABLE $table(contentId INTEGER PRIMARY KEY, dateMillis INTEGER, xmpSubjects TEXT, latitude REAL, longitude REAL)',
);
},
version: 1,

View file

@ -37,19 +37,26 @@ class InfoPageState extends State<InfoPage> {
}
initMetadataLoader() {
_catalogLoader = MetadataService.getCatalogMetadata(entry.contentId, entry.path).then((metadata) async {
_catalogLoader = MetadataService.getCatalogMetadata(entry.contentId, entry.path);//.then(addAddressToMetadata);
_metadataLoader = MetadataService.getAllMetadata(entry.path);
}
Future<CatalogMetadata> addAddressToMetadata(metadata) async {
if (metadata == null) return null;
final latitude = metadata.latitude;
final longitude = metadata.longitude;
if (latitude != null && longitude != null) {
final coordinates = Coordinates(latitude, longitude);
try {
final addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
if (addresses != null && addresses.length > 0) {
metadata.address = addresses.first;
}
} catch (e) {
debugPrint('$runtimeType addAddressToMetadata failed with exception=${e.message}');
}
}
return metadata;
});
_metadataLoader = MetadataService.getAllMetadata(entry.path);
}
@override
@ -104,8 +111,8 @@ class InfoPageState extends State<InfoPage> {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
..._buildLocationSection(metadata.latitude, metadata.longitude, metadata.address),
..._buildTagSection(metadata.keywords),
..._buildLocationSection(metadata?.latitude, metadata?.longitude, metadata?.address),
..._buildTagSection(metadata?.xmpSubjects),
],
);
},
@ -180,12 +187,12 @@ class InfoPageState extends State<InfoPage> {
];
}
List<Widget> _buildTagSection(String keywords) {
if (keywords == null) return [];
List<Widget> _buildTagSection(String xmpSubjects) {
if (xmpSubjects == null) return [];
return [
SectionRow('XMP Tags'),
Wrap(
children: keywords
children: xmpSubjects
.split(' ')
.where((word) => word.isNotEmpty)
.map((word) => Padding(