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

Expose autoplay option to SpircLoadCommand #1446

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [connect] Add `seek_to` field to `SpircLoadCommand` (breaking)
- [connect] Add `repeat_track` field to `SpircLoadCommand` (breaking)
- [connect] Add `autoplay` field to `SpircLoadCommand` (breaking)
- [connect] Add `pause` parameter to `Spirc::disconnect` method (breaking)
- [playback] Add `track` field to `PlayerEvent::RepeatChanged` (breaking)
- [core] Add `request_with_options` and `request_with_protobuf_and_options` to `SpClient`
Expand Down
7 changes: 6 additions & 1 deletion connect/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ pub struct SpircLoadCommand {
pub shuffle: bool,
pub repeat: bool,
pub repeat_track: bool,
/// Decides if the context or the autoplay of the context is played
///
/// ## Remarks:
/// If `true` is provided, the option values (`shuffle`, `repeat` and `repeat_track`) are ignored
pub autoplay: bool,
/// Decides the starting position in the given context
///
/// ## Remarks:
/// If none is provided and shuffle true, a random track is played, otherwise the first
/// If `None` is provided and `shuffle` is `true`, a random track is played, otherwise the first
pub playing_track: Option<PlayingTrack>,
}

Expand Down
23 changes: 18 additions & 5 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use futures_util::StreamExt;
use protobuf::MessageField;
use std::{
future::Future,
ops::Deref,
sync::atomic::{AtomicUsize, Ordering},
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
Expand Down Expand Up @@ -934,6 +935,7 @@ impl SpircTask {
shuffle,
repeat,
repeat_track,
autoplay: false,
},
Some(play.context),
)
Expand Down Expand Up @@ -1127,7 +1129,6 @@ impl SpircTask {
self.handle_activate();
}

let current_context_uri = self.connect_state.context_uri();
let fallback = if let Some(ref ctx) = context {
match ConnectState::get_context_uri_from_context(ctx) {
Some(ctx_uri) => ctx_uri,
Expand All @@ -1137,6 +1138,16 @@ impl SpircTask {
&cmd.context_uri
};

let update_context = if cmd.autoplay {
UpdateContext::Autoplay
} else {
UpdateContext::Default
};

self.connect_state
.set_active_context(*update_context.deref());

let current_context_uri = self.connect_state.context_uri();
if current_context_uri == &cmd.context_uri && fallback == cmd.context_uri {
debug!("context <{current_context_uri}> didn't change, no resolving required")
} else {
Expand All @@ -1145,7 +1156,7 @@ impl SpircTask {
self.context_resolver.add(ResolveContext::from_uri(
&cmd.context_uri,
fallback,
UpdateContext::Default,
update_context,
ContextAction::Replace,
));
let context = self.context_resolver.get_next_context(Vec::new).await;
Expand Down Expand Up @@ -1182,9 +1193,11 @@ impl SpircTask {
cmd.shuffle, cmd.repeat, cmd.repeat_track
);

self.connect_state.set_shuffle(cmd.shuffle);
self.connect_state.set_repeat_context(cmd.repeat);
self.connect_state.set_repeat_track(cmd.repeat_track);
self.connect_state.set_shuffle(!cmd.autoplay && cmd.shuffle);
self.connect_state
.set_repeat_context(!cmd.autoplay && cmd.repeat);
self.connect_state
.set_repeat_track(!cmd.autoplay && cmd.repeat_track);

if cmd.shuffle {
if let Some(index) = index {
Expand Down
1 change: 1 addition & 0 deletions examples/play_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async fn main() {
shuffle: false,
repeat: false,
repeat_track: false,
autoplay: false,
// the index specifies which track in the context starts playing, in this case the first in the album
playing_track: PlayingTrack::Index(0).into(),
})
Expand Down
Loading