Skip to content

Commit 8d9470f

Browse files
committed
fix: improve queue management and song skipping logic in audio service
1 parent 8cceabd commit 8d9470f

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

lib/services/audio_service.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -865,17 +865,21 @@ class MusifyAudioHandler extends BaseAudioHandler {
865865

866866
if (index < _currentQueueIndex) {
867867
_currentQueueIndex--;
868-
} else if (index == _currentQueueIndex && _queueList.isNotEmpty) {
869-
// If removing the currently-loading song, reset loading state
870-
if (_currentLoadingIndex == index) {
871-
_currentLoadingIndex = -1;
872-
_currentLoadingTransitionId = -1;
873-
}
868+
} else if (index == _currentQueueIndex) {
869+
if (_queueList.isEmpty) {
870+
await stop();
871+
} else {
872+
// If removing the currently-loading song, reset loading state
873+
if (_currentLoadingIndex == index) {
874+
_currentLoadingIndex = -1;
875+
_currentLoadingTransitionId = -1;
876+
}
874877

875-
if (_currentQueueIndex >= _queueList.length) {
876-
_currentQueueIndex = _queueList.length - 1;
878+
if (_currentQueueIndex >= _queueList.length) {
879+
_currentQueueIndex = _queueList.length - 1;
880+
}
881+
await _playFromQueue(_currentQueueIndex);
877882
}
878-
await _playFromQueue(_currentQueueIndex);
879883
}
880884

881885
_hydrateQueueEntryIds();
@@ -1598,6 +1602,9 @@ class MusifyAudioHandler extends BaseAudioHandler {
15981602
}
15991603
}
16001604

1605+
@override
1606+
Future<void> skipToQueueItem(int index) => skipToSong(index);
1607+
16011608
@override
16021609
Future<void> skipToNext() async {
16031610
try {

lib/services/common_services.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ Future<void> updateSongLikeStatus(dynamic songId, bool add) async {
241241
}
242242

243243
void moveLikedSong(int oldIndex, int newIndex) {
244-
final _song = userLikedSongsList[oldIndex];
245-
userLikedSongsList
246-
..removeAt(oldIndex)
247-
..insert(newIndex, _song);
244+
if (oldIndex < newIndex) {
245+
newIndex -= 1;
246+
}
247+
final _song = userLikedSongsList.removeAt(oldIndex);
248+
userLikedSongsList.insert(newIndex, _song);
248249
currentLikedSongsLength.value = userLikedSongsList.length;
249250
unawaited(addOrUpdateData('user', 'likedSongs', userLikedSongsList));
250251
}
@@ -701,7 +702,6 @@ const recentlyPlayedSongsLimit = 250;
701702
Future<void> updateRecentlyPlayed(dynamic songId) async {
702703
try {
703704
if (userRecentlyPlayed.isNotEmpty &&
704-
userRecentlyPlayed.length == 1 &&
705705
userRecentlyPlayed[0]['ytid'] == songId) {
706706
final existing = userRecentlyPlayed[0] as Map;
707707
existing['listeningCount'] = (existing['listeningCount'] ?? 0) + 1;

lib/services/playlist_download_service.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,8 @@ class OfflinePlaylistService {
478478
void cleanupProgressNotifier(String playlistId) {
479479
try {
480480
if (downloadProgressNotifiers.containsKey(playlistId)) {
481-
downloadProgressNotifiers[playlistId]?.value = DownloadProgress(
482-
total: 0,
483-
);
481+
final notifier = downloadProgressNotifiers.remove(playlistId);
482+
notifier?.dispose();
484483
}
485484
} catch (e, stackTrace) {
486485
logger.log(

0 commit comments

Comments
 (0)