Skip to content

Commit

Permalink
boost(Update): Improve the way SoundSwitch check for new updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed May 15, 2021
1 parent 0c427fc commit 8823e7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
16 changes: 8 additions & 8 deletions SoundSwitch/Framework/Updater/GitHubRelease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

namespace SoundSwitch.Framework.Updater
{
public class GitHubRelease
public record GitHubRelease
{
public string tag_name { get; set; }
public string body { get; set; }
public string name { get; set; }
public bool prerelease { get; set; }
public List<Asset> assets { get; set; }
public string tag_name { get; init; }
public string body { get; init; }
public string name { get; init; }
public bool prerelease { get; init; }
public List<Asset> assets { get; init; }

public class Asset
{
public string name { get; set; }
public string browser_download_url { get; set; }
public string name { get; init; }
public string browser_download_url { get;init; }
}

public override string ToString()
Expand Down
33 changes: 8 additions & 25 deletions SoundSwitch/Framework/Updater/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Sentry;
using Serilog;

namespace SoundSwitch.Framework.Updater
Expand All @@ -32,7 +31,6 @@ public class UpdateChecker
private static readonly Version AppVersion = new Version(Application.ProductVersion);

private readonly Uri _releaseUrl;
private readonly WebClient _webClient = new WebClient();
public EventHandler<NewReleaseEvent> UpdateAvailable;
public bool Beta { get; set; }

Expand All @@ -43,25 +41,9 @@ public UpdateChecker(Uri releaseUrl) : this(releaseUrl, false)
public UpdateChecker(Uri releaseUrl, bool checkBeta)
{
_releaseUrl = releaseUrl;
_webClient.DownloadStringCompleted += DownloadStringCompleted;
Beta = checkBeta;
}

private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Log.Error(e.Error, "Exception while getting release");
return;
}

var serverRelease = JsonConvert.DeserializeObject<List<GitHubRelease>>(e.Result);
if (!serverRelease.Any(ProcessRelease))
{
Log.Information("No new Version found: {Releases}", serverRelease);
}
}

private bool ProcessRelease(GitHubRelease serverRelease)
{

Expand All @@ -73,7 +55,6 @@ private bool ProcessRelease(GitHubRelease serverRelease)
}

var version = new Version(serverRelease.tag_name.Substring(1));
var changelog = Regex.Split(serverRelease.body, "\r\n|\r|\n");
try
{
if (version > AppVersion)
Expand All @@ -83,6 +64,7 @@ private bool ProcessRelease(GitHubRelease serverRelease)
{
return false;
}
var changelog = Regex.Split(serverRelease.body, "\r\n|\r|\n");
var release = new Release(version, installer, serverRelease.name);
release.Changelog.AddRange(changelog);
UpdateAvailable?.Invoke(this, new NewReleaseEvent(release));
Expand All @@ -102,13 +84,14 @@ private bool ProcessRelease(GitHubRelease serverRelease)
/// </summary>
public void CheckForUpdate()
{
_webClient.Headers.Add("User-Agent", UserAgent);
Task.Factory.StartNew(() => _webClient.DownloadStringAsync(_releaseUrl));
using var httpClient = new HttpClient(new SentryHttpMessageHandler());
var releases = httpClient.GetFromJsonAsync<GitHubRelease[]>(_releaseUrl).GetAwaiter().GetResult();
foreach (var _ in (releases ?? Array.Empty<GitHubRelease>()).SkipWhile(release => !ProcessRelease(release))) ;
}

public class NewReleaseEvent : EventArgs
{
public Release Release { get; private set; }
public Release Release { get;}

public NewReleaseEvent(Release release)
{
Expand Down

0 comments on commit 8823e7a

Please sign in to comment.