import 'dart:async'; import 'package:audio_service/audio_service.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:freezer/api/definitions.dart'; import 'package:freezer/api/player.dart'; import 'package:freezer/translations.i18n.dart'; import 'package:freezer/ui/menu.dart'; import 'package:freezer/ui/tiles.dart'; class QueueScreen extends StatefulWidget { const QueueScreen({super.key}); @override State createState() => _QueueScreenState(); } class _QueueScreenState extends State { static const itemExtent = 72.0; // height of each TrackTile final _scrollController = ScrollController(); late StreamSubscription _queueSub; static const _dismissibleBackground = DecoratedBox( decoration: BoxDecoration(color: Colors.red), child: Align( alignment: Alignment.centerLeft, child: Padding( padding: EdgeInsets.symmetric(horizontal: 24.0), child: Icon(Icons.delete)))); static const _dismissibleSecondaryBackground = DecoratedBox( decoration: BoxDecoration(color: Colors.red), child: Align( alignment: Alignment.centerRight, child: Padding( padding: EdgeInsets.symmetric(horizontal: 24.0), child: Icon(Icons.delete)))); bool _isReordering = false; /// Basically a simple list that keeps itself synchronized with [AudioHandler.queue], /// so that the [ReorderableListView] is updated instanly (as it should be) List _queueCache = []; @override void initState() { _queueCache = List.from(audioHandler.queue.value); // avoid shadow-copying _queueSub = audioHandler.queue.listen((newQueue) { print('got new queue!'); // if (listEquals(_queueCache, newQueue)) { // print('avoiding rebuilding queue since they are the same'); // return; // } _queueCache = List.from(newQueue); setState(() {}); }); WidgetsBinding.instance.addPostFrameCallback((_) { // calculate position of current item final position = playerHelper.queueIndex * itemExtent; _scrollController.jumpTo(position); }); super.initState(); } @override void dispose() { _queueSub.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Queue'.i18n), systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.light, statusBarBrightness: Brightness.light, systemNavigationBarColor: Theme.of(context).scaffoldBackgroundColor, systemNavigationBarDividerColor: Color( Theme.of(context).scaffoldBackgroundColor.value - 0x00111111), systemNavigationBarIconBrightness: Brightness.light, ), // actions: [ // IconButton( // icon: Icon( // Icons.shuffle, // semanticLabel: "Shuffle".i18n, // ), // onPressed: () async { // await playerHelper.toggleShuffle(); // setState(() {}); // }, // ) // ], ), body: SafeArea( child: ReorderableListView.builder( buildDefaultDragHandles: false, scrollController: _scrollController, // specify the itemExtent normally and remove it when reordering because of an issue with [SliverFixedExtentList] // https://github.com/flutter/flutter/issues/84901 itemExtent: _isReordering ? null : itemExtent, onReorderStart: (_) => setState(() => _isReordering = true), onReorderEnd: (_) => setState(() => _isReordering = true), onReorder: (oldIndex, newIndex) { setState(() => _queueCache.reorder(oldIndex, newIndex)); if (oldIndex == playerHelper.queueIndex) { audioHandler.customAction('setIndex', {'index': newIndex}); } playerHelper.reorder(oldIndex, newIndex); }, itemCount: _queueCache.length, itemBuilder: (BuildContext context, int index) { Track track = Track.fromMediaItem(_queueCache[index]); final int itemId = _queueCache[index].extras!['id'] ?? 0; return Dismissible( key: ValueKey(track.id.hashCode ^ itemId), background: _dismissibleBackground, secondaryBackground: _dismissibleSecondaryBackground, onDismissed: (_) { audioHandler.removeQueueItemAt(index).then((value) { if (index == playerHelper.queueIndex) { audioHandler.skipToNext(); } }); setState(() => _queueCache.removeAt(index)); }, confirmDismiss: (_) { final completer = Completer(); ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context) .showSnackBar(SnackBar( behavior: SnackBarBehavior.floating, content: Text('Song deleted from queue'.i18n), action: SnackBarAction( label: 'UNDO'.i18n, onPressed: () => completer.complete(false)))) .closed .then((value) { if (value == SnackBarClosedReason.action) return; completer.complete(true); }); return completer.future; }, child: SizedBox( height: itemExtent, child: TrackTile( track, trailing: ReorderableDragStartListener( index: index, child: const Icon(Icons.drag_handle)), onTap: () { audioHandler.skipToQueueItem(index).then((value) { Navigator.of(context).pop(); }); }, onHold: () => MenuSheet(context).defaultTrackMenu(track), ), ), ); }, ), ), ); } }