Skip to content

Commit

Permalink
Allow shuffling context playback from CLI (#275)
Browse files Browse the repository at this point in the history
Co-authored-by: Thang Pham <[email protected]>
  • Loading branch information
rudiejd and aome510 authored Oct 23, 2023
1 parent 9ba3a03 commit d021d70
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
16 changes: 10 additions & 6 deletions spotify_player/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ async fn handle_playback_request(
let sid = get_spotify_id(client, item_type, id_or_name).await?;
let tracks = client.radio_tracks(sid.uri()).await?;

PlayerRequest::StartPlayback(Playback::URIs(
tracks.into_iter().map(|t| t.id).collect(),
PlayerRequest::StartPlayback(
Playback::URIs(tracks.into_iter().map(|t| t.id).collect(), None),
None,
))
)
}
Command::StartLikedTracks { limit, random } => {
let mut tracks = client.current_user_saved_tracks().await?;
Expand All @@ -314,9 +314,13 @@ async fn handle_playback_request(
.map(|t| t.id.to_owned())
.collect();

PlayerRequest::StartPlayback(Playback::URIs(ids, None))
PlayerRequest::StartPlayback(Playback::URIs(ids, None), None)
}
Command::StartContext(context_type, id_or_name) => {
Command::StartContext {
context_type,
id_or_name,
shuffle,
} => {
let sid = get_spotify_id(client, context_type.into(), id_or_name).await?;
let context_id = match sid {
ItemId::Playlist(id) => ContextId::Playlist(id),
Expand All @@ -325,7 +329,7 @@ async fn handle_playback_request(
_ => unreachable!(),
};

PlayerRequest::StartPlayback(Playback::Context(context_id, None))
PlayerRequest::StartPlayback(Playback::Context(context_id, None), Some(shuffle))
}
Command::PlayPause => PlayerRequest::ResumePause,
Command::Next => PlayerRequest::NextTrack,
Expand Down
7 changes: 7 additions & 0 deletions spotify_player/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ fn init_playback_start_subcommand() -> Command {
Arg::new("context_type")
.value_parser(EnumValueParser::<ContextType>::new())
.required(true),
)
.arg(
Arg::new("shuffle")
.short('s')
.long("shuffle")
.action(ArgAction::SetTrue)
.help("Shuffle tracks within the launched playback"),
),
))
.subcommand(
Expand Down
8 changes: 7 additions & 1 deletion spotify_player/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ fn handle_playback_subcommand(args: &ArgMatches, socket: &UdpSocket) -> Result<(
.get_one::<ContextType>("context_type")
.expect("context_type is required")
.to_owned();
let shuffle = args.get_flag("shuffle");

let id_or_name = get_id_or_name(args)?;
Command::StartContext(context_type, id_or_name)
Command::StartContext {
context_type,
id_or_name,
shuffle,
}
}
Some(("liked", args)) => {
let limit = *args
Expand Down
16 changes: 13 additions & 3 deletions spotify_player/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,25 @@ pub enum PlaylistCommand {

#[derive(Debug, Serialize, Deserialize)]
pub enum Command {
StartContext(ContextType, IdOrName),
StartLikedTracks { limit: usize, random: bool },
StartContext {
context_type: ContextType,
id_or_name: IdOrName,
shuffle: bool,
},
StartLikedTracks {
limit: usize,
random: bool,
},
StartRadio(ItemType, IdOrName),
PlayPause,
Next,
Previous,
Shuffle,
Repeat,
Volume { percent: i8, is_offset: bool },
Volume {
percent: i8,
is_offset: bool,
},
Seek(i64),
}

Expand Down
5 changes: 4 additions & 1 deletion spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ impl Client {

state.player.write().mute_state = new_mute_state;
}
PlayerRequest::StartPlayback(p) => {
PlayerRequest::StartPlayback(p, shuffle) => {
if let Some(shuffle) = shuffle {
playback.shuffle_state = shuffle;
}
self.start_playback(p, device_id).await?;
// for some reasons, when starting a new playback, the integrated `spotify_player`
// client doesn't respect the initial shuffle state, so we need to manually update the state
Expand Down
2 changes: 1 addition & 1 deletion spotify_player/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum PlayerRequest {
Volume(u8),
ToggleMute,
TransferPlayback(String, bool),
StartPlayback(Playback),
StartPlayback(Playback, Option<bool>),
}

#[derive(Debug)]
Expand Down
3 changes: 3 additions & 0 deletions spotify_player/src/event/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ pub fn handle_command_for_track_table_window(
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
base_playback
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
None,
)))?;
}
Command::ChooseSelected => {
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
base_playback
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
None,
)))?;
}
Command::ShowActionsOnSelectedItem => {
Expand Down Expand Up @@ -265,6 +267,7 @@ pub fn handle_command_for_track_list_window(
// containing all the tracks in the table.
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
Playback::URIs(vec![tracks[id].id.clone()], None),
None,
)))?;
}
Command::ShowActionsOnSelectedItem => {
Expand Down

0 comments on commit d021d70

Please sign in to comment.