Merge pull request #325 from ashthespy/dynamic-playlists

Refactor adding context tracks to state
This commit is contained in:
Sasha Hilton 2019-04-16 18:39:25 +01:00 committed by GitHub
commit 5d2cb32e20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,9 @@
use std; use std;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use futures::{Async, Future, Poll, Sink, Stream};
use futures::future; use futures::future;
use futures::sync::{mpsc, oneshot}; use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Poll, Sink, Stream};
use protobuf::{self, Message}; use protobuf::{self, Message};
use rand; use rand;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
@ -776,26 +776,22 @@ impl SpircTask {
self.context_fut = self.resolve_uri(&context.next_page_url); self.context_fut = self.resolve_uri(&context.next_page_url);
let new_tracks = &context.tracks; let new_tracks = &context.tracks;
debug!("Adding {:?} tracks from context to playlist", new_tracks.len()); debug!("Adding {:?} tracks from context to frame", new_tracks.len());
let current_index = self.state.get_playing_track_index(); let mut track_vec = self.state.take_track().into_vec();
let mut new_index = 0; if let Some(head) = track_vec.len().checked_sub(CONTEXT_TRACKS_HISTORY) {
{ track_vec.drain(0..head);
let mut tracks = self.state.mut_track(); }
// Does this need to be optimised - we don't need to actually traverse the len of tracks track_vec.extend_from_slice(&new_tracks);
let tracks_len = tracks.len(); self.state.set_track(protobuf::RepeatedField::from_vec(track_vec));
if tracks_len > CONTEXT_TRACKS_HISTORY {
tracks.rotate_right(tracks_len - CONTEXT_TRACKS_HISTORY); // Update playing index
tracks.truncate(CONTEXT_TRACKS_HISTORY); if let Some(new_index) = self
} .state
// tracks.extend_from_slice(&mut new_tracks); // method doesn't exist for protobuf::RepeatedField .get_playing_track_index()
for t in new_tracks { .checked_sub(CONTEXT_TRACKS_HISTORY as u32)
tracks.push(t.to_owned()); {
} self.state.set_playing_track_index(new_index);
if current_index > CONTEXT_TRACKS_HISTORY as u32 {
new_index = current_index - CONTEXT_TRACKS_HISTORY as u32;
}
} }
self.state.set_playing_track_index(new_index);
} }
} }