Skip to content

Commit

Permalink
Add liked albums button (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
prophetarmed authored Jul 5, 2024
1 parent ef3197a commit 62037fc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/include/lib/spotify/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ namespace lib

void saved_albums(const paged_callback<saved_album> &callback) const;

void add_saved_albums(const std::vector<std::string> &album_ids,
lib::callback<std::string> &callback);

void remove_saved_albums(const std::vector<std::string> &album_ids,
lib::callback<std::string> &callback);

void is_saved_album(const std::vector<std::string> &album_ids,
lib::callback<std::vector<bool>> &callback);

/**
* @deprecated Use with pagination instead
*/
Expand Down
24 changes: 23 additions & 1 deletion lib/src/spotifyapi/library.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "lib/spotify/api.hpp"

// Currently unavailable:
// me/albums/contains
// me/shows
// me/shows/contains

Expand All @@ -10,6 +9,29 @@ void lib::spt::api::saved_albums(const paged_callback<saved_album> &callback) co
request.get_page<saved_album>("me/albums", {}, callback);
}

void lib::spt::api::add_saved_albums(const std::vector<std::string> &album_ids,
lib::callback<std::string> &callback)
{
put("me/albums", {
{"ids", album_ids},
}, callback);
}

void lib::spt::api::remove_saved_albums(const std::vector<std::string> &album_ids,
lib::callback<std::string> &callback)
{
del("me/albums", {
{"ids", album_ids},
}, callback);
}

void lib::spt::api::is_saved_album(const std::vector<std::string> &album_ids,
lib::callback<std::vector<bool>> &callback)
{
get(lib::fmt::format("me/albums/contains?ids={}",
lib::strings::join(album_ids, ",")), callback);
}

void lib::spt::api::saved_tracks(lib::callback<std::vector<lib::spt::track>> &callback)
{
get_items("me/tracks?limit=50", callback);
Expand Down
53 changes: 52 additions & 1 deletion src/menu/album.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ Menu::Album::Album(lib::spt::api &spotify, lib::cache &cache,
trackCount = addAction("...");
trackCount->setEnabled(false);
addSeparator();

auto *playShuffle = addAction(Icon::get("media-playlist-shuffle"), "Shuffle play");

QAction::connect(playShuffle, &QAction::triggered,
this, &Menu::Album::onShuffle);

toggleLikedAlbum = addAction("Add to liked albums");
toggleLikedAlbum->setEnabled(false);

QAction::connect(toggleLikedAlbum, &QAction::triggered,
this, &Menu::Album::onLikeAlbum);

auto *share = addMenu(Icon::get("document-share"), "Share");

auto *copyLink = share->addAction("Copy album link");
Expand All @@ -38,6 +44,13 @@ Menu::Album::Album(lib::spt::api &spotify, lib::cache &cache,
addToPlaylist->setEnabled(false);
addMenu(addToPlaylist);

spotify.is_saved_album({albumId}, [this](const std::vector<bool> &likes)
{
auto liked = !likes.empty() && likes.front();
this->setLikedAlbum(liked);
this->toggleLikedAlbum->setEnabled(true);
});

album = cache.get_album(albumId);
if (album.is_valid())
{
Expand Down Expand Up @@ -103,6 +116,31 @@ void Menu::Album::onShuffle(bool /*checked*/)
});
}

void Menu::Album::onLikeAlbum(bool /*checked*/)
{
auto callback = [this](const std::string &status)
{
if (status.empty())
{
return;
}

StatusMessage::error(QString("Failed to %1 album: %2")
.arg(isLiked ? "unlike" : "like")
.arg(QString::fromStdString(status)));
};

std::vector<std::string> albumIds = { album.id };

if (isLiked)
{
spotify.remove_saved_albums(albumIds, callback);
} else
{
spotify.add_saved_albums(albumIds, callback);
}
}

void Menu::Album::onCopyLink(bool /*checked*/)
{
QApplication::clipboard()->setText(QString("https://open.spotify.com/album/%1")
Expand All @@ -123,6 +161,19 @@ void Menu::Album::onOpenInSpotify(bool /*checked*/)
MainWindow::find(parentWidget()));
}

void Menu::Album::setLikedAlbum(bool liked)
{
isLiked = liked;

toggleLikedAlbum->setIcon(Icon::get(liked
? QStringLiteral("list-remove")
: QStringLiteral("list-add")));

toggleLikedAlbum->setText(liked
? QStringLiteral("Remove from liked albums")
: QStringLiteral("Add to liked albums"));
}

void Menu::Album::tracksLoaded()
{
if (addToPlaylist != nullptr)
Expand Down
5 changes: 5 additions & 0 deletions src/menu/album.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ namespace Menu
const std::string &albumId, QWidget *parent);

private:
bool isLiked = false;

std::vector<lib::spt::track> tracks;
lib::spt::album album;
lib::spt::api &spotify;
lib::cache &cache;

QAction *toggleLikedAlbum = nullptr;
QAction *trackCount = nullptr;
AddToPlaylist *addToPlaylist = nullptr;

void setLikedAlbum(bool liked);
void tracksLoaded();
auto getTrackIds() const -> std::vector<std::string>;

void onShuffle(bool checked);
void onLikeAlbum(bool checked);
void onCopyLink(bool checked);
void onCopyName(bool checked);
void onOpenInSpotify(bool checked);
Expand Down

0 comments on commit 62037fc

Please sign in to comment.