From 2a6a19f12b83eec2ce64c1657a79a6e46d98ff00 Mon Sep 17 00:00:00 2001 From: Curve Date: Fri, 19 Mar 2021 21:22:32 +0100 Subject: [PATCH] refactor: version check --- src/helper/json/bindings.hpp | 14 ++++++++++++++ src/helper/version/check.cpp | 14 ++++---------- src/helper/version/check.hpp | 16 +++++++++++++++- src/ui/impl/webview/webview.cpp | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/helper/json/bindings.hpp b/src/helper/json/bindings.hpp index 1b53d2c..71ef461 100644 --- a/src/helper/json/bindings.hpp +++ b/src/helper/json/bindings.hpp @@ -1,5 +1,6 @@ #pragma once #include "../../core/global/globals.hpp" +#include "../../helper/version/check.hpp" #include namespace nlohmann @@ -147,6 +148,19 @@ namespace nlohmann j.at("settings").get_to(obj.settings); } }; + template <> struct adl_serializer + { + static void to_json(json &j, const Soundux::Objects::VersionStatus &obj) + { + j = {{"current", obj.current}, {"latest", obj.latest}, {"outdated", obj.outdated}}; + } + static void from_json(const json &j, Soundux::Objects::VersionStatus &obj) + { + j.at("latest").get_to(obj.latest); + j.at("current").get_to(obj.current); + j.at("outdated").get_to(obj.outdated); + } + }; #if defined(__linux__) template <> struct adl_serializer { diff --git a/src/helper/version/check.cpp b/src/helper/version/check.cpp index c10614d..317b11c 100644 --- a/src/helper/version/check.cpp +++ b/src/helper/version/check.cpp @@ -1,11 +1,12 @@ #include "check.hpp" #include #include +#include #include httplib::Client VersionCheck::client("https://api.github.com"); -bool VersionCheck::isLatest() +std::optional VersionCheck::getStatus() { auto githubTags = client.Get("/repos/Soundux/Soundux/tags"); @@ -26,14 +27,7 @@ bool VersionCheck::isLatest() { if (match[1].matched) { - if (match[1] != SOUNDUX_VERSION) - { - Fancy::fancy.logTime().warning() << "Current version is " << SOUNDUX_VERSION - << " latest version is " << match[1] << std::endl; - return false; - } - Fancy::fancy.logTime().success() << "You are using the latest version of soundux" << std::endl; - return true; + return Soundux::Objects::VersionStatus{SOUNDUX_VERSION, match[1], match[1] != SOUNDUX_VERSION}; } Fancy::fancy.logTime().warning() << "Failed to fetch latest version" << std::endl; } @@ -56,5 +50,5 @@ bool VersionCheck::isLatest() { Fancy::fancy.logTime().warning() << "Request failed!" << std::endl; } - return true; + return std::nullopt; } \ No newline at end of file diff --git a/src/helper/version/check.hpp b/src/helper/version/check.hpp index 7e44cd4..4ecffea 100644 --- a/src/helper/version/check.hpp +++ b/src/helper/version/check.hpp @@ -1,10 +1,24 @@ #pragma once #include +#include + +namespace Soundux +{ + namespace Objects + { + struct VersionStatus + { + std::string current; + std::string latest; + bool outdated; + }; + } // namespace Objects +} // namespace Soundux class VersionCheck { static httplib::Client client; public: - static bool isLatest(); + static std::optional getStatus(); }; \ No newline at end of file diff --git a/src/ui/impl/webview/webview.cpp b/src/ui/impl/webview/webview.cpp index a5da131..b7f10f0 100644 --- a/src/ui/impl/webview/webview.cpp +++ b/src/ui/impl/webview/webview.cpp @@ -78,7 +78,7 @@ namespace Soundux::Objects [](const std::string &url) { return Globals::gYtdl.download(url); }); webview.addCallback("stopYoutubeDLDownload", []() { Globals::gYtdl.killDownload(); }); webview.addCallback("getSystemInfo", []() -> std::string { return SystemInfo::getSummary(); }); - webview.addCallback("isLatest", []() { return VersionCheck::isLatest(); }); + webview.addCallback("updateCheck", []() { return VersionCheck::getStatus(); }); #if !defined(__linux__) webview.addCallback("getOutputs", [this]() { return getOutputs(); });