Skip to content

Commit

Permalink
Fix hasNext/Previous in example_playlist with repeat modes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Sep 15, 2021
1 parent 83a8999 commit a55f565
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions audio_service/example/lib/example_playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class MainScreen extends StatelessWidget {
child: StreamBuilder<QueueState>(
stream: _audioHandler.queueState,
builder: (context, snapshot) {
final queueState = snapshot.data ?? QueueState([], 0, []);
final queueState = snapshot.data ?? QueueState.empty;
final queue = queueState.queue;
return ReorderableListView(
onReorder: (int oldIndex, int newIndex) {
Expand Down Expand Up @@ -250,7 +250,7 @@ class ControlButtons extends StatelessWidget {
StreamBuilder<QueueState>(
stream: audioHandler.queueState,
builder: (context, snapshot) {
final queueState = snapshot.data ?? QueueState([], null, []);
final queueState = snapshot.data ?? QueueState.empty;
return IconButton(
icon: const Icon(Icons.skip_previous),
onPressed:
Expand Down Expand Up @@ -290,7 +290,7 @@ class ControlButtons extends StatelessWidget {
StreamBuilder<QueueState>(
stream: audioHandler.queueState,
builder: (context, snapshot) {
final queueState = snapshot.data ?? QueueState([], null, []);
final queueState = snapshot.data ?? QueueState.empty;
return IconButton(
icon: const Icon(Icons.skip_next),
onPressed: queueState.hasNext ? audioHandler.skipToNext : null,
Expand Down Expand Up @@ -322,14 +322,22 @@ class ControlButtons extends StatelessWidget {
}

class QueueState {
static final QueueState empty =
const QueueState([], 0, [], AudioServiceRepeatMode.none);

final List<MediaItem> queue;
final int? queueIndex;
final List<int>? shuffleIndices;
final AudioServiceRepeatMode repeatMode;

QueueState(this.queue, this.queueIndex, this.shuffleIndices);
const QueueState(
this.queue, this.queueIndex, this.shuffleIndices, this.repeatMode);

bool get hasPrevious => (queueIndex ?? 0) > 0;
bool get hasNext => (queueIndex ?? 0) + 1 < queue.length;
bool get hasPrevious =>
repeatMode != AudioServiceRepeatMode.none || (queueIndex ?? 0) > 0;
bool get hasNext =>
repeatMode != AudioServiceRepeatMode.none ||
(queueIndex ?? 0) + 1 < queue.length;

List<int> get indices =>
shuffleIndices ?? List.generate(queue.length, (i) => i);
Expand Down Expand Up @@ -404,11 +412,13 @@ class AudioPlayerHandlerImpl extends BaseAudioHandler
playbackState,
_player.shuffleIndicesStream.whereType<List<int>>(),
(queue, playbackState, shuffleIndices) => QueueState(
queue,
playbackState.queueIndex,
playbackState.shuffleMode == AudioServiceShuffleMode.all
? shuffleIndices
: null)).where((state) =>
queue,
playbackState.queueIndex,
playbackState.shuffleMode == AudioServiceShuffleMode.all
? shuffleIndices
: null,
playbackState.repeatMode,
)).where((state) =>
state.shuffleIndices == null ||
state.queue.length == state.shuffleIndices!.length);

Expand Down

0 comments on commit a55f565

Please sign in to comment.