Skip to content

Commit

Permalink
connect: simpler update of current index
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Nov 26, 2024
1 parent bca6b70 commit ffc31bb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
17 changes: 12 additions & 5 deletions connect/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ pub struct ConnectState {
pub device: DeviceInfo,

unavailable_uri: Vec<String>,
/// is only some when we're playing a queued item and have to preserve the index
player_index: Option<ContextIndex>,

/// index: 0 based, so the first track is index 0
player: PlayerState,
Expand Down Expand Up @@ -254,6 +252,17 @@ impl ConnectState {
self.update_restrictions()
}

pub fn update_current_index(&mut self, f: impl Fn(&mut ContextIndex)) {
match self.player.index.as_mut() {
Some(player_index) => f(player_index),
None => {
let mut new_index = ContextIndex::new();
f(&mut new_index);
self.player.index = MessageField::some(new_index)
}
}
}

pub fn update_position(&mut self, position_ms: u32, timestamp: i64) {
self.player.position_as_of_timestamp = position_ms.into();
self.player.timestamp = timestamp;
Expand All @@ -271,9 +280,7 @@ impl ConnectState {

pub fn reset_playback_to_position(&mut self, new_index: Option<usize>) -> Result<(), Error> {
let new_index = new_index.unwrap_or(0);
if let Some(player_index) = self.player.index.as_mut() {
player_index.track = new_index as u32;
}
self.update_current_index(|i| i.track = new_index as u32);

self.update_context_index(new_index + 1)?;

Expand Down
4 changes: 3 additions & 1 deletion connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl ConnectState {

if matches!(new_context, Some(ctx) if self.player.context_uri != ctx) {
self.context = None;
self.next_contexts.clear();
} else if let Some(ctx) = self.context.as_mut() {
ctx.index.track = 0;
ctx.index.page = 0;
Expand Down Expand Up @@ -275,7 +276,7 @@ impl ConnectState {
}?;

// assumption: the uid is used as unique-id of any item
// - queue resorting is done by each client and orients itself by the given uid
// - queue resorting is done by each client and orients itself by the given uid
// - if no uid is present, resorting doesn't work or behaves not as intended
let uid = if ctx_track.uid.is_empty() {
// so setting providing a unique id should allow to resort the queue
Expand Down Expand Up @@ -326,6 +327,7 @@ impl ConnectState {
};

if next.tracks.is_empty() {
self.update_current_index(|i| i.page += 1);
return Ok(LoadNext::PageUrl(next.page_url));
}

Expand Down
20 changes: 4 additions & 16 deletions connect/src/state/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ impl<'ct> ConnectState {

self.player.track = MessageField::some(new_track.clone());

if let Some(player_index) = self.player.index.as_mut() {
player_index.track = index as u32;
}
self.update_current_index(|i| i.track = index as u32);

Ok(())
}
Expand Down Expand Up @@ -100,11 +98,7 @@ impl<'ct> ConnectState {
self.fill_up_next_tracks()?;

let is_queue_or_autoplay = new_track.is_queue() || new_track.is_autoplay();
let update_index = if is_queue_or_autoplay && self.player.index.is_some() {
// the index isn't send when we are a queued track, but we have to preserve it for later
self.player_index = self.player.index.take();
None
} else if is_queue_or_autoplay {
let update_index = if is_queue_or_autoplay {
None
} else {
let ctx = self.context.as_ref();
Expand All @@ -119,11 +113,7 @@ impl<'ct> ConnectState {
};

if let Some(update_index) = update_index {
if let Some(index) = self.player.index.as_mut() {
index.track = update_index
} else {
debug!("next: index can't be updated, no index available")
}
self.update_current_index(|i| i.track = update_index)
}

self.player.track = MessageField::some(new_track);
Expand Down Expand Up @@ -180,10 +170,8 @@ impl<'ct> ConnectState {

if self.player.index.track == 0 {
warn!("prev: trying to skip into negative, index update skipped")
} else if let Some(index) = self.player.index.as_mut() {
index.track -= 1;
} else {
debug!("prev: index can't be decreased, no index available")
self.update_current_index(|i| i.track -= 1)
}

self.update_restrictions();
Expand Down
12 changes: 2 additions & 10 deletions connect/src/state/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::state::provider::{IsProvider, Provider};
use crate::state::{ConnectState, StateError};
use librespot_core::Error;
use librespot_protocol::player::{ContextIndex, ProvidedTrack, TransferState};
use librespot_protocol::player::{ProvidedTrack, TransferState};
use protobuf::MessageField;

impl ConnectState {
Expand Down Expand Up @@ -96,15 +96,7 @@ impl ConnectState {

let current_index = current_index.ok();
if let Some(current_index) = current_index {
if let Some(index) = self.player.index.as_mut() {
index.track = current_index as u32;
} else {
self.player.index = MessageField::some(ContextIndex {
page: 0,
track: current_index as u32,
..Default::default()
})
}
self.update_current_index(|i| i.track = current_index as u32);
}

debug!(
Expand Down

0 comments on commit ffc31bb

Please sign in to comment.