Skip to content

Commit

Permalink
Fix: updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Jun 20, 2019
1 parent 195cd68 commit 40f906b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 60 deletions.
9 changes: 3 additions & 6 deletions App/Initalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IMainFormView>();
var mainPresenter = new MainFormPresenter(mainForm, new MainFormModel(CollectionEditor, UserDialogs), infoTextModel, WebCollectionProvider);
Expand Down
14 changes: 8 additions & 6 deletions App/Interfaces/IUpdateModel.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 10 additions & 6 deletions App/Presenters/Controls/InfoTextPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
}


Expand All @@ -53,21 +53,25 @@ 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)
{
_view.UpdateText = UpdateError;
}
else
{
_view.UpdateText = string.Format(UpdateNewestVersion, updater.currentVersion);
_view.ColorUpdateText = false;
_view.UpdateText = string.Format(NoUpdatesAvailable, updater.CurrentVersion);
}
}
else
{
_view.ColorUpdateText = false;
_view.UpdateText = "";
}
}
Expand Down
6 changes: 3 additions & 3 deletions App/SidePanelActionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
71 changes: 34 additions & 37 deletions App/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -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("<html>") || contents.Contains("<head>") || 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);
}

}
}
}
2 changes: 1 addition & 1 deletion Common/Interfaces/Controls/IInfoTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
8 changes: 7 additions & 1 deletion GuiComponents/Controls/InfoTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 40f906b

Please sign in to comment.