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.
This commit is contained in:
zarzet committed 2026-10-23 22:49:09 +07:00
1 parent cdb250c4ed
commit 4707feeaa3
8 files changed
+209 -89

No files matched your search

+27
View File
@@ -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<T extends StatefulWidget> on State<T> {
});
}
/// 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<String> ids,
required double bottomPadding,
required List<Widget> 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<String> validIds) {
+8 -25
View File
@@ -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<AlbumScreen>
? 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<AlbumScreen>
context,
colorScheme,
tracks,
ids,
bottomPadding,
),
appBar: _buildAppBar(
@@ -507,12 +508,7 @@ class _AlbumScreenState extends ConsumerState<AlbumScreen>
],
);
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<AlbumScreen>
BuildContext barContext,
ColorScheme colorScheme,
List<Track> tracks,
List<String> 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(
+3 -15
View File
@@ -823,20 +823,12 @@ class _ArtistScreenState extends ConsumerState<ArtistScreen>
super.toggleSelection(itemId);
}
void _deselectAll() {
setState(() {
selectedIds.clear();
});
}
Widget _buildSelectionBar(
BuildContext context,
ColorScheme colorScheme,
List<ArtistAlbum> 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<ArtistScreen>
(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
+8 -25
View File
@@ -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<PlaylistScreen>
Widget _buildSelectionBottomBar(
BuildContext context,
ColorScheme colorScheme,
List<String> 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<PlaylistScreen>
? 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<PlaylistScreen>
selectionBar: _buildSelectionBottomBar(
context,
colorScheme,
ids,
bottomPadding,
),
appBar: _buildAppBar(context, colorScheme),
@@ -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
+6 -21
View File
@@ -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,
),
],
],
+21
View File
@@ -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<SelectionBottomBar>(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);
});
}
}
+128
View File
@@ -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 <String>{},
),
],
);
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<SelectionBottomBar>(find.byType(SelectionBottomBar));
List<bool> selected() => [
for (final tile in tester.widgetList<TrackListTile>(
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<TrackListTile>(find.byType(TrackListTile))
.any((tile) => tile.isSelectionMode),
isFalse,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
});
}
}