fixed source/lens after delete/move/copy

This commit is contained in:
Thibault Deckers 2020-06-02 15:59:27 +09:00
parent 209bb70f03
commit a09e910840
3 changed files with 42 additions and 27 deletions

View file

@ -150,20 +150,20 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin, CollectionSel
case SortFactor.date:
switch (groupFactor) {
case GroupFactor.album:
sections = Map.unmodifiable(groupBy<ImageEntry, String>(_filteredEntries, (entry) => entry.directory));
sections = groupBy<ImageEntry, String>(_filteredEntries, (entry) => entry.directory);
break;
case GroupFactor.month:
sections = Map.unmodifiable(groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.monthTaken));
sections = groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.monthTaken);
break;
case GroupFactor.day:
sections = Map.unmodifiable(groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.dayTaken));
sections = groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.dayTaken);
break;
}
break;
case SortFactor.size:
sections = Map.unmodifiable(Map.fromEntries([
sections = Map.fromEntries([
MapEntry(null, _filteredEntries),
]));
]);
break;
case SortFactor.name:
final byAlbum = groupBy(_filteredEntries, (ImageEntry entry) => entry.directory);
@ -172,9 +172,10 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin, CollectionSel
final ub = source.getUniqueAlbumName(b);
return compareAsciiUpperCase(ua, ub);
};
sections = Map.unmodifiable(SplayTreeMap.of(byAlbum, compare));
sections = SplayTreeMap.of(byAlbum, compare);
break;
}
sections = Map.unmodifiable(sections);
_sortedEntries = null;
notifyListeners();
}
@ -188,10 +189,13 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin, CollectionSel
}
void onEntryRemoved(Iterable<ImageEntry> entries) {
// do not apply sort/group as section order change would surprise the user while browsing
// we should remove obsolete entries and sections
// but do not apply sort/group
// as section order change would surprise the user while browsing
_filteredEntries.removeWhere(entries.contains);
_sortedEntries?.removeWhere(entries.contains);
sections.forEach((key, sectionEntries) => sectionEntries.removeWhere(entries.contains));
sections = Map.unmodifiable(Map.fromEntries(sections.entries.where((kv) => kv.value.isNotEmpty)));
selection.removeAll(entries);
notifyListeners();
}

View file

@ -152,17 +152,29 @@ class CollectionSource {
void removeEntries(Iterable<ImageEntry> entries) async {
entries.forEach((entry) => entry.removeFromFavourites());
_rawEntries.removeWhere(entries.contains);
cleanEmptyAlbums(entries.map((entry) => entry.directory).toSet());
_cleanEmptyAlbums(entries.map((entry) => entry.directory).toSet());
_filterEntryCountMap.clear();
eventBus.fire(EntryRemovedEvent(entries));
}
void notifyMovedEntries(Iterable<ImageEntry> entries) {
void applyMove({
@required Iterable<ImageEntry> entries,
@required Set<String> fromAlbums,
@required String toAlbum,
@required bool copy,
}) {
if (copy) {
addAll(entries);
} else {
_cleanEmptyAlbums(fromAlbums);
_folderPaths.add(toAlbum);
}
updateAlbums();
_filterEntryCountMap.clear();
eventBus.fire(EntryMovedEvent(entries));
}
void cleanEmptyAlbums(Set<String> albums) {
void _cleanEmptyAlbums(Set<String> albums) {
final emptyAlbums = albums.where(_isEmptyAlbum);
if (emptyAlbums.isNotEmpty) {
_folderPaths.removeAll(emptyAlbums);

View file

@ -56,7 +56,6 @@ class SelectionActionDelegate with PermissionAwareMixin {
Future _moveSelection(BuildContext context, {@required bool copy}) async {
final source = collection.source;
var isNewAlbum = false;
final destinationAlbum = await Navigator.push(
context,
MaterialPageRoute<String>(
@ -75,7 +74,6 @@ class SelectionActionDelegate with PermissionAwareMixin {
builder: (context) => CreateAlbumDialog(),
);
if (newAlbum != null && newAlbum.isNotEmpty) {
isNewAlbum = true;
Navigator.pop<String>(context, newAlbum);
}
},
@ -113,23 +111,23 @@ class SelectionActionDelegate with PermissionAwareMixin {
_showFeedback(context, '${copy ? 'Copied' : 'Moved'} ${Intl.plural(count, one: '${count} item', other: '${count} items')}');
}
if (movedCount > 0) {
final fromAlbums = <String>{};
final movedEntries = <ImageEntry>[];
if (copy) {
final newEntries = movedOps.map((movedOp) {
movedOps.forEach((movedOp) {
final sourceUri = movedOp.uri;
final newFields = movedOp.newFields;
final sourceEntry = selection.firstWhere((entry) => entry.uri == sourceUri, orElse: () => null);
return sourceEntry?.copyWith(
fromAlbums.add(sourceEntry.directory);
movedEntries.add(sourceEntry?.copyWith(
uri: newFields['uri'] as String,
path: newFields['path'] as String,
contentId: newFields['contentId'] as int,
);
}).toList();
source.addAll(newEntries);
metadataDb.saveMetadata(newEntries.map((entry) => entry.catalogMetadata));
metadataDb.saveAddresses(newEntries.map((entry) => entry.addressDetails));
));
});
metadataDb.saveMetadata(movedEntries.map((entry) => entry.catalogMetadata));
metadataDb.saveAddresses(movedEntries.map((entry) => entry.addressDetails));
} else {
final movedEntries = <ImageEntry>[];
final fromAlbums = <String>{};
movedOps.forEach((movedOp) {
final sourceUri = movedOp.uri;
final newFields = movedOp.newFields;
@ -141,19 +139,20 @@ class SelectionActionDelegate with PermissionAwareMixin {
entry.uri = newFields['uri'] as String;
entry.path = newFields['path'] as String;
entry.contentId = newContentId;
movedEntries.add(entry);
metadataDb.updateMetadataId(oldContentId, entry.catalogMetadata);
metadataDb.updateAddressId(oldContentId, entry.addressDetails);
metadataDb.updateFavouriteId(oldContentId, FavouriteRow(contentId: entry.contentId, path: entry.path));
}
movedEntries.add(entry);
});
source.cleanEmptyAlbums(fromAlbums);
source.notifyMovedEntries(movedEntries);
}
if (isNewAlbum) {
source.updateAlbums();
}
source.applyMove(
entries: movedEntries,
fromAlbums: fromAlbums,
toAlbum: destinationAlbum,
copy: copy,
);
}
collection.clearSelection();
collection.browse();