214 lines
7.1 KiB
Dart
214 lines
7.1 KiB
Dart
import 'dart:collection';
|
|
|
|
import 'package:aves/model/image_entry.dart';
|
|
import 'package:aves/model/image_file_service.dart';
|
|
import 'package:aves/model/image_metadata.dart';
|
|
import 'package:aves/model/metadata_db.dart';
|
|
import 'package:collection/collection.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:path/path.dart';
|
|
|
|
class ImageCollection with ChangeNotifier {
|
|
final List<ImageEntry> _rawEntries;
|
|
Map<dynamic, List<ImageEntry>> sections = Map.unmodifiable({});
|
|
GroupFactor groupFactor = GroupFactor.month;
|
|
SortFactor sortFactor = SortFactor.date;
|
|
List<String> sortedAlbums = List.unmodifiable(const Iterable.empty());
|
|
List<String> sortedTags = List.unmodifiable(const Iterable.empty());
|
|
|
|
ImageCollection({
|
|
@required List<ImageEntry> entries,
|
|
this.groupFactor,
|
|
this.sortFactor,
|
|
}) : _rawEntries = entries {
|
|
if (_rawEntries.isNotEmpty) updateSections();
|
|
}
|
|
|
|
int get imageCount => _rawEntries.where((entry) => !entry.isVideo).length;
|
|
|
|
int get videoCount => _rawEntries.where((entry) => entry.isVideo).length;
|
|
|
|
int get albumCount => sortedAlbums.length;
|
|
|
|
int get tagCount => sortedTags.length;
|
|
|
|
List<ImageEntry> get sortedEntries => List.unmodifiable(sections.entries.expand((e) => e.value));
|
|
|
|
void sort(SortFactor sortFactor) {
|
|
this.sortFactor = sortFactor;
|
|
updateSections();
|
|
}
|
|
|
|
void group(GroupFactor groupFactor) {
|
|
this.groupFactor = groupFactor;
|
|
updateSections();
|
|
}
|
|
|
|
void updateSections() {
|
|
_applySort();
|
|
switch (sortFactor) {
|
|
case SortFactor.date:
|
|
switch (groupFactor) {
|
|
case GroupFactor.album:
|
|
sections = Map.unmodifiable(groupBy(_rawEntries, (entry) => entry.directory));
|
|
break;
|
|
case GroupFactor.month:
|
|
sections = Map.unmodifiable(groupBy(_rawEntries, (entry) => entry.monthTaken));
|
|
break;
|
|
case GroupFactor.day:
|
|
sections = Map.unmodifiable(groupBy(_rawEntries, (entry) => entry.dayTaken));
|
|
break;
|
|
}
|
|
break;
|
|
case SortFactor.size:
|
|
sections = Map.unmodifiable(Map.fromEntries([
|
|
MapEntry(null, _rawEntries),
|
|
]));
|
|
break;
|
|
case SortFactor.name:
|
|
final byAlbum = groupBy(_rawEntries, (ImageEntry entry) => entry.directory);
|
|
final albums = byAlbum.keys.toSet();
|
|
final compare = (a, b) {
|
|
final ua = getUniqueAlbumName(a, albums);
|
|
final ub = getUniqueAlbumName(b, albums);
|
|
return compareAsciiUpperCase(ua, ub);
|
|
};
|
|
sections = Map.unmodifiable(SplayTreeMap.from(byAlbum, compare));
|
|
break;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
void _applySort() {
|
|
switch (sortFactor) {
|
|
case SortFactor.date:
|
|
_rawEntries.sort((a, b) => b.bestDate.compareTo(a.bestDate));
|
|
break;
|
|
case SortFactor.size:
|
|
_rawEntries.sort((a, b) => b.sizeBytes.compareTo(a.sizeBytes));
|
|
break;
|
|
case SortFactor.name:
|
|
_rawEntries.sort((a, b) => compareAsciiUpperCase(a.title, b.title));
|
|
break;
|
|
}
|
|
}
|
|
|
|
void add(ImageEntry entry) => _rawEntries.add(entry);
|
|
|
|
Future<bool> delete(ImageEntry entry) async {
|
|
final success = await ImageFileService.delete(entry);
|
|
if (success) {
|
|
_rawEntries.remove(entry);
|
|
updateSections();
|
|
}
|
|
return success;
|
|
}
|
|
|
|
void updateAlbums() {
|
|
final albums = _rawEntries.map((entry) => entry.directory).toSet();
|
|
final sorted = albums.toList()
|
|
..sort((a, b) {
|
|
final ua = getUniqueAlbumName(a, albums);
|
|
final ub = getUniqueAlbumName(b, albums);
|
|
return compareAsciiUpperCase(ua, ub);
|
|
});
|
|
sortedAlbums = List.unmodifiable(sorted);
|
|
}
|
|
|
|
void updateTags() {
|
|
final tags = _rawEntries.expand((entry) => entry.xmpSubjects).toSet();
|
|
final sorted = tags.toList()..sort(compareAsciiUpperCase);
|
|
sortedTags = List.unmodifiable(sorted);
|
|
}
|
|
|
|
void onMetadataChanged() {
|
|
// metadata dates impact sorting and grouping
|
|
updateSections();
|
|
updateTags();
|
|
}
|
|
|
|
Future<void> loadCatalogMetadata() async {
|
|
final stopwatch = Stopwatch()..start();
|
|
final saved = await metadataDb.loadMetadataEntries();
|
|
_rawEntries.forEach((entry) {
|
|
final contentId = entry.contentId;
|
|
if (contentId != null) {
|
|
entry.catalogMetadata = saved.firstWhere((metadata) => metadata.contentId == contentId, orElse: () => null);
|
|
}
|
|
});
|
|
debugPrint('$runtimeType loadCatalogMetadata complete in ${stopwatch.elapsed.inMilliseconds}ms with ${saved.length} saved entries');
|
|
onMetadataChanged();
|
|
}
|
|
|
|
Future<void> loadAddresses() async {
|
|
final stopwatch = Stopwatch()..start();
|
|
final saved = await metadataDb.loadAddresses();
|
|
_rawEntries.forEach((entry) {
|
|
final contentId = entry.contentId;
|
|
if (contentId != null) {
|
|
entry.addressDetails = saved.firstWhere((address) => address.contentId == contentId, orElse: () => null);
|
|
}
|
|
});
|
|
debugPrint('$runtimeType loadAddresses complete in ${stopwatch.elapsed.inMilliseconds}ms with ${saved.length} saved entries');
|
|
}
|
|
|
|
Future<void> catalogEntries() async {
|
|
final stopwatch = Stopwatch()..start();
|
|
final uncataloguedEntries = _rawEntries.where((entry) => !entry.isCatalogued).toList();
|
|
if (uncataloguedEntries.isEmpty) return;
|
|
|
|
final newMetadata = <CatalogMetadata>[];
|
|
await Future.forEach<ImageEntry>(uncataloguedEntries, (entry) async {
|
|
await entry.catalog();
|
|
if (entry.isCatalogued) {
|
|
newMetadata.add(entry.catalogMetadata);
|
|
}
|
|
});
|
|
if (newMetadata.isEmpty) return;
|
|
|
|
await metadataDb.saveMetadata(List.unmodifiable(newMetadata));
|
|
onMetadataChanged();
|
|
debugPrint('$runtimeType catalogEntries complete in ${stopwatch.elapsed.inSeconds}s with ${newMetadata.length} new entries');
|
|
}
|
|
|
|
Future<void> locateEntries() async {
|
|
final stopwatch = Stopwatch()..start();
|
|
final unlocatedEntries = _rawEntries.where((entry) => entry.hasGps && !entry.isLocated).toList();
|
|
final newAddresses = <AddressDetails>[];
|
|
await Future.forEach<ImageEntry>(unlocatedEntries, (entry) async {
|
|
await entry.locate();
|
|
if (entry.isLocated) {
|
|
newAddresses.add(entry.addressDetails);
|
|
if (newAddresses.length >= 50) {
|
|
await metadataDb.saveAddresses(List.unmodifiable(newAddresses));
|
|
newAddresses.clear();
|
|
}
|
|
}
|
|
});
|
|
await metadataDb.saveAddresses(List.unmodifiable(newAddresses));
|
|
debugPrint('$runtimeType locateEntries complete in ${stopwatch.elapsed.inMilliseconds}ms');
|
|
}
|
|
|
|
ImageCollection filter(bool Function(ImageEntry) filter) {
|
|
return ImageCollection(
|
|
entries: _rawEntries.where(filter).toList(),
|
|
groupFactor: groupFactor,
|
|
sortFactor: sortFactor,
|
|
);
|
|
}
|
|
|
|
String getUniqueAlbumName(String album, Iterable<String> albums) {
|
|
final otherAlbums = albums.where((item) => item != album);
|
|
final parts = album.split(separator);
|
|
int partCount = 0;
|
|
String testName;
|
|
do {
|
|
testName = separator + parts.skip(parts.length - ++partCount).join(separator);
|
|
} while (otherAlbums.any((item) => item.endsWith(testName)));
|
|
return parts.skip(parts.length - partCount).join(separator);
|
|
}
|
|
}
|
|
|
|
enum SortFactor { date, size, name }
|
|
|
|
enum GroupFactor { album, month, day }
|