diff --git a/DownKyi/App.axaml.cs b/DownKyi/App.axaml.cs index 2051858..9e16bd9 100644 --- a/DownKyi/App.axaml.cs +++ b/DownKyi/App.axaml.cs @@ -122,6 +122,7 @@ public partial class App : PrismApplication containerRegistry.RegisterDialog(ViewDownloadSetterViewModel.Tag); containerRegistry.RegisterDialog(ViewParsingSelectorViewModel.Tag); containerRegistry.RegisterDialog(ViewAlreadyDownloadedDialogViewModel.Tag); + containerRegistry.RegisterDialog(NewVersionAvailableDialogViewModel.Tag); } diff --git a/DownKyi/DownKyi.csproj b/DownKyi/DownKyi.csproj index ee8c3a2..c70292f 100644 --- a/DownKyi/DownKyi.csproj +++ b/DownKyi/DownKyi.csproj @@ -33,4 +33,11 @@ + + + + + NewVersionAvailableDialog.axaml + + diff --git a/DownKyi/Languanges/Default.axaml b/DownKyi/Languanges/Default.axaml index 486c47c..8c26374 100644 --- a/DownKyi/Languanges/Default.axaml +++ b/DownKyi/Languanges/Default.axaml @@ -312,6 +312,8 @@ 警告 错误 确定 + 前往下载 + 更新提示 取消 此项需重启生效,您确定要重新启动吗? 您确定要删除吗? diff --git a/DownKyi/Services/VersionCheckerService.cs b/DownKyi/Services/VersionCheckerService.cs new file mode 100644 index 0000000..68237d2 --- /dev/null +++ b/DownKyi/Services/VersionCheckerService.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +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() + { + string json; + try + { + 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')); + return (version, updateNotes); + } + catch (Exception e) + { + return (null, null); + } + } + } +} diff --git a/DownKyi/Utils/MarkdownUtil.cs b/DownKyi/Utils/MarkdownUtil.cs new file mode 100644 index 0000000..e682738 --- /dev/null +++ b/DownKyi/Utils/MarkdownUtil.cs @@ -0,0 +1,54 @@ +using Avalonia.Controls.Documents; +using Avalonia.Media; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DownKyi.Utils +{ + public class MarkdownUtil + { + public static List ConvertMarkdownToRuns(string markdownText) + { + var runs = new List(); + var lines = markdownText.Split('\n'); + foreach (var line in lines) + { + if (line.Trim().StartsWith("")) + { + continue; + } + if (line.StartsWith("# ")) + { + runs.Add(new Run(line.Substring(2)) { FontSize = 18, FontWeight = FontWeight.Bold }); + } + else if (line.StartsWith("## ")) + { + runs.Add(new Run(line.Substring(3)) { FontSize = 19, FontWeight = FontWeight.Bold }); // 二级标题 + } + else if (line.StartsWith("### ")) + { + runs.Add(new Run(line.Substring(4)) { FontSize = 15, FontWeight = FontWeight.Bold }); // 三级标题 + } + else + { + var parts = line.Split(new[] { "**" }, StringSplitOptions.None); + for (int i = 0; i < parts.Length; i++) + { + if (i % 2 == 1) + { + runs.Add(new Run(parts[i]) { FontWeight = FontWeight.Bold }); + } + else + { + runs.Add(new Run(parts[i]) { FontSize = 13}); + } + } + } + } + return runs; + } + } +} diff --git a/DownKyi/Utils/PlatformHelper.cs b/DownKyi/Utils/PlatformHelper.cs index 823118e..99f9e26 100644 --- a/DownKyi/Utils/PlatformHelper.cs +++ b/DownKyi/Utils/PlatformHelper.cs @@ -40,17 +40,17 @@ public static class PlatformHelper { if (OperatingSystem.IsWindows()) { - Process.Start(filename); + Process.Start(new ProcessStartInfo(filename) { UseShellExecute = true }); } if (OperatingSystem.IsMacOS()) { - Process.Start("open", $"\"{filename}\""); + Process.Start(new ProcessStartInfo("open", $"\"{filename}\"")); } if (OperatingSystem.IsLinux()) { - Process.Start("xdg-open", $"\"{filename}\""); + Process.Start(new ProcessStartInfo("xdg-open", $"\"{filename}\"")); } } catch (Exception e) diff --git a/DownKyi/ViewModels/Dialogs/NewVersionAvailableDialogViewModel.cs b/DownKyi/ViewModels/Dialogs/NewVersionAvailableDialogViewModel.cs new file mode 100644 index 0000000..820b7f1 --- /dev/null +++ b/DownKyi/ViewModels/Dialogs/NewVersionAvailableDialogViewModel.cs @@ -0,0 +1,41 @@ +using Avalonia.Controls.Documents; +using DownKyi.Models; +using DownKyi.Utils; +using Prism.Commands; +using Prism.Services.Dialogs; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace DownKyi.ViewModels.Dialogs +{ + public class NewVersionAvailableDialogViewModel : BaseDialogViewModel + { + public const string Tag = "NewVersionAvailable"; + + private DelegateCommand? _allowCommand; + public DelegateCommand AllowCommand => _allowCommand ??= new DelegateCommand(ExecuteAllowCommand); + + /// + /// 确认事件 + /// + private void ExecuteAllowCommand() + { + ButtonResult result = ButtonResult.OK; + RaiseRequestClose(new DialogResult(result)); + } + + public ObservableCollection Messages { get; set; } = new(); + + + public override void OnDialogOpened(IDialogParameters parameters) + { + Messages.AddRange(MarkdownUtil.ConvertMarkdownToRuns(parameters.GetValue("body"))); + } + } +} diff --git a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs index 6a7180e..3db844b 100644 --- a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs +++ b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs @@ -1,11 +1,17 @@ using DownKyi.Core.Settings; using DownKyi.Events; +using DownKyi.Images; using DownKyi.Models; using DownKyi.PrismExtension.Dialog; +using DownKyi.Services; using DownKyi.Utils; +using DownKyi.ViewModels.Dialogs; using Prism.Commands; using Prism.Events; using Prism.Regions; +using Prism.Services.Dialogs; +using System; + namespace DownKyi.ViewModels.Settings; @@ -51,7 +57,7 @@ public class ViewAboutViewModel : ViewModelBase #endregion - public ViewAboutViewModel(IEventAggregator eventAggregator, IDialogService dialogService) : base(eventAggregator, + public ViewAboutViewModel(IEventAggregator eventAggregator, PrismExtension.Dialog.IDialogService dialogService) : base(eventAggregator, dialogService) { #region 属性初始化 @@ -104,13 +110,35 @@ public class ViewAboutViewModel : ViewModelBase public DelegateCommand CheckUpdateCommand => _checkUpdateCommand ??= new DelegateCommand(ExecuteCheckUpdateCommand); + private bool IsCheckVersion = false; /// /// 检查更新事件 /// - private void ExecuteCheckUpdateCommand() + private async void ExecuteCheckUpdateCommand() { - //eventAggregator.GetEvent().Publish("开始查找更新,请稍后~"); - EventAggregator.GetEvent().Publish("请前往主页下载最新版~"); + if (IsCheckVersion) return; + IsCheckVersion = true; + (Version version, string body) =await new VersionCheckerService().GetLatestVersion(); + if(version == null) + { + EventAggregator.GetEvent().Publish("检查失败,请稍后重试~"); + return; + } + #if DEBUG + var versionString = AppVersion.Replace("-debug", string.Empty); + #endif + var curr_version = Version.Parse(versionString); + if(curr_version < version) + { + await DialogService?.ShowDialogAsync(NewVersionAvailableDialogViewModel.Tag, new Prism.Services.Dialogs.DialogParameters { { "body", body } }, result => + { + if(result.Result == ButtonResult.OK) + { + PlatformHelper.Open("https://github.com/yaobiao131/downkyicore/releases/latest", EventAggregator); + } + })!; + } + IsCheckVersion = false; } // 意见反馈事件 @@ -279,7 +307,7 @@ public class ViewAboutViewModel : ViewModelBase PlatformHelper.Open("FFmpeg_LICENSE.txt"); } - #endregion +#endregion /// /// 发送需要显示的tip diff --git a/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml b/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml new file mode 100644 index 0000000..983760e --- /dev/null +++ b/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + +