Skip to content

Commit 305824a

Browse files
committed
功能优化:重构更新弹窗交互逻辑,支持显示日志与手动下载;修复并发检测问题
1 parent 40deaee commit 305824a

5 files changed

Lines changed: 68 additions & 5 deletions

File tree

Dialogs/ViewModels/AboutViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public AboutViewModel() : this(new UpdateService())
2424
[RelayCommand]
2525
private async Task OnCheckUpdateAsync()
2626
{
27+
if (UpdateViewModel.IsUpdateDialogOpen) return;
28+
2729
IsCheckingUpdate = true;
2830
try
2931
{

Dialogs/ViewModels/UpdateViewModel.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace ProxyChecker.Dialogs.ViewModels;
1414

1515
public partial class UpdateViewModel : ObservableObject, IDialogContext
1616
{
17+
public static bool IsUpdateDialogOpen { get; set; }
18+
1719
private readonly UpdateService _updateService;
1820
private UpdateInfo? _updateInfo;
1921
private CancellationTokenSource? _cts;
@@ -24,6 +26,8 @@ public partial class UpdateViewModel : ObservableObject, IDialogContext
2426
[NotifyCanExecuteChangedFor(nameof(StartCommand), nameof(StopCommand), nameof(RestartCommand))]
2527
private DownloadStatus _status = DownloadStatus.NotStarted;
2628

29+
[ObservableProperty] private string? _releaseNotes;
30+
2731
// Design-time constructor
2832
public UpdateViewModel()
2933
{
@@ -39,8 +43,7 @@ public UpdateViewModel(UpdateService updateService, UpdateInfo? updateInfo = nul
3943
if (_updateInfo != null)
4044
{
4145
Statistics.Version = _updateInfo.TargetFullRelease.Version.ToString();
42-
// 直接开始下载,UpdateService 已在 AboutViewModel 中初始化
43-
_ = StartAsync();
46+
ReleaseNotes = _updateInfo.TargetFullRelease.NotesMarkdown;
4447
}
4548
else
4649
{
@@ -58,8 +61,7 @@ private async Task CheckUpdatesAsync()
5861
if (_updateInfo != null)
5962
{
6063
Statistics.Version = _updateInfo.TargetFullRelease.Version.ToString();
61-
// Auto start download if update is found, as we are in the UpdateDialog
62-
await StartAsync();
64+
ReleaseNotes = _updateInfo.TargetFullRelease.NotesMarkdown;
6365
}
6466
else
6567
{

Dialogs/Views/UpdateDialog.axaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,31 @@
6868
<Run Text="版本更新" />
6969
</SelectableTextBlock>
7070

71+
<!-- Release Notes -->
72+
<Border Height="150"
73+
Width="400"
74+
Background="{DynamicResource SemiColorFill0}"
75+
CornerRadius="6"
76+
Padding="10"
77+
IsVisible="{Binding Status,
78+
Converter={StaticResource EnumToBooleanConverter},
79+
ConverterParameter={x:Static models:DownloadStatus.NotStarted},
80+
Mode=OneWay}">
81+
<ScrollViewer>
82+
<SelectableTextBlock Text="{Binding ReleaseNotes}" TextWrapping="Wrap" />
83+
</ScrollViewer>
84+
</Border>
85+
7186
<!-- Status Text -->
7287
<Panel>
88+
<SelectableTextBlock
89+
HorizontalAlignment="Center"
90+
IsVisible="{Binding Status,
91+
Converter={StaticResource EnumToBooleanConverter},
92+
ConverterParameter={x:Static models:DownloadStatus.NotStarted},
93+
Mode=OneWay}"
94+
Text="发现新版本,是否立即更新?" />
95+
7396
<SelectableTextBlock
7497
HorizontalAlignment="Center"
7598
IsVisible="{Binding Status,
@@ -98,6 +121,26 @@
98121
<!-- Controls -->
99122
<StackPanel Orientation="Horizontal" Spacing="10" HorizontalAlignment="Center">
100123

124+
<!-- Start Update -->
125+
<Button
126+
Content="立即更新"
127+
Classes="Primary"
128+
Command="{Binding StartCommand}"
129+
IsVisible="{Binding Status,
130+
Converter={StaticResource EnumToBooleanConverter},
131+
ConverterParameter={x:Static models:DownloadStatus.NotStarted},
132+
Mode=OneWay}" />
133+
134+
<!-- Cancel Update -->
135+
<Button
136+
Content="暂不更新"
137+
Classes="Secondary"
138+
Command="{Binding CloseCommand}"
139+
IsVisible="{Binding Status,
140+
Converter={StaticResource EnumToBooleanConverter},
141+
ConverterParameter={x:Static models:DownloadStatus.NotStarted},
142+
Mode=OneWay}" />
143+
101144
<!-- Stop / Cancel -->
102145
<u:IconButton
103146
Name="StopButton"

Dialogs/Views/UpdateDialog.axaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Avalonia.Controls;
2+
using Avalonia.Interactivity;
23
using Avalonia.Markup.Xaml;
4+
using ProxyChecker.Dialogs.ViewModels;
35

46
namespace ProxyChecker.Dialogs.Views;
57

@@ -8,6 +10,8 @@ public partial class UpdateDialog : UserControl
810
public UpdateDialog()
911
{
1012
InitializeComponent();
13+
Loaded += (s, e) => UpdateViewModel.IsUpdateDialogOpen = true;
14+
Unloaded += (s, e) => UpdateViewModel.IsUpdateDialogOpen = false;
1115
}
1216

1317
private void InitializeComponent()

Services/UpdateService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class UpdateService
1111
private const string RepoUrl = "https://github.com/interface95/ProxyChecker";
1212
private UpdateManager? _updateManager;
1313
private UpdateInfo? _updateInfo;
14+
private Task<UpdateInfo?>? _checkUpdateTask;
1415

1516
public bool IsSupported => true;
1617

@@ -21,7 +22,18 @@ public async Task InitializeAsync()
2122
_updateManager = new UpdateManager(source);
2223
}
2324

24-
public async Task<UpdateInfo?> CheckForUpdatesAsync()
25+
public Task<UpdateInfo?> CheckForUpdatesAsync()
26+
{
27+
if (_checkUpdateTask != null && !_checkUpdateTask.IsCompleted)
28+
{
29+
return _checkUpdateTask;
30+
}
31+
32+
_checkUpdateTask = CheckForUpdatesInternalAsync();
33+
return _checkUpdateTask;
34+
}
35+
36+
private async Task<UpdateInfo?> CheckForUpdatesInternalAsync()
2537
{
2638
if (_updateManager == null) await InitializeAsync();
2739

0 commit comments

Comments
 (0)