Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add support for following a playlist
  • Loading branch information
echoSayonara committed Nov 27, 2019
commit 81494b5d70736853da4c916dcde2039514b8b8ca
35 changes: 35 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,39 @@ impl App {
}
}
}

pub fn user_follow_playlists(&mut self) {
if let (Some(playlists), Some(selected_index), Some(spotify)) = (
&self.search_results.playlists,
self.search_results.selected_playlists_index,
&self.spotify,
) {
let selected_playlist: &SimplifiedPlaylist = &playlists.playlists.items[selected_index];
let selected_id = &selected_playlist.id;
let selected_public = selected_playlist.public;
let selected_owner_id = &selected_playlist.owner.id;
if let Err(e) = spotify.user_playlist_follow_playlist(
&selected_owner_id,
&selected_id,
selected_public,
) {
self.handle_error(e);
}
}
}

pub fn user_unfollow_playlists(&mut self) {
if let (Some(playlists), Some(selected_index), Some(user), Some(spotify)) = (
&self.playlists,
self.selected_playlist_index,
&self.user,
&self.spotify,
) {
let selected_playlist = &playlists.items[selected_index];
let selected_id = &selected_playlist.id;
if let Err(e) = spotify.user_playlist_unfollow(&user.id, &selected_id) {
self.handle_error(e);
}
}
}
}
11 changes: 11 additions & 0 deletions src/handlers/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ pub fn handler(key: Key, app: &mut App) {
}
};
}
Key::Char('D') => {
app.user_unfollow_playlists();
if let Some(spotify) = &app.spotify {
let playlists = spotify.current_user_playlists(app.large_search_limit, None);

match playlists {
Ok(p) => app.playlists = Some(p),
Err(e) => app.handle_error(e),
};
}
}
_ => {}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/handlers/search_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,17 @@ pub fn handler(key: Key, app: &mut App) {
SearchResultBlock::AlbumSearch => app.current_user_saved_album_add(),
SearchResultBlock::SongSearch => {}
SearchResultBlock::ArtistSearch => app.user_follow_artists(),
SearchResultBlock::PlaylistSearch => {}
SearchResultBlock::PlaylistSearch => {
app.user_follow_playlists();
if let Some(spotify) = &app.spotify {
let playlists = spotify.current_user_playlists(app.large_search_limit, None);

match playlists {
Ok(p) => app.playlists = Some(p),
Err(e) => app.handle_error(e),
}
}
}
SearchResultBlock::Empty => {}
},
// Add `s` to "see more" on each option
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ use redirect_uri::redirect_uri_web_server;
use user_config::UserConfig;
use util::{Event, Events};

const SCOPES: [&str; 11] = [
const SCOPES: [&str; 13] = [
"playlist-read-collaborative",
"playlist-read-private",
"playlist-modify-private",
"playlist-modify-public",
"user-follow-read",
"user-follow-modify",
"user-library-modify",
Expand Down