diff --git a/DownKyi/Services/VersionCheckerService.cs b/DownKyi/Services/VersionCheckerService.cs index 68237d2..618167c 100644 --- a/DownKyi/Services/VersionCheckerService.cs +++ b/DownKyi/Services/VersionCheckerService.cs @@ -10,34 +10,27 @@ namespace DownKyi.Services { public class VersionCheckerService { - private const string gh_releases = "https://api.github.com/repos/yaobiao131/downkyicore/releases/latest"; - public async Task<(Version, string)> GetLatestVersion() + private const string GhReleases = "https://api.github.com/repos/yaobiao131/downkyicore/releases/latest"; + public async Task<(Version?, string?)> GetLatestVersion() { - string json; + Version? version = default; + string? updateNotes = default; try { - var hc = new HttpClient(); + using var hc = new HttpClient(); hc.DefaultRequestHeaders.Add("User-Agent", "downkyicore"); - - json =await hc.GetStringAsync(new Uri(gh_releases)); - - } - catch (Exception e) when (e is HttpRequestException or TimeoutException) - { - return (null, null); - } - try - { - using JsonDocument doc = JsonDocument.Parse(json); - var versionString = doc.RootElement.GetProperty("tag_name").GetString(); - var updateNotes = doc.RootElement.GetProperty("body").GetString(); - var version = Version.Parse(versionString.TrimStart('v')); + var json =await hc.GetStringAsync(new Uri(GhReleases)); + using var doc = JsonDocument.Parse(json); + var versionString = doc.RootElement.GetProperty("tag_name").GetString()!; + updateNotes = doc.RootElement.GetProperty("body").GetString()!; + version = Version.Parse(versionString.TrimStart('v')); return (version, updateNotes); } catch (Exception e) { return (null, null); } + } } } diff --git a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs index 476a3e1..762eb09 100644 --- a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs +++ b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs @@ -110,25 +110,28 @@ public class ViewAboutViewModel : ViewModelBase public DelegateCommand CheckUpdateCommand => _checkUpdateCommand ??= new DelegateCommand(ExecuteCheckUpdateCommand); - private bool IsCheckVersion = false; + private bool _isCheckVersion = false; /// /// 检查更新事件 /// private async void ExecuteCheckUpdateCommand() { - if (IsCheckVersion) return; - IsCheckVersion = true; - (Version version, string body) =await new VersionCheckerService().GetLatestVersion(); - if(version == null) + if (_isCheckVersion) return; + _isCheckVersion = true; + (Version? version, string? body) = await new VersionCheckerService().GetLatestVersion(); + if(version is null) { EventAggregator.GetEvent().Publish("检查失败,请稍后重试~"); + _isCheckVersion = false; return; } #if DEBUG var versionString = AppVersion.Replace("-debug", string.Empty); + #else + var versionString = AppVersion; #endif - var curr_version = Version.Parse(versionString); - if(curr_version < version) + var currVersion = Version.Parse(versionString); + if(currVersion < version) { await DialogService?.ShowDialogAsync(NewVersionAvailableDialogViewModel.Tag, new Prism.Services.Dialogs.DialogParameters { { "body", body } }, result => { @@ -142,7 +145,7 @@ public class ViewAboutViewModel : ViewModelBase { EventAggregator.GetEvent().Publish("已是最新版~"); } - IsCheckVersion = false; + _isCheckVersion = false; } // 意见反馈事件 diff --git a/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml b/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml index 983760e..45b6e1c 100644 --- a/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml +++ b/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml @@ -8,54 +8,60 @@ xmlns:vmd="clr-namespace:DownKyi.ViewModels.Dialogs" x:DataType="vmd:NewVersionAvailableDialogViewModel" x:Class="DownKyi.Views.Dialogs.NewVersionAvailableDialog"> - - - - - - - + + + - - - - - - - - - - - - - - - + - + + + + + + + + + + + + + + +