fixed bug in queue

This commit is contained in:
Moritz Weber 2023-03-19 21:00:12 +01:00
parent 727168c128
commit c68c2e9806
4 changed files with 26 additions and 30 deletions

View file

@ -1,6 +1,7 @@
## Unreleased Changes
- Fixed bug in "Append to manually queued songs"
- Fixed bug in queue when moving a song directly before the currently playing song
## 1.2.0

View file

@ -35,4 +35,7 @@ abstract class AudioPlayerDataSource {
Future<void> seekToIndex(int index);
Future<void> setLoopMode(LoopMode loopMode);
/// Calculate the new current index when moving a song from [oldIndex] to [newIndex].
int calcNewCurrentIndexOnMove(int currentIndex, int oldIndex, int newIndex);
}

View file

@ -172,18 +172,7 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
@override
Future<void> moveQueueItem(int oldIndex, int newIndex) async {
_log.d('moveQueueItem: $oldIndex -> $newIndex');
final int oldCurrentIndex = currentIndexStream.value;
int newCurrentIndex = oldCurrentIndex;
if (oldIndex == oldCurrentIndex) {
newCurrentIndex = newIndex;
} else {
if (oldIndex < oldCurrentIndex) {
newCurrentIndex--;
}
if (newIndex < oldCurrentIndex) {
newCurrentIndex++;
}
}
final newCurrentIndex = calcNewCurrentIndexOnMove(currentIndexStream.value, oldIndex, newIndex);
final song = _queue[oldIndex];
final newQueue = List<SongModel>.from(_queue);
@ -502,4 +491,24 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
await _audioPlayer.seek(duration * position);
}
}
@override
int calcNewCurrentIndexOnMove(int currentIndex, int oldIndex, int newIndex) {
int newCurrentIndex = currentIndex;
if (oldIndex == currentIndex) {
// moving the currently playing song
newCurrentIndex = newIndex;
} else {
if (oldIndex < currentIndex) {
// equality is caught by the first if
newCurrentIndex--;
}
if (oldIndex > currentIndex && newIndex <= currentIndex) {
newCurrentIndex++;
} else if (newIndex < currentIndex) {
newCurrentIndex++;
}
}
return newCurrentIndex;
}
}

View file

@ -151,7 +151,7 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
@override
Future<void> moveQueueItem(int oldIndex, int newIndex) async {
_dynamicQueue.moveQueueItem(oldIndex, newIndex);
final newCurrentIndex = _calcNewCurrentIndexOnMove(
final newCurrentIndex = _audioPlayerDataSource.calcNewCurrentIndexOnMove(
currentIndexStream.value ?? 0,
oldIndex,
newIndex,
@ -337,23 +337,6 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
return result;
}
/// Calculate the new current index when moving a song from [oldIndex] to [newIndex].
int _calcNewCurrentIndexOnMove(int currentIndex, int oldIndex, int newIndex) {
int newCurrentIndex = currentIndex;
if (oldIndex == currentIndex) {
// moving the currently playing song
newCurrentIndex = newIndex;
} else {
if (oldIndex < currentIndex) {
newCurrentIndex--;
}
if (newIndex < currentIndex) {
newCurrentIndex++;
}
}
return newCurrentIndex;
}
@override
Future<void> seekToPosition(double position) async =>
_audioPlayerDataSource.seekToPosition(position);