SaveTool: add rest of response handling.

This commit is contained in:
Guillaume Jacquemin 2021-07-28 14:18:39 +02:00
parent 51127241ef
commit ab124174b0
3 changed files with 69 additions and 0 deletions

View File

@ -32,6 +32,8 @@
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <windef.h>
#include <winuser.h>
#include <processthreadsapi.h>
@ -303,6 +305,56 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
_queue.addToast(Toast::Type::Error, Utility::formatString("The request failed with error code {}: {}", r.status_code, r.reason));
return;
}
using json = nlohmann::json;
json response = json::parse(r.text);
struct Version {
explicit Version(const std::string& str) {
std::size_t start_point = 0;
if(str[0] == 'v') {
start_point++;
}
major = std::atoi(str.c_str() + start_point);
start_point = str.find('.', start_point) + 1;
minor = std::atoi(str.c_str() + start_point);
start_point = str.find('.', start_point) + 1;
patch = std::atoi(str.c_str() + start_point);
}
Int major;
Int minor;
Int patch;
bool operator==(const Version& other) const {
return (major == other.major) && (minor == other.minor) && (patch == other.patch);
}
bool operator>(const Version& other) const {
return ( major * 10000 + minor * 100 + patch) >
(other.major * 10000 + other.minor * 100 + other.patch);
}
};
static const Version current_ver{"0.9.9"};
Version latest_ver{response[0]["tag_name"]};
if(latest_ver == current_ver) {
_queue.addToast(Toast::Type::Success, "The application is already up to date.");
}
else if(latest_ver > current_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information.",
std::chrono::milliseconds{5000});
_updateAvailable = true;
_latestVersion = Utility::formatString("{}.{}.{}", latest_ver.major, latest_ver.minor, latest_ver.patch);
_releaseLink = response[0]["html_url"];
_downloadLink = response[0]["assets"][0]["browser_download_url"];
Utility::Debug{} << _downloadLink.c_str();
}
else if(current_ver > latest_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???");
}
}
void SaveTool::initialiseConfiguration() {

View File

@ -199,4 +199,9 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener
bool _checkUpdatesOnStartup{true};
bool _unsafeMode{false};
bool _updateAvailable{false};
std::string _latestVersion;
std::string _releaseLink;
std::string _downloadLink;
};

View File

@ -67,6 +67,18 @@ void SaveTool::drawMainMenu() {
_thread = std::thread{[this]{ checkForUpdates(); }};
}
if(_updateAvailable) {
ImGui::AlignTextToFramePadding();
ImGui::Text("Version %s is available.", _latestVersion.c_str());
if(ImGui::Button(ICON_FA_FILE_SIGNATURE " Release notes")) {
openUri(_releaseLink);
}
ImGui::SameLine();
if(ImGui::Button(ICON_FA_DOWNLOAD " Download now")) {
openUri(_downloadLink);
}
}
ImGui::EndMenu();
}