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

Unsave already saved songs with 's' #104

Merged
merged 3 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,35 @@ impl App {
}
}

pub fn toggle_save_track(&mut self, track_id: String) {
if let Some(spotify) = &self.spotify {
match spotify.current_user_saved_tracks_contains(&[track_id.clone()]) {
Ok(saved) => {
if saved[0] {
Copy link
Owner

Choose a reason for hiding this comment

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

This is probably overkill, but to be safe let's use saved.first() instead of the array accessor [0]. We'd then need to handle the option.

match spotify.current_user_saved_tracks_delete(&[track_id]) {
Ok(()) => {}
Err(e) => {
self.handle_error(e);
}
}
} else {
match spotify.current_user_saved_tracks_add(&[track_id]) {
Ok(()) => {}
Err(e) => {
self.handle_error(e);
}
}
}
}
Err(e) => {
self.handle_error(e);
}
}
};
}

// Currently unused but keep for future changes
#[allow(dead_code)]
Copy link
Owner

Choose a reason for hiding this comment

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

Are you able to delete this? No need to allow dead code (shouldn't exist in master).

pub fn save_tracks(&mut self, track_ids: Vec<String>) {
if let Some(spotify) = &self.spotify {
match spotify.current_user_saved_tracks_add(&track_ids) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/album_tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn handler(key: Key, app: &mut App) {
.get(app.saved_album_tracks_index)
{
if let Some(track_id) = &selected_track.id {
app.save_tracks(vec![track_id.clone()]);
app.toggle_save_track(track_id.clone());
};
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/playbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn handler(key: Key, app: &mut App) {
if let Some(playing_context) = &app.current_playback_context {
if let Some(track) = &playing_context.item {
if let Some(id) = track.id.to_owned() {
app.save_tracks(vec![id]);
app.toggle_save_track(id);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/recently_played.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn handler(key: Key, app: &mut App) {
recently_played_result.items.get(app.recently_played.index)
{
if let Some(track_id) = &selected_track.track.id {
app.save_tracks(vec![track_id.clone()]);
app.toggle_save_track(track_id.clone());
};
};
};
Expand Down