diff --git a/lib/model/collection_source.dart b/lib/model/collection_source.dart index 38ce4bee0..bb13c351c 100644 --- a/lib/model/collection_source.dart +++ b/lib/model/collection_source.dart @@ -144,6 +144,7 @@ class CollectionSource { } void removeEntries(Iterable entries) async { + entries.forEach((entry) => entry.removeFromFavourites()); _rawEntries.removeWhere(entries.contains); eventBus.fire(EntryRemovedEvent(entries)); } diff --git a/lib/model/favourite_repo.dart b/lib/model/favourite_repo.dart index 4db0f6f5c..fbe9d08aa 100644 --- a/lib/model/favourite_repo.dart +++ b/lib/model/favourite_repo.dart @@ -17,14 +17,16 @@ class FavouriteRepo { bool isFavourite(ImageEntry entry) => _rows.any((row) => row.contentId == entry.contentId); - Future add(ImageEntry entry) async { - final newRows = [FavouriteRow(contentId: entry.contentId, path: entry.path)]; + FavouriteRow _entryToRow(ImageEntry entry) => FavouriteRow(contentId: entry.contentId, path: entry.path); + + Future add(Iterable entries) async { + final newRows = entries.map(_entryToRow); await metadataDb.addFavourites(newRows); _rows.addAll(newRows); } - Future remove(ImageEntry entry) async { - final removedRows = [FavouriteRow(contentId: entry.contentId, path: entry.path)]; + Future remove(Iterable entries) async { + final removedRows = entries.map(_entryToRow); await metadataDb.removeFavourites(removedRows); removedRows.forEach(_rows.remove); } diff --git a/lib/model/image_entry.dart b/lib/model/image_entry.dart index 830ba1897..edd3baa15 100644 --- a/lib/model/image_entry.dart +++ b/lib/model/image_entry.dart @@ -335,10 +335,23 @@ class ImageEntry { void toggleFavourite() { if (isFavourite) { - favourites.remove(this); + removeFromFavourites(); } else { - favourites.add(this); + addToFavourites(); + } + } + + void addToFavourites() { + if (!isFavourite) { + favourites.add([this]); + isFavouriteNotifier.value = true; + } + } + + void removeFromFavourites() { + if (isFavourite) { + favourites.remove([this]); + isFavouriteNotifier.value = false; } - isFavouriteNotifier.value = !isFavouriteNotifier.value; } } diff --git a/lib/widgets/common/action_delegates/selection_action_delegate.dart b/lib/widgets/common/action_delegates/selection_action_delegate.dart index e4660279b..6c1f277d6 100644 --- a/lib/widgets/common/action_delegates/selection_action_delegate.dart +++ b/lib/widgets/common/action_delegates/selection_action_delegate.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:aves/model/collection_lens.dart'; import 'package:aves/model/filters/album.dart'; import 'package:aves/model/image_entry.dart'; +import 'package:aves/model/metadata_db.dart'; import 'package:aves/services/android_app_service.dart'; import 'package:aves/services/image_file_service.dart'; import 'package:aves/widgets/album/app_bar.dart'; @@ -98,20 +99,30 @@ class SelectionActionDelegate with PermissionAwareMixin { } if (movedCount > 0) { if (copy) { - collection.source.addAll(movedOps.map((movedOp) { + final newEntries = []; + final newFavs = []; + movedOps.forEach((movedOp) { final sourceUri = movedOp.uri; final newFields = movedOp.newFields; final sourceEntry = selection.firstWhere((entry) => entry.uri == sourceUri, orElse: () => null); - return sourceEntry?.copyWith( + final copy = sourceEntry?.copyWith( uri: newFields['uri'] as String, path: newFields['path'] as String, contentId: newFields['contentId'] as int, ); - })); + newEntries.add(copy); + if (sourceEntry.isFavourite) { + newFavs.add(copy); + } + }); + collection.source.addAll(newEntries); + metadataDb.saveMetadata(newEntries.map((entry) => entry.catalogMetadata)); + metadataDb.saveAddresses(newEntries.map((entry) => entry.addressDetails)); + newFavs.forEach((entry) => entry.addToFavourites()); } else { // TODO TLAD update old entries path/dir/ID + // TODO TLAD update DB for catalog/address/fav } - // TODO TLAD update DB for catalog/address/fav } collection.clearSelection(); collection.browse(); diff --git a/lib/widgets/fullscreen/fullscreen_body.dart b/lib/widgets/fullscreen/fullscreen_body.dart index 0e67bba8a..b813247a5 100644 --- a/lib/widgets/fullscreen/fullscreen_body.dart +++ b/lib/widgets/fullscreen/fullscreen_body.dart @@ -413,12 +413,12 @@ class _FullscreenVerticalPageViewState extends State void _registerWidget(FullscreenVerticalPageView widget) { widget.verticalPager.addListener(_onVerticalPageControllerChanged); - widget.entry.imageChangeNotifier.addListener(_onImageChanged); + widget.entry?.imageChangeNotifier?.addListener(_onImageChanged); } void _unregisterWidget(FullscreenVerticalPageView widget) { widget.verticalPager.removeListener(_onVerticalPageControllerChanged); - widget.entry.imageChangeNotifier.removeListener(_onImageChanged); + widget.entry?.imageChangeNotifier?.removeListener(_onImageChanged); } @override