Skip to content

Commit 5c87acf

Browse files
committed
fix(queue): preserve drop position when reordering top item
1 parent ac2b9ef commit 5c87acf

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

lib/services/audio_service.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,54 @@ class MusifyAudioHandler extends BaseAudioHandler {
912912
}
913913
}
914914

915+
Future<void> reorderQueueById(String queueEntryId, int targetIndex) async {
916+
try {
917+
_queueEntryIds.ensureIds(_queueList);
918+
919+
final oldIndex = _queueList.indexWhere(
920+
(s) => _queueEntryIds.ensureId(s) == queueEntryId,
921+
);
922+
if (oldIndex == -1) return;
923+
924+
// Clamp target index to valid range (allow insert at end)
925+
if (targetIndex < 0) targetIndex = 0;
926+
if (targetIndex > _queueList.length) targetIndex = _queueList.length;
927+
928+
final song = _queueList.removeAt(oldIndex);
929+
var newIndex = targetIndex;
930+
if (newIndex > _queueList.length) newIndex = _queueList.length;
931+
_queueList.insert(newIndex, song);
932+
933+
if (oldIndex == _currentQueueIndex) {
934+
_currentQueueIndex = newIndex;
935+
} else if (oldIndex < _currentQueueIndex &&
936+
newIndex >= _currentQueueIndex) {
937+
_currentQueueIndex--;
938+
} else if (oldIndex > _currentQueueIndex &&
939+
newIndex <= _currentQueueIndex) {
940+
_currentQueueIndex++;
941+
}
942+
943+
if (oldIndex == _currentLoadingIndex) {
944+
_currentLoadingIndex = newIndex;
945+
} else if (oldIndex < _currentLoadingIndex &&
946+
newIndex >= _currentLoadingIndex) {
947+
_currentLoadingIndex--;
948+
} else if (oldIndex > _currentLoadingIndex &&
949+
newIndex <= _currentLoadingIndex) {
950+
_currentLoadingIndex++;
951+
}
952+
953+
_updateQueueMediaItems();
954+
} catch (e, stackTrace) {
955+
logger.log(
956+
'Error reordering queue by id',
957+
error: e,
958+
stackTrace: stackTrace,
959+
);
960+
}
961+
}
962+
915963
void clearQueue() {
916964
try {
917965
_queueList.clear();

lib/widgets/queue_list_view.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,22 @@ class _QueueWidgetState extends State<QueueWidget> {
289289
padding: const EdgeInsets.only(top: 4, bottom: 24, left: 8, right: 8),
290290
itemCount: _queue.length,
291291
onReorderItem: (oldIndex, newIndex) {
292-
if (newIndex > oldIndex) newIndex--;
292+
final movingId =
293+
_queue[oldIndex]['queueEntryId']?.toString() ??
294+
'legacy_${_queue[oldIndex]['ytid']}_$oldIndex';
295+
293296
setState(() {
294297
final item = _queue.removeAt(oldIndex);
295-
_queue.insert(newIndex, item);
298+
var insertIndex = newIndex;
299+
if (insertIndex < 0) insertIndex = 0;
300+
if (insertIndex > _queue.length) insertIndex = _queue.length;
301+
_queue.insert(insertIndex, item);
296302
});
297-
audioHandler.reorderQueue(oldIndex, newIndex);
303+
304+
final actualIndex = _queue.indexWhere(
305+
(item) => item['queueEntryId']?.toString() == movingId,
306+
);
307+
audioHandler.reorderQueueById(movingId, actualIndex);
298308
},
299309
proxyDecorator: (child, index, animation) => Material(
300310
elevation: 8,

0 commit comments

Comments
 (0)