diff --git a/App/Initalizer.cs b/App/Initalizer.cs index 9b40a41..7bd1a81 100644 --- a/App/Initalizer.cs +++ b/App/Initalizer.cs @@ -70,12 +70,9 @@ public void Run(string[] args) } } - var UpdateChecker = new UpdateChecker(); - UpdateChecker.currentVersion = System.Reflection.Assembly.GetExecutingAssembly() - .GetName() - .Version - .ToString(); - var infoTextModel = new InfoTextModel(UpdateChecker); + var updateChecker = new UpdateChecker(); + updateChecker.CheckForUpdates(); + var infoTextModel = new InfoTextModel(updateChecker); var mainForm = GuiComponentsProvider.Instance.GetClassImplementing(); var mainPresenter = new MainFormPresenter(mainForm, new MainFormModel(CollectionEditor, UserDialogs), infoTextModel, WebCollectionProvider); diff --git a/App/Interfaces/IUpdateModel.cs b/App/Interfaces/IUpdateModel.cs index 7cfd328..504631a 100644 --- a/App/Interfaces/IUpdateModel.cs +++ b/App/Interfaces/IUpdateModel.cs @@ -1,12 +1,14 @@ -namespace App.Interfaces +using System; + +namespace App.Interfaces { public interface IUpdateModel { - bool IsUpdateAvaliable(); + bool UpdateIsAvailable { get; } bool Error { get; } - string newVersion { get; } - string newVersionLink { get; } - string currentVersion { get; } - void CheckIfUpdateIsAvaliable(); + Version OnlineVersion { get; } + string NewVersionLink { get; } + Version CurrentVersion { get; } + bool CheckForUpdates(); } } \ No newline at end of file diff --git a/App/Presenters/Controls/InfoTextPresenter.cs b/App/Presenters/Controls/InfoTextPresenter.cs index 34cad00..3579a9c 100644 --- a/App/Presenters/Controls/InfoTextPresenter.cs +++ b/App/Presenters/Controls/InfoTextPresenter.cs @@ -11,7 +11,7 @@ public class InfoTextPresenter private const string UpdateAvaliable = "Update is avaliable!({0})"; private const string UpdateError = "Error while checking for updates."; - private const string UpdateNewestVersion = "No updates avaliable ({0})"; + private const string NoUpdatesAvailable = "No updates avaliable ({0})"; private const string LoadedBeatmaps = "Loaded {0} beatmaps"; private const string LoadedCollections = "Loaded {0} collections"; @@ -32,10 +32,10 @@ public InfoTextPresenter(IInfoTextView view, IInfoTextModel model) private void ViewOnUpdateTextClicked(object sender, EventArgs eventArgs) { var updater = Model.GetUpdater(); - updater.CheckIfUpdateIsAvaliable(); + updater.CheckForUpdates(); Model.EmitUpdateTextClicked(); - + SetUpdateText(); } @@ -53,9 +53,11 @@ private void SetUpdateText() var updater = Model.GetUpdater(); if (updater != null) { - if (updater.IsUpdateAvaliable()) + _view.ColorUpdateText = true; + + if (updater.UpdateIsAvailable) { - _view.UpdateText = string.Format(UpdateAvaliable, updater.newVersion); + _view.UpdateText = string.Format(UpdateAvaliable, updater.OnlineVersion); } else if (updater.Error) { @@ -63,11 +65,13 @@ private void SetUpdateText() } else { - _view.UpdateText = string.Format(UpdateNewestVersion, updater.currentVersion); + _view.ColorUpdateText = false; + _view.UpdateText = string.Format(NoUpdatesAvailable, updater.CurrentVersion); } } else { + _view.ColorUpdateText = false; _view.UpdateText = ""; } } diff --git a/App/SidePanelActionsHandler.cs b/App/SidePanelActionsHandler.cs index d15a52b..ccba6f4 100644 --- a/App/SidePanelActionsHandler.cs +++ b/App/SidePanelActionsHandler.cs @@ -422,10 +422,10 @@ private void DownloadAllMissing(object sender, object data = null) private void FormUpdateTextClicked(object sender, EventArgs args) { var updater = _mainFormPresenter.InfoTextModel.GetUpdater(); - if (updater.IsUpdateAvaliable()) + if (updater.UpdateIsAvailable) { - if (!string.IsNullOrWhiteSpace(updater.newVersionLink)) - Process.Start(updater.newVersionLink); + if (!string.IsNullOrWhiteSpace(updater.NewVersionLink)) + Process.Start(updater.NewVersionLink); } } diff --git a/App/UpdateChecker.cs b/App/UpdateChecker.cs index fb0fcf4..617e398 100644 --- a/App/UpdateChecker.cs +++ b/App/UpdateChecker.cs @@ -1,67 +1,64 @@ using System; +using System.Diagnostics; +using System.Reflection; using App.Interfaces; +using CollectionManagerExtensionsDll.Utils; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace App { public class UpdateChecker : IUpdateModel { - private const string UpdateUrl = "http://osustats.ppy.sh/api/ce/version"; + private const string baseGithubUrl = "https://api.github.com/repos/Piotrekol/CollectionManager"; + private const string githubUpdateUrl = baseGithubUrl + "/releases/latest"; - public bool Error { get; private set; } - public string newVersion { get; private set; } - public string newVersionLink { get; private set; } - public string currentVersion { get; set; } = "???"; - - public bool IsUpdateAvaliable() - { - return CheckForUpdates(); - } - public void CheckIfUpdateIsAvaliable() + public UpdateChecker() { - UpdateVersion(); + var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); + CurrentVersion = new Version(version.ProductVersion); } - private bool CheckForUpdates() + public bool Error { get; private set; } + public Version OnlineVersion { get; private set; } + public string NewVersionLink { get; private set; } + public Version CurrentVersion { get; } + + public bool UpdateIsAvailable => OnlineVersion != null && OnlineVersion > CurrentVersion; + + public bool CheckForUpdates() { - UpdateVersion(); - if (string.IsNullOrWhiteSpace(newVersion)) + var data = GetStringData(githubUpdateUrl); + if (string.IsNullOrEmpty(data)) { Error = true; return false; } - Version verLocal, verOnline; + + JObject json; try { - verLocal = new Version(currentVersion); - verOnline = new Version(newVersion); + json = JObject.Parse(data); } - catch + catch (JsonReaderException) { - return true; + return false; } + var newestReleaseVersion = json["tag_name"].ToString(); + OnlineVersion = new Version(newestReleaseVersion); + NewVersionLink = json["html_url"].ToString(); - return verLocal.CompareTo(verOnline) < 0; + return UpdateIsAvailable; } - private void UpdateVersion() - { - try - { - string contents; - using (var wc = new System.Net.WebClient()) - contents = wc.DownloadString(UpdateUrl); - if (contents.Contains("") || contents.Contains("") || contents.Contains("html>")) - return; - var splited = contents.Split(new[] { ',' }, 2); - newVersionLink = splited[1]; - newVersion = splited[0]; - - } - catch (Exception) + private string GetStringData(string url) + { + using (var wc = new ImpatientWebClient()) { + wc.Headers.Add("user-agent", $"CollectionManager_Updater_{CurrentVersion}"); + return wc.DownloadString(url); } - } } } \ No newline at end of file diff --git a/Common/Interfaces/Controls/IInfoTextView.cs b/Common/Interfaces/Controls/IInfoTextView.cs index 7531dba..710c99f 100644 --- a/Common/Interfaces/Controls/IInfoTextView.cs +++ b/Common/Interfaces/Controls/IInfoTextView.cs @@ -4,7 +4,7 @@ namespace GuiComponents.Interfaces { public interface IInfoTextView { - bool UpdateTextIsClickable { set; } + bool ColorUpdateText { set; } string UpdateText { set; } string BeatmapLoaded { set; } string CollectionsLoaded { set; } diff --git a/GuiComponents/Controls/InfoTextView.cs b/GuiComponents/Controls/InfoTextView.cs index 0918d75..cf43790 100644 --- a/GuiComponents/Controls/InfoTextView.cs +++ b/GuiComponents/Controls/InfoTextView.cs @@ -14,16 +14,22 @@ public InfoTextView() label_UpdateText.Click += (s, a) => { UpdateTextClicked?.Invoke(this, EventArgs.Empty); }; } - public bool UpdateTextIsClickable + public bool ColorUpdateText { set { if (value) { label_UpdateText.Cursor = Cursors.Hand; + label_UpdateText.ForeColor = Color.Crimson; + label_UpdateText.Font = new Font(label_UpdateText.Font, FontStyle.Bold); } else + { label_UpdateText.Cursor = DefaultCursor; + label_UpdateText.ForeColor = DefaultForeColor; + label_UpdateText.Font = new Font(label_UpdateText.Font, FontStyle.Regular); + } } }