Skip to content

Commit 3681bb3

Browse files
committed
fix: add page_index to SkipTo and handle it
1 parent f0d6afe commit 3681bb3

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

connect/src/model.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
core::dealer::protocol::SkipTo, protocol::context_player_options::ContextPlayerOptionOverrides,
2+
core::dealer::protocol::SkipTo,
3+
protocol::{context_page::ContextPage, context_player_options::ContextPlayerOptionOverrides},
34
};
45

56
use std::ops::Deref;
@@ -129,22 +130,33 @@ pub enum PlayingTrack {
129130
Uid(String),
130131
}
131132

132-
impl TryFrom<SkipTo> for PlayingTrack {
133-
type Error = ();
134-
135-
fn try_from(value: SkipTo) -> Result<Self, Self::Error> {
133+
impl PlayingTrack {
134+
pub(crate) fn from_skip_to(value: SkipTo, pages: &[ContextPage]) -> Result<Self, ()> {
136135
// order of checks is important, as the index can be 0, but still has an uid or uri provided,
137136
// so we only use the index as last resort
138137
if let Some(uri) = value.track_uri {
139138
Ok(PlayingTrack::Uri(uri))
140139
} else if let Some(uid) = value.track_uid {
141140
Ok(PlayingTrack::Uid(uid))
142-
} else if let Some(index) = value.track_index {
143-
Ok(PlayingTrack::Index(index))
141+
} else if value.track_index.is_some() || value.page_index.is_some() {
142+
Ok(PlayingTrack::Index(
143+
PlayingTrack::from_index(&value, pages) as u32
144+
))
144145
} else {
145146
Err(())
146147
}
147148
}
149+
150+
pub(crate) fn from_index(value: &SkipTo, pages: &[ContextPage]) -> usize {
151+
let page_index = value.track_index.unwrap_or_default() as usize;
152+
let track_index = value.page_index.unwrap_or_default() as usize;
153+
154+
(0..page_index).fold(track_index, |t, p| {
155+
let page_size = pages.get(p).map(|p| p.tracks.len()).unwrap_or_default();
156+
157+
t + page_size
158+
})
159+
}
148160
}
149161

150162
#[derive(Debug)]

connect/src/spirc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,16 +1002,17 @@ impl SpircTask {
10021002
.options
10031003
.skip_to
10041004
.as_ref()
1005-
.and_then(|s| s.track_index)
1006-
.map(|i| i as usize);
1005+
.map(|skip| PlayingTrack::from_index(skip, &play.context.pages));
10071006

10081007
self.handle_load(
10091008
LoadRequest {
10101009
context,
10111010
options: LoadRequestOptions {
10121011
start_playing: true,
10131012
seek_to: play.options.seek_to.unwrap_or_default(),
1014-
playing_track: play.options.skip_to.and_then(|s| s.try_into().ok()),
1013+
playing_track: play.options.skip_to.and_then(|s| {
1014+
PlayingTrack::from_skip_to(s, &play.context.pages).ok()
1015+
}),
10151016
context_options,
10161017
},
10171018
},

core/src/dealer/protocol/request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub struct OptionsOptions {
200200
pub struct SkipTo {
201201
pub track_uid: Option<String>,
202202
pub track_uri: Option<String>,
203+
pub page_index: Option<u32>,
203204
pub track_index: Option<u32>,
204205
}
205206

0 commit comments

Comments
 (0)