Skip to content

Commit

Permalink
feat: add play next param (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw authored Jun 19, 2024
1 parent 8d8e7cf commit 41012e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/commands/play/play.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class PlayItemCommand {
this.logger.debug(
`Adding ${tracks.length} tracks with a duration of ${reducedDuration} ticks`,
);
this.playbackService.getPlaylistOrDefault().enqueueTracks(tracks);
this.playbackService.getPlaylistOrDefault().enqueueTracks(tracks, dto.next);

const remoteImages = tracks.flatMap((track) => track.getRemoteImages());
const remoteImage: RemoteImageInfo | undefined =
Expand Down
7 changes: 7 additions & 0 deletions src/commands/play/play.params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export class PlayCommandParams {
})
name: string;

@Param({
description: 'Add to the start of the playlist',
required: false,
type: ParamType.BOOLEAN,
})
next: boolean;

@Choice(SearchType)
@Param({ description: 'Desired item type', type: ParamType.INTEGER })
type: SearchType | undefined;
Expand Down
13 changes: 9 additions & 4 deletions src/models/music/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Playlist {
* @param tracks the tracks that should be added
* @returns the new length of the tracks in the playlist
*/
enqueueTracks(tracks: Track[]) {
enqueueTracks(tracks: Track[], playNext = false) {
if (tracks.length === 0) {
return 0;
}
Expand All @@ -88,21 +88,26 @@ export class Playlist {
count: tracks.length,
activeTrack: this.activeTrackIndex,
});
const length = this.tracks.push(...tracks);

if (playNext) {
this.tracks = [...tracks, ...this.tracks];
} else {
this.tracks.push(...tracks);
}

// existing tracks are in the playlist, but none are playing. play the first track out of the new tracks
if (!this.hasAnyPlaying() && tracks.length > 0) {
this.activeTrackIndex = previousTrackLength;
this.announceTrackChange();
return length;
return this.tracks.length;
}

// emit a track change if there is no item
if (this.activeTrackIndex === undefined) {
this.announceTrackChange();
}

return length;
return this.tracks.length;
}

/**
Expand Down

0 comments on commit 41012e3

Please sign in to comment.