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..618167c --- /dev/null +++ b/DownKyi/Services/VersionCheckerService.cs @@ -0,0 +1,36 @@ +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 GhReleases = "https://api.github.com/repos/yaobiao131/downkyicore/releases/latest"; + public async Task<(Version?, string?)> GetLatestVersion() + { + Version? version = default; + string? updateNotes = default; + try + { + using var hc = new HttpClient(); + hc.DefaultRequestHeaders.Add("User-Agent", "downkyicore"); + 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/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/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..762eb09 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,42 @@ 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 is null) + { + EventAggregator.GetEvent().Publish("检查失败,请稍后重试~"); + _isCheckVersion = false; + return; + } + #if DEBUG + var versionString = AppVersion.Replace("-debug", string.Empty); + #else + var versionString = AppVersion; + #endif + var currVersion = Version.Parse(versionString); + if(currVersion < 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); + } + })!; + } + else + { + EventAggregator.GetEvent().Publish("已是最新版~"); + } + _isCheckVersion = false; } // 意见反馈事件 @@ -279,7 +314,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..45b6e1c --- /dev/null +++ b/DownKyi/Views/Dialogs/NewVersionAvailableDialog.axaml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + +