forward declare Updater

This commit is contained in:
2023-01-02 12:05:20 +01:00
parent 8949c37891
commit 386ea5b6ad
2 changed files with 30 additions and 19 deletions

View File

@@ -2,21 +2,28 @@
#include "GuiWidget.h" #include "GuiWidget.h"
#include "updater/Updater.h"
#include <future> #include <future>
#include <memory>
namespace td { namespace td {
namespace utils {
class Updater;
} // namespace utils
namespace gui { namespace gui {
class UpdateMenu : public GuiWidget { class UpdateMenu : public GuiWidget {
private: private:
bool m_Opened; bool m_Opened;
std::string m_Error; std::string m_Error;
utils::Updater m_Updater; std::unique_ptr<utils::Updater> m_Updater;
std::shared_future<bool> m_UpdateAvailable; std::shared_future<bool> m_UpdateAvailable;
public: public:
UpdateMenu(client::Client* client); UpdateMenu(client::Client* client);
virtual ~UpdateMenu();
virtual void Render(); virtual void Render();
private: private:

View File

@@ -1,5 +1,7 @@
#include "render/gui/UpdateMenu.h" #include "render/gui/UpdateMenu.h"
#include "updater/Updater.h"
#include "render/gui/imgui/imgui.h" #include "render/gui/imgui/imgui.h"
#include <chrono> #include <chrono>
@@ -7,10 +9,12 @@
namespace td { namespace td {
namespace gui { namespace gui {
UpdateMenu::UpdateMenu(client::Client* client) : GuiWidget(client), m_Opened(true) { UpdateMenu::UpdateMenu(client::Client* client) : GuiWidget(client), m_Opened(true), m_Updater(std::make_unique<utils::Updater>()) {
CheckUpdates(); CheckUpdates();
} }
UpdateMenu::~UpdateMenu() {}
void UpdateMenu::Render() { void UpdateMenu::Render() {
RenderErrorPopup(); RenderErrorPopup();
if (m_Opened) { if (m_Opened) {
@@ -20,40 +24,40 @@ void UpdateMenu::Render() {
bool updateAvailable = m_UpdateAvailable.get(); bool updateAvailable = m_UpdateAvailable.get();
if (updateAvailable) { if (updateAvailable) {
if (m_Updater.IsFileWrited()) { if (m_Updater->IsFileWrited()) {
ImGui::Text("The update is now installed"); ImGui::Text("The update is now installed");
ImGui::Text("The game needs to be restarted"); ImGui::Text("The game needs to be restarted");
} else if (m_Updater.IsDownloadComplete()) { } else if (m_Updater->IsDownloadComplete()) {
ImGui::Text("Download done!"); ImGui::Text("Download done!");
if (ImGui::Button("Install")) { if (ImGui::Button("Install")) {
if (!m_Updater.WriteFile()) { if (!m_Updater->WriteFile()) {
m_Error = "Failed to write file !\n"; m_Error = "Failed to write file !\n";
ImGui::OpenPopup("UpdateError"); ImGui::OpenPopup("UpdateError");
} }
} }
if (ImGui::Button("Cancel")) { if (ImGui::Button("Cancel")) {
m_Updater.CancelDownload(); m_Updater->CancelDownload();
m_Updater.ClearCache(); m_Updater->ClearCache();
} }
} else { } else {
if (m_Updater.GetDownloadProgress() > 0) { if (m_Updater->GetDownloadProgress() > 0) {
ImGui::Text("Downloading ..."); ImGui::Text("Downloading ...");
ImGui::ProgressBar(m_Updater.GetDownloadProgress()); ImGui::ProgressBar(m_Updater->GetDownloadProgress());
if (ImGui::Button("Cancel")) { if (ImGui::Button("Cancel")) {
m_Updater.CancelDownload(); m_Updater->CancelDownload();
} }
} else { } else {
ImGui::Text("An update is available!"); ImGui::Text("An update is available!");
ImGui::Separator(); ImGui::Separator();
ImGui::Text("Current version : %s", m_Updater.GetCurrentVersion().c_str()); ImGui::Text("Current version : %s", m_Updater->GetCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.GetLastVersion().c_str()); ImGui::Text("Last version : %s", m_Updater->GetLastVersion().c_str());
bool canDownloadFile = m_Updater.CanUpdate(); bool canDownloadFile = m_Updater->CanUpdate();
if (!canDownloadFile) ImGui::BeginDisabled(); if (!canDownloadFile) ImGui::BeginDisabled();
if (ImGui::Button("Download")) { if (ImGui::Button("Download")) {
m_Updater.DownloadUpdate(); m_Updater->DownloadUpdate();
} }
if (!canDownloadFile) ImGui::EndDisabled(); if (!canDownloadFile) ImGui::EndDisabled();
@@ -67,8 +71,8 @@ void UpdateMenu::Render() {
} else { } else {
ImGui::Text("No update available!"); ImGui::Text("No update available!");
ImGui::Separator(); ImGui::Separator();
ImGui::Text("Current version : %s", m_Updater.GetCurrentVersion().c_str()); ImGui::Text("Current version : %s", m_Updater->GetCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.GetLastVersion().c_str()); ImGui::Text("Last version : %s", m_Updater->GetLastVersion().c_str());
} }
} else { } else {
ImGui::Text("Checking updates ..."); ImGui::Text("Checking updates ...");
@@ -89,7 +93,7 @@ bool UpdateMenu::IsUpdateChecked() {
} }
void UpdateMenu::CheckUpdates() { void UpdateMenu::CheckUpdates() {
m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater.CheckUpdate();}); m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater->CheckUpdate();});
} }
} // namespace gui } // namespace gui