From 4707feeaa3b2eaa267de9031558909e8a09db403 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:41:59 +0700 Subject: [PATCH] refactor(library): share the collection selection bar toggle Album, playlist and artist pages each rebuilt the same select all toggle around SelectionBottomBar; SelectionModeMixin.buildSelectionBar now does it once. The Library selection bar's mobile delete reuses DestructiveSelectionButton with its own label. --- lib/controllers/selection_mode_mixin.dart | 27 ++++ lib/screens/album_screen.dart | 33 ++--- lib/screens/artist_screen.dart | 18 +-- lib/screens/playlist_screen.dart | 33 ++--- lib/widgets/destructive_selection_button.dart | 11 +- lib/widgets/library_track_selection_bar.dart | 27 +--- test/artist_album_selection_test.dart | 21 +++ test/collection_selection_bar_test.dart | 128 ++++++++++++++++++ 8 files changed, 209 insertions(+), 89 deletions(-) create mode 100644 test/collection_selection_bar_test.dart diff --git a/lib/controllers/selection_mode_mixin.dart b/lib/controllers/selection_mode_mixin.dart index cfe69dfb5..350dc734c 100644 --- a/lib/controllers/selection_mode_mixin.dart +++ b/lib/controllers/selection_mode_mixin.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:mornye/widgets/desktop/desktop_selection.dart'; +import 'package:mornye/widgets/selection_bottom_bar.dart'; /// Shared multi-select state for list screens: enter/exit selection mode, /// toggle individual items, select all, and prune ids that fell out of the @@ -105,6 +106,32 @@ mixin SelectionModeMixin on State { }); } + /// The selection bar over [ids], every selectable item in display order. + /// Toggling with everything selected calls [onDeselectAll], which exits + /// selection mode by default. + Widget buildSelectionBar({ + required List ids, + required double bottomPadding, + required List children, + VoidCallback? onDeselectAll, + String? allSelectedLabel, + String? tapToSelectLabel, + }) { + final allSelected = ids.isNotEmpty && selectedIds.length == ids.length; + return SelectionBottomBar( + selectedCount: selectedIds.length, + allSelected: allSelected, + onClose: exitSelectionMode, + onToggleSelectAll: allSelected + ? onDeselectAll ?? exitSelectionMode + : () => selectAll(ids), + bottomPadding: bottomPadding, + allSelectedLabel: allSelectedLabel, + tapToSelectLabel: tapToSelectLabel, + children: children, + ); + } + /// Drops ids no longer present in [validIds]; call from build() with the /// current item ids. Exits selection mode next frame if that empties it. void pruneSelection(Set validIds) { diff --git a/lib/screens/album_screen.dart b/lib/screens/album_screen.dart index db6791db5..e7da3bcac 100644 --- a/lib/screens/album_screen.dart +++ b/lib/screens/album_screen.dart @@ -39,7 +39,6 @@ import 'package:mornye/widgets/audio_quality_badges.dart'; import 'package:mornye/widgets/motion_header_banner.dart'; import 'package:mornye/widgets/track_detail_actions.dart'; import 'package:mornye/widgets/selection_action_button.dart'; -import 'package:mornye/widgets/selection_bottom_bar.dart'; import 'package:mornye/widgets/downloadable_cover.dart'; import 'package:mornye/widgets/disc_separator_chip.dart'; import 'package:mornye/widgets/mornye_artist_header.dart'; @@ -454,10 +453,11 @@ class _AlbumScreenState extends ConsumerState ? MediaQuery.paddingOf(context).bottom : 0.0; - pruneSelection({ + final ids = [ for (var index = 0; index < tracks.length; index++) _trackSelectionId(tracks[index], index), - }); + ]; + pruneSelection(ids.toSet()); final scaffold = CollectionScaffold( scrollController: scrollController, @@ -468,6 +468,7 @@ class _AlbumScreenState extends ConsumerState context, colorScheme, tracks, + ids, bottomPadding, ), appBar: _buildAppBar( @@ -507,12 +508,7 @@ class _AlbumScreenState extends ConsumerState ], ); return DesktopListScope( - onSelectAll: tracks.isEmpty - ? null - : () => selectAll([ - for (var index = 0; index < tracks.length; index++) - _trackSelectionId(tracks[index], index), - ]), + onSelectAll: ids.isEmpty ? null : () => selectAll(ids), onClearSelection: isSelectionMode ? exitSelectionMode : null, onActivate: () => _desktopActivations[selectionAnchorId]?.call(), child: scaffold, @@ -817,25 +813,12 @@ class _AlbumScreenState extends ConsumerState BuildContext barContext, ColorScheme colorScheme, List tracks, + List ids, double bottomPadding, ) { final selectedCount = selectedIds.length; - final allSelected = tracks.isNotEmpty && selectedCount == tracks.length; - - return SelectionBottomBar( - selectedCount: selectedCount, - allSelected: allSelected, - onClose: exitSelectionMode, - onToggleSelectAll: () { - if (allSelected) { - exitSelectionMode(); - } else { - selectAll([ - for (var index = 0; index < tracks.length; index++) - _trackSelectionId(tracks[index], index), - ]); - } - }, + return buildSelectionBar( + ids: ids, bottomPadding: bottomPadding, children: [ SizedBox( diff --git a/lib/screens/artist_screen.dart b/lib/screens/artist_screen.dart index c9dd2d276..c3048ebbd 100644 --- a/lib/screens/artist_screen.dart +++ b/lib/screens/artist_screen.dart @@ -823,20 +823,12 @@ class _ArtistScreenState extends ConsumerState super.toggleSelection(itemId); } - void _deselectAll() { - setState(() { - selectedIds.clear(); - }); - } - Widget _buildSelectionBar( BuildContext context, ColorScheme colorScheme, List allAlbums, double bottomPadding, ) { - final allSelected = - selectedIds.length == allAlbums.length && allAlbums.isNotEmpty; final selectedCount = selectedIds.length; final selectedAlbums = allAlbums .where((a) => selectedIds.contains(a.id)) @@ -846,13 +838,9 @@ class _ArtistScreenState extends ConsumerState (sum, a) => sum + a.totalTracks, ); - return SelectionBottomBar( - selectedCount: selectedCount, - allSelected: allSelected, - onClose: exitSelectionMode, - onToggleSelectAll: allSelected - ? _deselectAll - : () => selectAll(allAlbums.map((a) => a.id)), + return buildSelectionBar( + ids: [for (final album in allAlbums) album.id], + onDeselectAll: () => setState(selectedIds.clear), bottomPadding: bottomPadding, allSelectedLabel: context.l10n.tracksCount(totalTracks), tapToSelectLabel: selectedCount > 0 diff --git a/lib/screens/playlist_screen.dart b/lib/screens/playlist_screen.dart index 78eff41d9..3ae4d0cd2 100644 --- a/lib/screens/playlist_screen.dart +++ b/lib/screens/playlist_screen.dart @@ -4,7 +4,6 @@ import 'package:mornye/widgets/album_detail_header.dart'; import 'package:mornye/widgets/collection_header_actions.dart'; import 'package:mornye/screens/selection_mode_mixin.dart'; import 'package:mornye/widgets/collection_scaffold.dart'; -import 'package:mornye/widgets/selection_bottom_bar.dart'; import 'package:mornye/widgets/selection_action_button.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mornye/services/platform_bridge.dart'; @@ -89,25 +88,12 @@ class _PlaylistScreenState extends ConsumerState Widget _buildSelectionBottomBar( BuildContext context, ColorScheme colorScheme, + List ids, double bottomPadding, ) { final selectedCount = selectedIds.length; - final allSelected = _tracks.isNotEmpty && selectedCount == _tracks.length; - - return SelectionBottomBar( - selectedCount: selectedCount, - allSelected: allSelected, - onClose: exitSelectionMode, - onToggleSelectAll: () { - if (allSelected) { - exitSelectionMode(); - } else { - selectAll([ - for (var index = 0; index < _tracks.length; index++) - _trackSelectionId(_tracks[index], index), - ]); - } - }, + return buildSelectionBar( + ids: ids, bottomPadding: bottomPadding, children: [ SizedBox( @@ -279,18 +265,14 @@ class _PlaylistScreenState extends ConsumerState ? MediaQuery.paddingOf(context).bottom : 0.0; - pruneSelection({ + final ids = [ for (var index = 0; index < _tracks.length; index++) _trackSelectionId(_tracks[index], index), - }); + ]; + pruneSelection(ids.toSet()); return DesktopListScope( - onSelectAll: _tracks.isEmpty - ? null - : () => selectAll([ - for (var index = 0; index < _tracks.length; index++) - _trackSelectionId(_tracks[index], index), - ]), + onSelectAll: ids.isEmpty ? null : () => selectAll(ids), onClearSelection: isSelectionMode ? exitSelectionMode : null, onActivate: () => _desktopActivations[selectionAnchorId]?.call(), child: CollectionScaffold( @@ -300,6 +282,7 @@ class _PlaylistScreenState extends ConsumerState selectionBar: _buildSelectionBottomBar( context, colorScheme, + ids, bottomPadding, ), appBar: _buildAppBar(context, colorScheme), diff --git a/lib/widgets/destructive_selection_button.dart b/lib/widgets/destructive_selection_button.dart index 0e7ca8bed..9ec5c7398 100644 --- a/lib/widgets/destructive_selection_button.dart +++ b/lib/widgets/destructive_selection_button.dart @@ -11,12 +11,16 @@ class DestructiveSelectionButton extends StatelessWidget { required this.count, required this.colorScheme, required this.onPressed, + this.label, }); final int count; final ColorScheme colorScheme; final VoidCallback onPressed; + /// Replaces the downloaded album delete label. + final String? label; + @override Widget build(BuildContext context) { return SizedBox( @@ -26,9 +30,10 @@ class DestructiveSelectionButton extends StatelessWidget { onPressed: count > 0 ? onPressed : null, icon: const Icon(Icons.delete_outline), label: Text( - count > 0 - ? context.l10n.downloadedAlbumDeleteCount(count) - : context.l10n.downloadedAlbumSelectToDelete, + label ?? + (count > 0 + ? context.l10n.downloadedAlbumDeleteCount(count) + : context.l10n.downloadedAlbumSelectToDelete), ), style: FilledButton.styleFrom( backgroundColor: count > 0 diff --git a/lib/widgets/library_track_selection_bar.dart b/lib/widgets/library_track_selection_bar.dart index 4cb66068b..3418c08d3 100644 --- a/lib/widgets/library_track_selection_bar.dart +++ b/lib/widgets/library_track_selection_bar.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:mornye/l10n/l10n.dart'; import 'package:mornye/utils/desktop_platform.dart'; -import 'package:mornye/widgets/app_action_button.dart'; +import 'package:mornye/widgets/destructive_selection_button.dart'; import 'package:mornye/widgets/selection_action_button.dart'; import 'package:mornye/widgets/selection_bottom_bar.dart'; @@ -113,26 +113,11 @@ class LibraryTrackSelectionBar extends StatelessWidget { ), if (!isDesktopPlatform) ...[ const SizedBox(height: 8), - SizedBox( - width: double.infinity, - child: AppActionButton( - isDestructive: true, - onPressed: hasSelection ? onDelete : null, - icon: const Icon(Icons.delete_outline), - label: Text(deleteLabel), - style: FilledButton.styleFrom( - backgroundColor: hasSelection - ? colorScheme.error - : colorScheme.surfaceContainerHighest, - foregroundColor: hasSelection - ? colorScheme.onError - : colorScheme.onSurfaceVariant, - padding: const EdgeInsets.symmetric(vertical: 16), - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(16), - ), - ), - ), + DestructiveSelectionButton( + count: selectedCount, + colorScheme: colorScheme, + onPressed: onDelete, + label: deleteLabel, ), ], ], diff --git a/test/artist_album_selection_test.dart b/test/artist_album_selection_test.dart index b186212b6..e69a66c3c 100644 --- a/test/artist_album_selection_test.dart +++ b/test/artist_album_selection_test.dart @@ -251,5 +251,26 @@ void main() { _expectSelection(tester, {2, 3}); await _disposeArtist(tester); }); + + testWidgets('$style: select all toggle clears but keeps selection mode', ( + tester, + ) async { + debugDesktopPlatformOverride = false; + await _pumpArtist(tester, mornye: mornye); + await tester.longPress(_card(2)); + await tester.pumpAndSettle(); + SelectionBottomBar bar() => + tester.widget(find.byType(SelectionBottomBar)); + expect(bar().allSelected, isFalse); + bar().onToggleSelectAll(); + await tester.pumpAndSettle(); + _expectSelection(tester, {0, 1, 2, 3, 4}); + expect(bar().allSelected, isTrue); + bar().onToggleSelectAll(); + await tester.pumpAndSettle(); + _expectSelection(tester, {}); + expect(bar().allSelected, isFalse); + await _disposeArtist(tester); + }); } } diff --git a/test/collection_selection_bar_test.dart b/test/collection_selection_bar_test.dart new file mode 100644 index 000000000..4b3e73800 --- /dev/null +++ b/test/collection_selection_bar_test.dart @@ -0,0 +1,128 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/l10n/l10n.dart'; +import 'package:mornye/models/track.dart'; +import 'package:mornye/providers/download_queue_provider.dart'; +import 'package:mornye/providers/library_collections_provider.dart'; +import 'package:mornye/providers/music_player_provider.dart'; +import 'package:mornye/providers/recent_access_provider.dart'; +import 'package:mornye/providers/settings_provider.dart'; +import 'package:mornye/screens/album_screen.dart'; +import 'package:mornye/screens/playlist_screen.dart'; +import 'package:mornye/theme/app_theme.dart'; +import 'package:mornye/widgets/selection_bottom_bar.dart'; +import 'package:mornye/widgets/track_list_tile.dart'; + +import 'support/fixed_settings.dart'; + +final _tracks = [ + for (var i = 0; i < 4; i++) + Track( + id: 'example-track-$i', + name: 'Example song $i', + artistName: 'Example artist', + albumName: 'Example album', + duration: 180, + trackNumber: i + 1, + ), +]; + +class _Collections extends LibraryCollectionsNotifier { + @override + LibraryCollectionsState build() => LibraryCollectionsState(isLoaded: true); +} + +class _Recent extends RecentAccessNotifier { + @override + RecentAccessState build() => const RecentAccessState(isLoaded: true); + + @override + void recordAlbumAccess({ + required String id, + required String name, + String? artistName, + String? imageUrl, + String? providerId, + }) {} +} + +void main() { + for (final album in [true, false]) { + final page = album ? 'album' : 'playlist'; + testWidgets('$page select all toggle selects every track, then exits', ( + tester, + ) async { + tester.view.physicalSize = const Size(430, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final container = ProviderContainer( + overrides: [ + settingsProvider.overrideWith(FixedSettingsNotifier.new), + libraryCollectionsProvider.overrideWith(_Collections.new), + recentAccessProvider.overrideWith(_Recent.new), + currentMediaItemProvider.overrideWithBuild( + (ref, notifier) => Stream.value(null), + ), + downloadQueueLookupProvider.overrideWithValue( + const DownloadQueueLookup.empty(), + ), + downloadHistoryVisibleBatchExistsProvider.overrideWith( + (ref, request) => const {}, + ), + ], + ); + addTearDown(container.dispose); + await tester.pumpWidget( + UncontrolledProviderScope( + container: container, + child: MaterialApp( + theme: AppTheme.light(), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: album + ? AlbumScreen( + albumId: 'example-album', + albumName: 'Example album', + tracks: _tracks, + ) + : PlaylistScreen( + playlistName: 'Example playlist', + tracks: _tracks, + ), + ), + ), + ); + await tester.pumpAndSettle(); + await tester.longPress(find.text('Example song 1')); + await tester.pumpAndSettle(); + + SelectionBottomBar bar() => + tester.widget(find.byType(SelectionBottomBar)); + List selected() => [ + for (final tile in tester.widgetList( + find.byType(TrackListTile), + )) + tile.isSelected, + ]; + expect(bar().selectedCount, 1); + expect(bar().allSelected, isFalse); + bar().onToggleSelectAll(); + await tester.pumpAndSettle(); + expect(bar().selectedCount, 4); + expect(bar().allSelected, isTrue); + expect(selected(), [true, true, true, true]); + bar().onToggleSelectAll(); + await tester.pumpAndSettle(); + expect(selected(), [false, false, false, false]); + expect( + tester + .widgetList(find.byType(TrackListTile)) + .any((tile) => tile.isSelectionMode), + isFalse, + ); + expect(tester.takeException(), isNull); + await tester.pumpWidget(const SizedBox()); + }); + } +}