From b704d72c9d44f34f9ae90f984c5828e117b82509 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Mon, 5 Jul 2021 17:20:26 +0900 Subject: [PATCH] collection: fixed sort/group on settings change --- lib/model/source/collection_lens.dart | 60 +++++++++++-------- lib/widgets/collection/app_bar.dart | 2 - .../collection/entry_set_action_delegate.dart | 2 - lib/widgets/collection/grid/thumbnail.dart | 2 - lib/widgets/viewer/entry_action_delegate.dart | 2 - lib/widgets/viewer/entry_viewer_stack.dart | 2 - lib/widgets/viewer/video_action_delegate.dart | 2 - 7 files changed, 34 insertions(+), 38 deletions(-) diff --git a/lib/model/source/collection_lens.dart b/lib/model/source/collection_lens.dart index f3d23bbc2..f1f2c1fed 100644 --- a/lib/model/source/collection_lens.dart +++ b/lib/model/source/collection_lens.dart @@ -35,27 +35,27 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin { CollectionLens({ required this.source, Iterable? filters, - EntryGroupFactor? groupFactor, - EntrySortFactor? sortFactor, this.id, this.listenToSource = true, }) : filters = (filters ?? {}).where((f) => f != null).cast().toSet(), - groupFactor = groupFactor ?? settings.collectionGroupFactor, - sortFactor = sortFactor ?? settings.collectionSortFactor { + groupFactor = settings.collectionGroupFactor, + sortFactor = settings.collectionSortFactor { id ??= hashCode; if (listenToSource) { - _subscriptions.add(source.eventBus.on().listen((e) => onEntryAdded(e.entries))); - _subscriptions.add(source.eventBus.on().listen((e) => onEntryRemoved(e.entries))); - _subscriptions.add(source.eventBus.on().listen((e) => _refresh())); - _subscriptions.add(source.eventBus.on().listen((e) => _refresh())); - _subscriptions.add(source.eventBus.on().listen((e) => _refresh())); - _subscriptions.add(source.eventBus.on().listen((e) { + final sourceEvents = source.eventBus; + _subscriptions.add(sourceEvents.on().listen((e) => onEntryAdded(e.entries))); + _subscriptions.add(sourceEvents.on().listen((e) => onEntryRemoved(e.entries))); + _subscriptions.add(sourceEvents.on().listen((e) => _refresh())); + _subscriptions.add(sourceEvents.on().listen((e) => _refresh())); + _subscriptions.add(sourceEvents.on().listen((e) => _refresh())); + _subscriptions.add(sourceEvents.on().listen((e) { if (this.filters.any((filter) => filter is LocationFilter)) { _refresh(); } })); - favourites.addListener(onFavouritesChanged); + favourites.addListener(_onFavouritesChanged); } + settings.addListener(_onSettingsChanged); _refresh(); } @@ -64,7 +64,8 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin { _subscriptions ..forEach((sub) => sub.cancel()) ..clear(); - favourites.removeListener(onFavouritesChanged); + favourites.removeListener(_onFavouritesChanged); + settings.addListener(_onSettingsChanged); super.dispose(); } @@ -112,19 +113,6 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin { filterChangeNotifier.notifyListeners(); } - void sort(EntrySortFactor sortFactor) { - this.sortFactor = sortFactor; - _applySort(); - _applyGroup(); - sortGroupChangeNotifier.notifyListeners(); - } - - void group(EntryGroupFactor groupFactor) { - this.groupFactor = groupFactor; - _applyGroup(); - sortGroupChangeNotifier.notifyListeners(); - } - void _applyFilters() { final entries = source.visibleEntries; _filteredSortedEntries = List.of(filters.isEmpty ? entries : entries.where((entry) => filters.every((filter) => filter.test(entry)))); @@ -187,12 +175,32 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin { _applyGroup(); } - void onFavouritesChanged() { + void _onFavouritesChanged() { if (filters.any((filter) => filter is FavouriteFilter)) { _refresh(); } } + void _onSettingsChanged() { + final newSortFactor = settings.collectionSortFactor; + final newGroupFactor = settings.collectionGroupFactor; + + final needSort = sortFactor != newSortFactor; + final needGroup = needSort || groupFactor != newGroupFactor; + + if (needSort) { + sortFactor = newSortFactor; + _applySort(); + } + if (needGroup) { + groupFactor = newGroupFactor; + _applyGroup(); + } + if (needSort || needGroup) { + sortGroupChangeNotifier.notifyListeners(); + } + } + void onEntryAdded(Set? entries) { _refresh(); } diff --git a/lib/widgets/collection/app_bar.dart b/lib/widgets/collection/app_bar.dart index 09089e7ca..8f4f4a078 100644 --- a/lib/widgets/collection/app_bar.dart +++ b/lib/widgets/collection/app_bar.dart @@ -321,7 +321,6 @@ class _CollectionAppBarState extends State with SingleTickerPr await Future.delayed(Durations.dialogTransitionAnimation * timeDilation); if (value != null) { settings.collectionGroupFactor = value; - collection.group(value); } break; case CollectionAction.sort: @@ -341,7 +340,6 @@ class _CollectionAppBarState extends State with SingleTickerPr await Future.delayed(Durations.dialogTransitionAnimation * timeDilation); if (value != null) { settings.collectionSortFactor = value; - collection.sort(value); } break; } diff --git a/lib/widgets/collection/entry_set_action_delegate.dart b/lib/widgets/collection/entry_set_action_delegate.dart index 4acfbbfdd..6175fb0cd 100644 --- a/lib/widgets/collection/entry_set_action_delegate.dart +++ b/lib/widgets/collection/entry_set_action_delegate.dart @@ -148,8 +148,6 @@ class EntrySetActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAware targetCollection = CollectionLens( source: collection.source, filters: collection.filters, - groupFactor: collection.groupFactor, - sortFactor: collection.sortFactor, )..addFilter(filter); unawaited(Navigator.pushReplacement( context, diff --git a/lib/widgets/collection/grid/thumbnail.dart b/lib/widgets/collection/grid/thumbnail.dart index 65e9a5e33..79e816b0c 100644 --- a/lib/widgets/collection/grid/thumbnail.dart +++ b/lib/widgets/collection/grid/thumbnail.dart @@ -71,8 +71,6 @@ class InteractiveThumbnail extends StatelessWidget { final viewerCollection = CollectionLens( source: collection.source, filters: collection.filters, - groupFactor: collection.groupFactor, - sortFactor: collection.sortFactor, id: collection.id, listenToSource: false, ); diff --git a/lib/widgets/viewer/entry_action_delegate.dart b/lib/widgets/viewer/entry_action_delegate.dart index 05fbe3821..fa5b8ba86 100644 --- a/lib/widgets/viewer/entry_action_delegate.dart +++ b/lib/widgets/viewer/entry_action_delegate.dart @@ -221,8 +221,6 @@ class EntryActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAwareMix final targetCollection = CollectionLens( source: source, filters: {AlbumFilter(destinationAlbum, source.getAlbumDisplayName(context, destinationAlbum))}, - groupFactor: _collection.groupFactor, - sortFactor: _collection.sortFactor, ); unawaited(Navigator.pushAndRemoveUntil( context, diff --git a/lib/widgets/viewer/entry_viewer_stack.dart b/lib/widgets/viewer/entry_viewer_stack.dart index ad5678289..e2f38a1e4 100644 --- a/lib/widgets/viewer/entry_viewer_stack.dart +++ b/lib/widgets/viewer/entry_viewer_stack.dart @@ -401,8 +401,6 @@ class _EntryViewerStackState extends State with SingleTickerPr collection: CollectionLens( source: baseCollection.source, filters: baseCollection.filters, - groupFactor: baseCollection.groupFactor, - sortFactor: baseCollection.sortFactor, )..addFilter(filter), ); }, diff --git a/lib/widgets/viewer/video_action_delegate.dart b/lib/widgets/viewer/video_action_delegate.dart index a6a5c99d2..d02a28634 100644 --- a/lib/widgets/viewer/video_action_delegate.dart +++ b/lib/widgets/viewer/video_action_delegate.dart @@ -96,8 +96,6 @@ class VideoActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAwareMix final targetCollection = CollectionLens( source: source, filters: {AlbumFilter(destinationAlbum, source.getAlbumDisplayName(context, destinationAlbum))}, - groupFactor: _collection.groupFactor, - sortFactor: _collection.sortFactor, ); unawaited(Navigator.pushAndRemoveUntil( context,