diff --git a/src/app.rs b/src/app.rs index 7e14bfa9..46da3828 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,7 +9,7 @@ use rspotify::spotify::model::offset::for_position; use rspotify::spotify::model::offset::Offset; use rspotify::spotify::model::page::{CursorBasedPage, Page}; use rspotify::spotify::model::playing::PlayHistory; -use rspotify::spotify::model::playlist::{PlaylistTrack, SimplifiedPlaylist}; +use rspotify::spotify::model::playlist::{PlaylistTrack, SimplifiedPlaylist, FullPlaylist}; use rspotify::spotify::model::search::{ SearchAlbums, SearchArtists, SearchPlaylists, SearchTracks, }; @@ -143,6 +143,7 @@ pub enum TrackTableContext { MyPlaylists, AlbumSearch, PlaylistSearch, + PlaylistOpen, SavedTracks, } @@ -217,6 +218,7 @@ pub struct App { pub large_search_limit: u32, pub library: Library, pub playback_params: PlaybackParams, + pub open_playlist: Option, pub playlist_tracks: Vec, pub playlists: Option>, pub recently_played: SpotifyResultAndSelectedIndex>>, @@ -266,6 +268,7 @@ impl App { input: vec![], input_idx: 0, input_cursor_position: 0, + open_playlist: None, playlist_tracks: vec![], playlists: None, search_results: SearchResult { diff --git a/src/handlers/input.rs b/src/handlers/input.rs index f1a09d0c..d60703cc 100644 --- a/src/handlers/input.rs +++ b/src/handlers/input.rs @@ -49,6 +49,38 @@ pub fn handler(key: Key, app: &mut App) { let country = Country::from_str(&user.country.unwrap_or_else(|| "".to_string())); let input_str: String = app.input.iter().collect(); // Can I run these functions in parellel? + + if input_str.starts_with("https://open.spotify.com/playlist/") { + let id = String::from(input_str + .trim_start_matches("https://open.spotify.com/playlist/") + .split("?") + .next() + .unwrap_or_else(||"")); + + match spotify.playlist(&id, None, None) { + Ok(playlist) => { + let tracks = playlist.tracks.items.clone(); + // TODO: select all the tracks, not just first page + + app.open_playlist = Some(playlist.clone()); + + app.track_table.tracks = tracks + .iter() + .map(|track| track.track.clone()) + .collect::<_>(); + app.playlist_tracks = tracks.clone(); + app.push_navigation_stack(RouteId::TrackTable, ActiveBlock::TrackTable); + } + Err(e) => { + app.handle_error(e); + } + } + + + return; + } + + // Can I run these functions in parallel? match spotify.search_track( &input_str, app.small_search_limit, @@ -100,7 +132,6 @@ pub fn handler(key: Key, app: &mut App) { app.handle_error(e); } } - // On searching for a track, clear the playlist selection app.selected_playlist_index = None; app.push_navigation_stack(RouteId::Search, ActiveBlock::SearchResultBlock); diff --git a/src/handlers/track_table.rs b/src/handlers/track_table.rs index 31d1eac7..69323cb9 100644 --- a/src/handlers/track_table.rs +++ b/src/handlers/track_table.rs @@ -65,6 +65,23 @@ pub fn handler(key: Key, app: &mut App) { }; } TrackTableContext::AlbumSearch => {} + TrackTableContext::PlaylistOpen => { + let TrackTable { + selected_index, + tracks, + .. + } = &app.track_table; + if let Some(_track) = tracks.get(*selected_index) { + + let context_uri = app.open_playlist.clone().map(|p| p.uri.to_owned()); + + app.start_playback( + context_uri, + None, + Some(app.track_table.selected_index), + ); + }; + } TrackTableContext::PlaylistSearch => { let TrackTable { selected_index, @@ -111,6 +128,7 @@ pub fn handler(key: Key, app: &mut App) { } TrackTableContext::AlbumSearch => {} TrackTableContext::PlaylistSearch => {} + TrackTableContext::PlaylistOpen => {} }, None => {} }; @@ -125,6 +143,7 @@ pub fn handler(key: Key, app: &mut App) { } TrackTableContext::AlbumSearch => {} TrackTableContext::PlaylistSearch => {} + TrackTableContext::PlaylistOpen => {} }, None => {} };