From c612a15033615c2aa2fca7bddf7aa99ba19962a1 Mon Sep 17 00:00:00 2001 From: kraxarn Date: Tue, 21 Apr 2020 22:19:35 +0200 Subject: [PATCH] Add add/remove liked tracks --- src/mainwindow.cpp | 7 +++++++ src/mainwindow.hpp | 1 + src/songmenu.cpp | 12 ++++++++++++ src/spotify/spotify.cpp | 18 ++++++++++++++++++ src/spotify/spotify.hpp | 2 ++ 5 files changed, 40 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5909326df..0d9b0e148 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -760,3 +760,10 @@ bool MainWindow::hasPlaylistSelected() { return playlists->currentRow() >= 0; } + +QString MainWindow::currentLibraryItem() +{ + return libraryList->currentIndex().row() >= 0 + ? libraryList->currentItem()->text(0) + : QString(); +} diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp index e3a0dccb0..23e137d0c 100644 --- a/src/mainwindow.hpp +++ b/src/mainwindow.hpp @@ -71,6 +71,7 @@ class MainWindow : public QMainWindow void openLyrics(const QString &artist, const QString &name); bool hasPlaylistSelected(); QVector playlistTracks(const QString &playlistId); + QString currentLibraryItem(); // I know these should be methods, I'm just lazy QString cacheLocation; QVector *sptPlaylists; diff --git a/src/songmenu.cpp b/src/songmenu.cpp index d7f418f38..842ca116d 100644 --- a/src/songmenu.cpp +++ b/src/songmenu.cpp @@ -32,6 +32,18 @@ SongMenu::SongMenu(const QString &trackId, const QString &artist, const QString .arg(QString(trackId).remove(0, QString("spotify:track:").length()))); }); addSeparator(); + // Add/remove liked + auto isLiked = mainWindow->currentLibraryItem() == "Liked"; + auto toggleLiked = addAction( + Icon::get(isLiked ? "starred-symbolic" : "non-starred-symbolic"), + isLiked ? "Remove from liked" : "Add to liked"); + QAction::connect(toggleLiked, &QAction::triggered, [isLiked, spotify, trackId, mainWindow](bool checked) { + auto status = isLiked ? spotify->removeSavedTrack(trackId) : spotify->addSavedTrack(trackId); + if (!status.isEmpty()) + mainWindow->setStatus(QString("Failed to %1 liked: %2") + .arg(isLiked ? "remove from" : "add to") + .arg(status)); + }); // Add to playlist auto addPlaylist = addMenu(Icon::get("list-add"), "Add to playlist"); auto currentPlaylist = !mainWindow->hasPlaylistSelected() diff --git a/src/spotify/spotify.cpp b/src/spotify/spotify.cpp index 4a2b78590..751a5a66b 100644 --- a/src/spotify/spotify.cpp +++ b/src/spotify/spotify.cpp @@ -417,6 +417,24 @@ QVector Spotify::savedTracks(int offset) return tracks; } +QString Spotify::addSavedTrack(const QString &trackId) +{ + QVariantMap body; + body["ids"] = QStringList({ + trackId.right(trackId.length() - QString("spotify:track:").length()) + }); + return put("me/tracks", &body); +} + +QString Spotify::removeSavedTrack(const QString &trackId) +{ + QVariantMap body; + body["ids"] = QStringList({ + trackId.right(trackId.length() - QString("spotify:track:").length()) + }); + return del("me/tracks", &body); +} + QVector Spotify::newReleases() { auto json = get("browse/new-releases"); diff --git a/src/spotify/spotify.hpp b/src/spotify/spotify.hpp index 1ada22a24..2044fb378 100644 --- a/src/spotify/spotify.hpp +++ b/src/spotify/spotify.hpp @@ -59,6 +59,8 @@ namespace spt QVector topTracks(); QVector savedAlbums(); QVector savedTracks(int offset = 0); + QString addSavedTrack(const QString &trackId); + QString removeSavedTrack(const QString &trackId); QVector recentlyPlayed(); QVector newReleases();