Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous fixes #439

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,32 @@ impl Client {
pub async fn handle_player_request(
&self,
request: PlayerRequest,
playback: Option<PlaybackMetadata>,
mut playback: Option<PlaybackMetadata>,
) -> Result<Option<PlaybackMetadata>> {
// `TransferPlayback` needs to be handled separately from other player requests
// because `TransferPlayback` doesn't require an active playback
if let PlayerRequest::TransferPlayback(device_id, force_play) = request {
self.transfer_playback(&device_id, Some(force_play)).await?;
tracing::info!("Transferred playback to device with id={}", device_id);
return Ok(playback);
// handle requests that don't require an active playback
match request {
PlayerRequest::TransferPlayback(device_id, force_play) => {
// `TransferPlayback` needs to be handled separately from other player requests
// because `TransferPlayback` doesn't require an active playback
self.transfer_playback(&device_id, Some(force_play)).await?;
tracing::info!("Transferred playback to device with id={}", device_id);
return Ok(playback);
}
PlayerRequest::StartPlayback(p, shuffle) => {
// Set the playback's shuffle state if specified in the request
if let (Some(shuffle), Some(playback)) = (shuffle, playback.as_mut()) {
playback.shuffle_state = shuffle;
}
let device_id = playback.as_ref().and_then(|p| p.device_id.as_deref());
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
if let Some(ref playback) = playback {
self.shuffle(playback.shuffle_state, device_id).await?;
}
return Ok(playback);
}
_ => {}
}

let mut playback = playback.context("no playback found")?;
Expand Down Expand Up @@ -188,14 +206,8 @@ impl Client {

playback.mute_state = new_mute_state;
}
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
self.shuffle(playback.shuffle_state, device_id).await?;
PlayerRequest::StartPlayback(..) => {
anyhow::bail!("`StartPlayback` should be handled earlier")
}
PlayerRequest::TransferPlayback(..) => {
anyhow::bail!("`TransferPlayback` should be handled earlier")
Expand Down
14 changes: 7 additions & 7 deletions spotify_player/src/event/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ fn handle_command_for_track_table_window(

match command {
Command::PlayRandom | Command::ChooseSelected => {
let id = if command == Command::PlayRandom {
rand::thread_rng().gen_range(0..tracks.len())
let uri = if command == Command::PlayRandom {
tracks[rand::thread_rng().gen_range(0..tracks.len())]
.id
.uri()
} else {
id
filtered_tracks[id].id.uri()
};

let base_playback = if let Some(context_id) = context_id {
Expand All @@ -222,10 +224,8 @@ fn handle_command_for_track_table_window(
};

client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
base_playback.uri_offset(
tracks[id].id.uri(),
config::get_config().app_config.tracks_playback_limit,
),
base_playback
.uri_offset(uri, config::get_config().app_config.tracks_playback_limit),
None,
)))?;
}
Expand Down
Loading