Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial stab at opening a playlist #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -143,6 +143,7 @@ pub enum TrackTableContext {
MyPlaylists,
AlbumSearch,
PlaylistSearch,
PlaylistOpen,
SavedTracks,
}

Expand Down Expand Up @@ -217,6 +218,7 @@ pub struct App {
pub large_search_limit: u32,
pub library: Library,
pub playback_params: PlaybackParams,
pub open_playlist: Option<FullPlaylist>,
pub playlist_tracks: Vec<PlaylistTrack>,
pub playlists: Option<Page<SimplifiedPlaylist>>,
pub recently_played: SpotifyResultAndSelectedIndex<Option<CursorBasedPage<PlayHistory>>>,
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 32 additions & 1 deletion src/handlers/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TODO here isn't possible - we need to manage pagination separately.


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,
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/handlers/track_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -111,6 +128,7 @@ pub fn handler(key: Key, app: &mut App) {
}
TrackTableContext::AlbumSearch => {}
TrackTableContext::PlaylistSearch => {}
TrackTableContext::PlaylistOpen => {}
},
None => {}
};
Expand All @@ -125,6 +143,7 @@ pub fn handler(key: Key, app: &mut App) {
}
TrackTableContext::AlbumSearch => {}
TrackTableContext::PlaylistSearch => {}
TrackTableContext::PlaylistOpen => {}
},
None => {}
};
Expand Down