Skip to content

Commit

Permalink
Add add/remove liked tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Apr 21, 2020
1 parent 05047d0 commit c612a15
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,10 @@ bool MainWindow::hasPlaylistSelected()
{
return playlists->currentRow() >= 0;
}

QString MainWindow::currentLibraryItem()
{
return libraryList->currentIndex().row() >= 0
? libraryList->currentItem()->text(0)
: QString();
}
1 change: 1 addition & 0 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MainWindow : public QMainWindow
void openLyrics(const QString &artist, const QString &name);
bool hasPlaylistSelected();
QVector<spt::Track> playlistTracks(const QString &playlistId);
QString currentLibraryItem();
// I know these should be methods, I'm just lazy
QString cacheLocation;
QVector<spt::Playlist> *sptPlaylists;
Expand Down
12 changes: 12 additions & 0 deletions src/songmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions src/spotify/spotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,24 @@ QVector<Track> 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<Album> Spotify::newReleases()
{
auto json = get("browse/new-releases");
Expand Down
2 changes: 2 additions & 0 deletions src/spotify/spotify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace spt
QVector<Track> topTracks();
QVector<Album> savedAlbums();
QVector<Track> savedTracks(int offset = 0);
QString addSavedTrack(const QString &trackId);
QString removeSavedTrack(const QString &trackId);
QVector<Track> recentlyPlayed();
QVector<Album> newReleases();

Expand Down

0 comments on commit c612a15

Please sign in to comment.