feat: 加入版本检测

This commit is contained in:
Nlick47
2024-10-10 20:23:02 +08:00
parent 263ac26455
commit 3b03c8a686
10 changed files with 259 additions and 8 deletions

View File

@@ -122,6 +122,7 @@ public partial class App : PrismApplication
containerRegistry.RegisterDialog<ViewDownloadSetter>(ViewDownloadSetterViewModel.Tag);
containerRegistry.RegisterDialog<ViewParsingSelector>(ViewParsingSelectorViewModel.Tag);
containerRegistry.RegisterDialog<ViewAlreadyDownloadedDialog>(ViewAlreadyDownloadedDialogViewModel.Tag);
containerRegistry.RegisterDialog<NewVersionAvailableDialog>(NewVersionAvailableDialogViewModel.Tag);
}

View File

@@ -33,4 +33,11 @@
<ItemGroup>
<ProjectReference Include="..\DownKyi.Core\DownKyi.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Views\Dialogs\NewVersionAvailableDialog.axaml.cs">
<DependentUpon>NewVersionAvailableDialog.axaml</DependentUpon>
</Compile>
</ItemGroup>
</Project>

View File

@@ -312,6 +312,8 @@
<system:String x:Key="Warning">警告</system:String>
<system:String x:Key="Error">错误</system:String>
<system:String x:Key="Allow">确定</system:String>
<system:String x:Key="ActionUpdate">前往下载</system:String>
<system:String x:Key="NewVersionTitle">更新提示</system:String>
<system:String x:Key="Cancel">取消</system:String>
<system:String x:Key="ConfirmReboot">此项需重启生效,您确定要重新启动吗?</system:String>
<system:String x:Key="ConfirmDelete">您确定要删除吗?</system:String>

View File

@@ -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);
}
}
}
}

View File

@@ -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<Run> ConvertMarkdownToRuns(string markdownText)
{
var runs = new List<Run>();
var lines = markdownText.Split('\n');
foreach (var line in lines)
{
if (line.Trim().StartsWith("<!--") && line.Trim().EndsWith("-->"))
{
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;
}
}
}

View File

@@ -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)

View File

@@ -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);
/// <summary>
/// 确认事件
/// </summary>
private void ExecuteAllowCommand()
{
ButtonResult result = ButtonResult.OK;
RaiseRequestClose(new DialogResult(result));
}
public ObservableCollection<Run> Messages { get; set; } = new();
public override void OnDialogOpened(IDialogParameters parameters)
{
Messages.AddRange(MarkdownUtil.ConvertMarkdownToRuns(parameters.GetValue<string>("body")));
}
}
}

View File

@@ -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;
/// <summary>
/// 检查更新事件
/// </summary>
private void ExecuteCheckUpdateCommand()
private async void ExecuteCheckUpdateCommand()
{
//eventAggregator.GetEvent<MessageEvent>().Publish("开始查找更新,请稍后~");
EventAggregator.GetEvent<MessageEvent>().Publish("请前往主页下载最新版~");
if (IsCheckVersion) return;
IsCheckVersion = true;
(Version version, string body) =await new VersionCheckerService().GetLatestVersion();
if(version == null)
{
EventAggregator.GetEvent<MessageEvent>().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
/// <summary>
/// 发送需要显示的tip

View File

@@ -0,0 +1,62 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400"
Height="360"
xmlns:prism="http://prismlibrary.com/"
xmlns:prismExtension="clr-namespace:DownKyi.PrismExtension.Dialog"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:vmd="clr-namespace:DownKyi.ViewModels.Dialogs"
x:DataType="vmd:NewVersionAvailableDialogViewModel"
x:Class="DownKyi.Views.Dialogs.NewVersionAvailableDialog">
<Border BorderBrush="{DynamicResource BrushWindowBorder}"
CornerRadius="10"
BorderThickness="1,1,0.6,0.6">
<Grid Margin="10" RowDefinitions="2*,6*,3*">
<Grid ColumnDefinitions="2*,2*,*" VerticalAlignment="Top" Grid.Row="0">
<Button
Grid.Column="2"
Command="{Binding CloseCommand}"
ToolTip.Tip="{DynamicResource Close}"
Theme="{StaticResource CloseBtnStyle}">
<Path
Width="{Binding CloseIcon.Width}"
Height="{Binding CloseIcon.Height}"
Data="{Binding CloseIcon.Data}"
Fill="{DynamicResource ColorSystemBtnTintDark}"
Stretch="UniformToFill" />
</Button>
</Grid>
<TextBlock
Text="{DynamicResource NewVersionTitle}"
FontSize="15"
TextAlignment="Center"
Grid.Row="0"/>
<ScrollViewer
Grid.Row="1"
VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Messages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Inlines>
<Run Text="{Binding Text}" FontWeight="{Binding FontWeight}" FontSize="{Binding FontSize}" />
</TextBlock.Inlines>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Grid Grid.Row="2" ColumnDefinitions="2*,2*,2*" RowDefinitions="2.5*,*,2*">
<Button Grid.Column="1" CornerRadius="10"
Foreground="White"
Grid.Row="2"
Command="{Binding AllowCommand}"
BorderBrush="Transparent" BorderThickness="0"
Content="{DynamicResource ActionUpdate}" Background="#FF6699"/>
</Grid>
</Grid>
</Border>
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DownKyi.Views.Dialogs;
public partial class NewVersionAvailableDialog : UserControl
{
public NewVersionAvailableDialog()
{
InitializeComponent();
}
}