Add check update

This commit is contained in:
Nguyen Duy
2024-04-30 15:21:28 +07:00
parent 7810e61d7f
commit a44e7d26df
5 changed files with 115 additions and 1 deletions
+1
View File
@@ -13,6 +13,7 @@ namespace ParsecVDisplay
const string ID = "QpHOX8IBUHBznGtqk9xm1";
public const string NAME = "ParsecVDisplay";
public const string VERSION = "0.45.2";
public const string GITHUB_REPO = "nomi-san/parsec-vdd";
public static bool Silent { get; private set; }
+2
View File
@@ -99,6 +99,8 @@
<MenuItem Header="Fallback display" IsCheckable="True" IsChecked="{Binding Path=(local:Config.FallbackDisplay), Mode=TwoWay}" />
<MenuItem Header="Keep screen on" IsCheckable="True" IsChecked="{Binding Path=(local:Config.KeepScreenOn), Mode=TwoWay}" />
<Separator />
<MenuItem Header="Check for update" x:Name="xMICheckUpdate" Click="CheckUpdate" />
<Separator />
<MenuItem Header="Exit" Click="ExitApp" />
</ContextMenu>
</Window.ContextMenu>
+31 -1
View File
@@ -59,6 +59,8 @@ namespace ParsecVDisplay
{
Loaded -= Window_Loaded;
CheckUpdate(null, null);
if (App.Silent)
Hide();
@@ -167,7 +169,35 @@ namespace ParsecVDisplay
private void OpenRepoLink(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Helper.OpenLink("https://github.com/nomi-san/parsec-vdd");
Helper.OpenLink($"https://github.com/{App.GITHUB_REPO}");
}
private async void CheckUpdate(object sender, RoutedEventArgs e)
{
var oldText = xMICheckUpdate.Header;
xMICheckUpdate.Header = "Checking for update...";
xMICheckUpdate.IsEnabled = false;
var newVersion = await Updater.CheckUpdate();
if (!string.IsNullOrEmpty(newVersion))
{
var ret = MessageBox.Show(this,
$"A new version — v{newVersion} is available!\n" +
$"To keep your experience optimal, please update it now.",
App.NAME, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (ret == MessageBoxResult.Yes)
{
Helper.OpenLink(Updater.DOWNLOAD_URL);
}
}
else if (sender != null)
{
MessageBox.Show(this, "Your app version is up-to-date.",
App.NAME, MessageBoxButton.OK, MessageBoxImage.Information);
}
xMICheckUpdate.Header = oldText;
xMICheckUpdate.IsEnabled = true;
}
}
}
+1
View File
@@ -86,6 +86,7 @@
<Compile Include="Shadow.cs" />
<Compile Include="Tray.cs" />
<Compile Include="ParsecVDD.cs" />
<Compile Include="Updater.cs" />
<Page Include="Components\Button.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
+80
View File
@@ -0,0 +1,80 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ParsecVDisplay
{
internal static class Updater
{
public static string DOWNLOAD_URL => $"https://github.com/{App.GITHUB_REPO}/releases/latest";
static string GH_API_URL => $"https://api.github.com/repos/{App.GITHUB_REPO}/releases/latest";
static Updater()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
}
public static Task<string> CheckUpdate()
{
return Task.Run(async () =>
{
var localVersion = new Version(App.VERSION);
var remoteVersion = await FetchLatestVersion();
if (remoteVersion.CompareTo(localVersion) > 0)
{
return remoteVersion.ToString();
}
return string.Empty;
});
}
static async Task<Version> FetchLatestVersion()
{
try
{
var json = await DownloadString(GH_API_URL);
if (!string.IsNullOrEmpty(json))
{
var tagNameRegex = new Regex("\"tag_name\":\\s+\"(.*)\"");
var match = tagNameRegex.Match(json);
if (match.Success && match.Groups.Count > 1)
{
var vtag = match.Groups[1].Value.ToLower();
if (vtag.StartsWith("v"))
vtag = vtag.Substring(1);
return new Version(vtag);
}
}
}
catch
{
}
return new Version(App.VERSION);
}
static async Task<string> DownloadString(string url)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true
};
return await client.GetStringAsync(url);
}
}
}
}