refactor: 版本检测修改

This commit is contained in:
Nlick47
2024-10-11 09:59:32 +08:00
parent 40532aa482
commit 52f7b73ecf
3 changed files with 72 additions and 70 deletions

View File

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

View File

@@ -110,25 +110,28 @@ public class ViewAboutViewModel : ViewModelBase
public DelegateCommand CheckUpdateCommand => _checkUpdateCommand ??= new DelegateCommand(ExecuteCheckUpdateCommand);
private bool IsCheckVersion = false;
private bool _isCheckVersion = false;
/// <summary>
/// 检查更新事件
/// </summary>
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<MessageEvent>().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<MessageEvent>().Publish("已是最新版~");
}
IsCheckVersion = false;
_isCheckVersion = false;
}
// 意见反馈事件

View File

@@ -8,54 +8,60 @@
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
<Border BorderBrush="{DynamicResource BrushWindowBorder}" BorderThickness="1,1,0.6,0.6">
<Grid RowDefinitions="40,*">
<Grid
Grid.Row="0"
Background="{DynamicResource BrushBackgroundGreyTranslucent3}"
KeyboardNavigation.TabNavigation="None" ColumnDefinitions="*,100,*,50">
<TextBlock
Text="{DynamicResource NewVersionTitle}"
FontSize="15"
TextAlignment="Center"
VerticalAlignment="Center"
Grid.Column="1"
HorizontalAlignment="Right"
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"/>
<Button
Grid.Column="4"
Command="{Binding CloseCommand}"
Theme="{StaticResource ImageBtnStyle}">
<Path
Width="{Binding CloseIcon.Width}"
Height="{Binding CloseIcon.Height}"
Data="{Binding CloseIcon.Data}"
Fill="{Binding CloseIcon.Fill}"
Stretch="UniformToFill" />
</Button>
</Grid>
<Grid Grid.Row="1" RowDefinitions="4*,*,15">
<ScrollViewer
Grid.Row="0"
VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Messages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Margin="15,5,0,0" TextWrapping="Wrap">
<TextBlock.Inlines>
<Run Text="{Binding Text}"
FontWeight="{Binding FontWeight}"
FontSize="{Binding FontSize}" />
</TextBlock.Inlines>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Button
Grid.Row="1"
Width="90"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Command="{Binding AllowCommand}"
Content="{DynamicResource ActionUpdate}"
FontSize="12"
Theme="{StaticResource BtnStyle}" />
</Grid>
</Grid>
</Border>