From a09e91084067d1af396e64de851987e4f75c98aa Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Tue, 2 Jun 2020 15:59:27 +0900 Subject: [PATCH] fixed source/lens after delete/move/copy --- lib/model/collection_lens.dart | 18 ++++++---- lib/model/collection_source.dart | 18 ++++++++-- .../selection_action_delegate.dart | 33 +++++++++---------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/lib/model/collection_lens.dart b/lib/model/collection_lens.dart index b4929d73d..563eeba54 100644 --- a/lib/model/collection_lens.dart +++ b/lib/model/collection_lens.dart @@ -150,20 +150,20 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin, CollectionSel case SortFactor.date: switch (groupFactor) { case GroupFactor.album: - sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.directory)); + sections = groupBy(_filteredEntries, (entry) => entry.directory); break; case GroupFactor.month: - sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.monthTaken)); + sections = groupBy(_filteredEntries, (entry) => entry.monthTaken); break; case GroupFactor.day: - sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.dayTaken)); + sections = groupBy(_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 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(); } diff --git a/lib/model/collection_source.dart b/lib/model/collection_source.dart index 60ca731a2..206fb9381 100644 --- a/lib/model/collection_source.dart +++ b/lib/model/collection_source.dart @@ -152,17 +152,29 @@ class CollectionSource { void removeEntries(Iterable 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 entries) { + void applyMove({ + @required Iterable entries, + @required Set 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 albums) { + void _cleanEmptyAlbums(Set albums) { final emptyAlbums = albums.where(_isEmptyAlbum); if (emptyAlbums.isNotEmpty) { _folderPaths.removeAll(emptyAlbums); diff --git a/lib/widgets/common/action_delegates/selection_action_delegate.dart b/lib/widgets/common/action_delegates/selection_action_delegate.dart index d02dd9f5a..da0e8baaa 100644 --- a/lib/widgets/common/action_delegates/selection_action_delegate.dart +++ b/lib/widgets/common/action_delegates/selection_action_delegate.dart @@ -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( @@ -75,7 +74,6 @@ class SelectionActionDelegate with PermissionAwareMixin { builder: (context) => CreateAlbumDialog(), ); if (newAlbum != null && newAlbum.isNotEmpty) { - isNewAlbum = true; Navigator.pop(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 = {}; + final movedEntries = []; 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 = []; - final fromAlbums = {}; 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();