Refactor the simplified playlist function

This commit is contained in:
Oscar Linderholm
2019-04-09 21:23:04 +02:00
committed by Henrik Friedrichsen
parent 0f0b2f3e2a
commit 5981b08458
4 changed files with 42 additions and 18 deletions

View File

@@ -3,7 +3,7 @@ use std::ops::Deref;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard};
use rspotify::spotify::model::playlist::SimplifiedPlaylist; use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
use config; use config;
use events::EventManager; use events::EventManager;
@@ -16,7 +16,9 @@ const CACHE_FILE: &str = "playlists.db";
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Playlist { pub struct Playlist {
pub meta: SimplifiedPlaylist, pub id: String,
pub name: String,
pub snapshot_id: String,
pub tracks: Vec<Track>, pub tracks: Vec<Track>,
} }
@@ -42,7 +44,7 @@ impl ListItem for Playlist {
} }
fn display_left(&self) -> String { fn display_left(&self) -> String {
self.meta.name.clone() self.name.clone()
} }
fn display_right(&self) -> String { fn display_right(&self) -> String {
@@ -108,10 +110,30 @@ impl Playlists {
} }
} }
pub fn process_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist { pub fn process_simplified_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist {
debug!("got list: {}", list.name); Playlists::_process_playlist(
let id = list.id.clone(); list.id.clone(),
list.name.clone(),
list.snapshot_id.clone(),
spotify,
)
}
pub fn process_full_playlist(list: &FullPlaylist, spotify: &Spotify) -> Playlist {
Playlists::_process_playlist(
list.id.clone(),
list.name.clone(),
list.snapshot_id.clone(),
spotify,
)
}
pub fn _process_playlist(
id: String,
name: String,
snapshot_id: String,
spotify: &Spotify,
) -> Playlist {
let mut collected_tracks = Vec::new(); let mut collected_tracks = Vec::new();
let mut tracks_result = spotify.user_playlist_tracks(&id, 100, 0); let mut tracks_result = spotify.user_playlist_tracks(&id, 100, 0);
@@ -135,15 +157,17 @@ impl Playlists {
} }
} }
Playlist { Playlist {
meta: list.clone(), id: id.clone(),
name: name.clone(),
snapshot_id: snapshot_id.clone(),
tracks: collected_tracks, tracks: collected_tracks,
} }
} }
fn needs_download(&self, remote: &SimplifiedPlaylist) -> bool { fn needs_download(&self, remote: &SimplifiedPlaylist) -> bool {
for local in self.store.read().expect("can't readlock playlists").iter() { for local in self.store.read().expect("can't readlock playlists").iter() {
if local.meta.id == remote.id { if local.id == remote.id {
return local.meta.snapshot_id != remote.snapshot_id; return local.snapshot_id != remote.snapshot_id;
} }
} }
true true
@@ -152,7 +176,7 @@ impl Playlists {
fn append_or_update(&self, updated: &Playlist) -> usize { fn append_or_update(&self, updated: &Playlist) -> usize {
let mut store = self.store.write().expect("can't writelock playlists"); let mut store = self.store.write().expect("can't writelock playlists");
for (index, mut local) in store.iter_mut().enumerate() { for (index, mut local) in store.iter_mut().enumerate() {
if local.meta.id == updated.meta.id { if local.id == updated.id {
*local = updated.clone(); *local = updated.clone();
return index; return index;
} }
@@ -163,7 +187,7 @@ impl Playlists {
pub fn delete_playlist(&self, id: &str) { pub fn delete_playlist(&self, id: &str) {
let mut store = self.store.write().expect("can't writelock playlists"); let mut store = self.store.write().expect("can't writelock playlists");
if let Some(position) = store.iter().position(|ref i| i.meta.id == id) { if let Some(position) = store.iter().position(|ref i| i.id == id) {
if self.spotify.delete_playlist(id) { if self.spotify.delete_playlist(id) {
store.remove(position); store.remove(position);
self.save_cache(); self.save_cache();
@@ -195,13 +219,13 @@ impl Playlists {
while let Some(ref lists) = lists_result.clone() { while let Some(ref lists) = lists_result.clone() {
for remote in &lists.items { for remote in &lists.items {
// remove from stale playlists so we won't prune it later on // remove from stale playlists so we won't prune it later on
if let Some(index) = stale_lists.iter().position(|x| x.meta.id == remote.id) { if let Some(index) = stale_lists.iter().position(|x| x.id == remote.id) {
stale_lists.remove(index); stale_lists.remove(index);
} }
if self.needs_download(remote) { if self.needs_download(remote) {
info!("updating playlist {}", remote.name); info!("updating playlist {}", remote.name);
let playlist = Self::process_playlist(&remote, &self.spotify); let playlist = Self::process_simplified_playlist(remote, &self.spotify);
self.append_or_update(&playlist); self.append_or_update(&playlist);
// trigger redraw // trigger redraw
self.ev.trigger(); self.ev.trigger();
@@ -226,9 +250,9 @@ impl Playlists {
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.position(|x| x.meta.id == stale.meta.id); .position(|x| x.id == stale.id);
if let Some(index) = index { if let Some(index) = index {
debug!("removing stale list: {:?}", stale.meta.name); debug!("removing stale list: {:?}", stale.name);
self.store.write().unwrap().remove(index); self.store.write().unwrap().remove(index);
} }
} }

View File

@@ -35,7 +35,7 @@ impl PlaylistView {
if let Some(playlist) = current { if let Some(playlist) = current {
let playlists = self.playlists.clone(); let playlists = self.playlists.clone();
let id = playlist.meta.id.clone(); let id = playlist.id.clone();
let dialog = Dialog::text("Are you sure you want to delete this playlist?") let dialog = Dialog::text("Are you sure you want to delete this playlist?")
.padding((1, 1, 1, 0)) .padding((1, 1, 1, 0))
.title("Delete playlist") .title("Delete playlist")

View File

@@ -68,7 +68,7 @@ impl QueueView {
list_select.add_item("[Create new]", None); list_select.add_item("[Create new]", None);
for list in playlists.items().iter() { for list in playlists.items().iter() {
list_select.add_item(list.meta.name.clone(), Some(list.meta.id.clone())); list_select.add_item(list.name.clone(), Some(list.id.clone()));
} }
list_select.set_on_submit(move |s, selected| { list_select.set_on_submit(move |s, selected| {

View File

@@ -129,7 +129,7 @@ impl SearchView {
.playlists .playlists
.items .items
.iter() .iter()
.map(|sp| Playlists::process_playlist(sp, &&spotify)) .map(|sp| Playlists::process_simplified_playlist(sp, &&spotify))
.collect(); .collect();
let mut r = playlists.write().unwrap(); let mut r = playlists.write().unwrap();
*r = pls; *r = pls;