Skip to content

Commit 5f7cfdc

Browse files
authored
Expose autoplay option to SpircLoadCommand (#1446)
* connect: add autoplay option to SpircLoadCommand * update CHANGELOG.md * actually ignore options when starting autoplay
1 parent 0e9a3de commit 5f7cfdc

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
- [connect] Add `seek_to` field to `SpircLoadCommand` (breaking)
2020
- [connect] Add `repeat_track` field to `SpircLoadCommand` (breaking)
21+
- [connect] Add `autoplay` field to `SpircLoadCommand` (breaking)
2122
- [connect] Add `pause` parameter to `Spirc::disconnect` method (breaking)
2223
- [playback] Add `track` field to `PlayerEvent::RepeatChanged` (breaking)
2324
- [core] Add `request_with_options` and `request_with_protobuf_and_options` to `SpClient`

connect/src/model.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ pub struct SpircLoadCommand {
99
pub shuffle: bool,
1010
pub repeat: bool,
1111
pub repeat_track: bool,
12+
/// Decides if the context or the autoplay of the context is played
13+
///
14+
/// ## Remarks:
15+
/// If `true` is provided, the option values (`shuffle`, `repeat` and `repeat_track`) are ignored
16+
pub autoplay: bool,
1217
/// Decides the starting position in the given context
1318
///
1419
/// ## Remarks:
15-
/// If none is provided and shuffle true, a random track is played, otherwise the first
20+
/// If `None` is provided and `shuffle` is `true`, a random track is played, otherwise the first
1621
pub playing_track: Option<PlayingTrack>,
1722
}
1823

connect/src/spirc.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use futures_util::StreamExt;
3737
use protobuf::MessageField;
3838
use std::{
3939
future::Future,
40+
ops::Deref,
4041
sync::atomic::{AtomicUsize, Ordering},
4142
sync::Arc,
4243
time::{Duration, SystemTime, UNIX_EPOCH},
@@ -934,6 +935,7 @@ impl SpircTask {
934935
shuffle,
935936
repeat,
936937
repeat_track,
938+
autoplay: false,
937939
},
938940
Some(play.context),
939941
)
@@ -1127,7 +1129,6 @@ impl SpircTask {
11271129
self.handle_activate();
11281130
}
11291131

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

1141+
let update_context = if cmd.autoplay {
1142+
UpdateContext::Autoplay
1143+
} else {
1144+
UpdateContext::Default
1145+
};
1146+
1147+
self.connect_state
1148+
.set_active_context(*update_context.deref());
1149+
1150+
let current_context_uri = self.connect_state.context_uri();
11401151
if current_context_uri == &cmd.context_uri && fallback == cmd.context_uri {
11411152
debug!("context <{current_context_uri}> didn't change, no resolving required")
11421153
} else {
@@ -1145,7 +1156,7 @@ impl SpircTask {
11451156
self.context_resolver.add(ResolveContext::from_uri(
11461157
&cmd.context_uri,
11471158
fallback,
1148-
UpdateContext::Default,
1159+
update_context,
11491160
ContextAction::Replace,
11501161
));
11511162
let context = self.context_resolver.get_next_context(Vec::new).await;
@@ -1182,9 +1193,11 @@ impl SpircTask {
11821193
cmd.shuffle, cmd.repeat, cmd.repeat_track
11831194
);
11841195

1185-
self.connect_state.set_shuffle(cmd.shuffle);
1186-
self.connect_state.set_repeat_context(cmd.repeat);
1187-
self.connect_state.set_repeat_track(cmd.repeat_track);
1196+
self.connect_state.set_shuffle(!cmd.autoplay && cmd.shuffle);
1197+
self.connect_state
1198+
.set_repeat_context(!cmd.autoplay && cmd.repeat);
1199+
self.connect_state
1200+
.set_repeat_track(!cmd.autoplay && cmd.repeat_track);
11881201

11891202
if cmd.shuffle {
11901203
if let Some(index) = index {

examples/play_connect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ async fn main() {
8383
shuffle: false,
8484
repeat: false,
8585
repeat_track: false,
86+
autoplay: false,
8687
// the index specifies which track in the context starts playing, in this case the first in the album
8788
playing_track: PlayingTrack::Index(0).into(),
8889
})

0 commit comments

Comments
 (0)