Skip to content

Commit

Permalink
Merge pull request #6 from BttrDrgn/sharing-is-caring
Browse files Browse the repository at this point in the history
Share button for currently playing station
  • Loading branch information
BttrDrgn authored May 14, 2022
2 parents 6062472 + e92da2b commit 367ee94
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
56 changes: 29 additions & 27 deletions src/app/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ void api::get_places()

std::thread([]
{
httplib::Client cli(API_URL);
httplib::Client cli(HOST_NAME);

if (httplib::Result res = cli.Get(PLACES_ENDPOINT))
{
logger::log_info(logger::va("Accessing %s%s", API_URL, PLACES_ENDPOINT));
logger::log_info(logger::va("Accessing %s%s", HOST_NAME, PLACES_ENDPOINT));

if (res->status == 200)
{
Expand Down Expand Up @@ -91,11 +91,11 @@ void api::get_details(const place_t& place_in)

std::thread([place_in]
{
httplib::Client cli(API_URL);
httplib::Client cli(HOST_NAME);

if (httplib::Result res = cli.Get(PLACE_DETAIL_ENDPOINT(&place_in.id[0])))
{
logger::log_info(logger::va("Accessing %s%s", API_URL, PLACE_DETAIL_ENDPOINT(&place_in.id[0])));
logger::log_info(logger::va("Accessing %s%s", HOST_NAME, PLACE_DETAIL_ENDPOINT(&place_in.id[0])));
if (res->status == 200)
{
nl::json json = nl::json::parse(res->body);
Expand Down Expand Up @@ -135,32 +135,34 @@ void api::get_details(const place_t& place_in)
}).detach();
}

void api::get_station(const std::string& id)
std::string api::get_shareable_url(const std::string& id)
{
api::stations_done = false;
std::string url;

std::thread([id]
//This one isnt threaded; I believe this should be fast enough to not cause issues when playing
httplib::Client cli(HOST_NAME);

if (httplib::Result res = cli.Get(STATION_ENDPOINT(&id[0])))
{
logger::log_info(logger::va("Accessing %s%s", HOST_NAME, STATION_ENDPOINT(&id[0])));

if (res->status == 200)
{
httplib::Client cli(API_URL);
nl::json json = nl::json::parse(res->body);
api::version_check(json);

if (httplib::Result res = cli.Get(STATION_ENDPOINT(&id[0])))
{
logger::log_info(logger::va("Accessing %s%s", API_URL, STATION_ENDPOINT(&id[0])));
if (res->status == 200)
{
nl::json json = nl::json::parse(res->body);
api::version_check(json);
url = json["data"]["url"].dump();
url.erase(std::remove(url.begin(), url.end(), '\"'), url.end());

//Finish
api::stations_done = true;
json.clear();
}
else
{
logger::log_error("An error occured when gathering station data!");
}
}
}).detach();
json.clear();
}
else
{
logger::log_error("An error occured when getting shareable url!");
}
}

return url;
}

void api::search_stations(const std::string& str)
Expand All @@ -176,11 +178,11 @@ void api::search_stations(const std::string& str)

std::thread([str]
{
httplib::Client cli(API_URL);
httplib::Client cli(HOST_NAME);

if (httplib::Result res = cli.Get(SEARCH_ENDPOINT(&str[0])))
{
logger::log_info(logger::va("Accessing %s%s", API_URL, SEARCH_ENDPOINT(&str[0])));
logger::log_info(logger::va("Accessing %s%s", HOST_NAME, SEARCH_ENDPOINT(&str[0])));
if (res->status == 200)
{
nl::json json = nl::json::parse(res->body);
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/api.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#define API_URL "http://radio.garden"
#define HOST_NAME "http://radio.garden"
#define VERSION 1
#define VERSION_HASH "\"bd23e54\"" //radio.garden git commit hash
#define PLACES_ENDPOINT "/api/ara/content/places"
Expand All @@ -27,7 +27,7 @@ class api
static void get_places();
static void filter_place(const std::string& key);
static void get_details(const place_t& place_in);
static void get_station(const std::string& id);
static std::string get_shareable_url(const std::string& id);
static void search_stations(const std::string& str);

static std::vector<place_t> places;
Expand Down
2 changes: 1 addition & 1 deletion src/app/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void audio::play(const std::string& url)
{
std::thread([url]
{
std::string final_url = logger::va("%s%s", API_URL, AUDIO_ENDPOINT(&url[0]));
std::string final_url = logger::va("%s%s", HOST_NAME, AUDIO_ENDPOINT(&url[0]));
audio::currently_playing.title = "N/A";
audio::currently_playing.url = url;
audio::paused = false;
Expand Down
20 changes: 20 additions & 0 deletions src/app/menus/menus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,26 @@ void menus::actions()
api::get_places();
}

ImGui::SameLine();

if (audio::currently_playing.station.title != "N/A")
{
if (ImGui::Button("Share Station"))
{
std::string url = logger::va("%s%s", HOST_NAME, &api::get_shareable_url(audio::currently_playing.station.id)[0]);

ImGuiIO& io = ImGui::GetIO();
io.SetClipboardTextFn(io.ClipboardUserData, &url[0]);
}
}

if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("Copies the link to the\ncurrently playing station");
ImGui::EndTooltip();
}

ImGui::NewLine();

ImGui::Text("Audio Controls");
Expand Down

0 comments on commit 367ee94

Please sign in to comment.