Skip to content

Commit 8b72954

Browse files
authored
Re-Add ability to handle/play tracks (#1468)
* re-add support to play a set of tracks * connect: reduce some cloning * connect: derive clone for LoadRequest * apply review, improve function naming * clippy fix
1 parent e2c3ac3 commit 8b72954

File tree

6 files changed

+212
-84
lines changed

6 files changed

+212
-84
lines changed

connect/src/context_resolver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ impl ResolveContext {
7676
// otherwise we might not even check if we need to fallback and just use the fallback uri
7777
match self.resolve {
7878
Resolve::Uri(ref uri) => ConnectState::valid_resolve_uri(uri),
79-
Resolve::Context(ref ctx) => ConnectState::get_context_uri_from_context(ctx),
79+
Resolve::Context(ref ctx) => {
80+
ConnectState::find_valid_uri(ctx.uri.as_deref(), ctx.pages.first())
81+
}
8082
}
8183
.or(self.fallback.as_deref())
8284
}
@@ -260,7 +262,7 @@ impl ContextResolver {
260262
ContextAction::Replace => {
261263
let remaining = state.update_context(context, next.update);
262264
if let Resolve::Context(ref ctx) = next.resolve {
263-
state.merge_context(Some(ctx.clone()));
265+
state.merge_context(ctx.pages.clone().pop());
264266
}
265267

266268
remaining

connect/src/model.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::{
55
use std::ops::Deref;
66

77
/// Request for loading playback
8-
#[derive(Debug)]
8+
#[derive(Debug, Clone)]
99
pub struct LoadRequest {
10-
pub(super) context_uri: String,
10+
pub(super) context: PlayContext,
1111
pub(super) options: LoadRequestOptions,
1212
}
1313

@@ -19,8 +19,14 @@ impl Deref for LoadRequest {
1919
}
2020
}
2121

22+
#[derive(Debug, Clone)]
23+
pub(super) enum PlayContext {
24+
Uri(String),
25+
Tracks(Vec<String>),
26+
}
27+
2228
/// The parameters for creating a load request
23-
#[derive(Debug, Default)]
29+
#[derive(Debug, Default, Clone)]
2430
pub struct LoadRequestOptions {
2531
/// Whether the given tracks should immediately start playing, or just be initially loaded.
2632
pub start_playing: bool,
@@ -44,7 +50,7 @@ pub struct LoadRequestOptions {
4450
///
4551
/// Separated into an `enum` to exclude the other variants from being used
4652
/// simultaneously, as they are not compatible.
47-
#[derive(Debug)]
53+
#[derive(Debug, Clone)]
4854
pub enum LoadContextOptions {
4955
/// Starts the context with options
5056
Options(Options),
@@ -56,7 +62,7 @@ pub enum LoadContextOptions {
5662
}
5763

5864
/// The available options that indicate how to start the context
59-
#[derive(Debug, Default)]
65+
#[derive(Debug, Default, Clone)]
6066
pub struct Options {
6167
/// Start the context in shuffle mode
6268
pub shuffle: bool,
@@ -80,16 +86,30 @@ impl LoadRequest {
8086
/// Create a load request from a `context_uri`
8187
///
8288
/// For supported `context_uri` see [`SpClient::get_context`](librespot_core::spclient::SpClient::get_context)
89+
///
90+
/// Equivalent to using [`/me/player/play`](https://developer.spotify.com/documentation/web-api/reference/start-a-users-playback)
91+
/// and providing `context_uri`
8392
pub fn from_context_uri(context_uri: String, options: LoadRequestOptions) -> Self {
8493
Self {
85-
context_uri,
94+
context: PlayContext::Uri(context_uri),
95+
options,
96+
}
97+
}
98+
99+
/// Create a load request from a set of `tracks`
100+
///
101+
/// Equivalent to using [`/me/player/play`](https://developer.spotify.com/documentation/web-api/reference/start-a-users-playback)
102+
/// and providing `uris`
103+
pub fn from_tracks(tracks: Vec<String>, options: LoadRequestOptions) -> Self {
104+
Self {
105+
context: PlayContext::Tracks(tracks),
86106
options,
87107
}
88108
}
89109
}
90110

91111
/// An item that represent a track to play
92-
#[derive(Debug)]
112+
#[derive(Debug, Clone)]
93113
pub enum PlayingTrack {
94114
/// Represent the track at a given index.
95115
Index(u32),

0 commit comments

Comments
 (0)