Skip to content

Commit 5855bd1

Browse files
committed
feat: add Notification.
1 parent e78b270 commit 5855bd1

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/Client.Avalonia/ViewModels/MainViewViewModel.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Controls.ApplicationLifetimes;
4+
using Avalonia.Controls.Notifications;
15
using Client.Avalonia.Models;
26
using Client.Avalonia.Services;
37
using CommunityToolkit.Mvvm.ComponentModel;
@@ -8,6 +12,7 @@ namespace Client.Avalonia.ViewModels;
812
public partial class MainViewViewModel : ViewModelBase
913
{
1014
private readonly IDownloadService _downloadService;
15+
private readonly WindowNotificationManager _notificationManager;
1116

1217
[ObservableProperty] private DownloadStatistics _statistics;
1318

@@ -17,8 +22,11 @@ [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(StartCommand), nameof(St
1722
public MainViewViewModel(IDownloadService downloadService)
1823
{
1924
_downloadService = downloadService;
25+
_notificationManager = new WindowNotificationManager(ResolveDefaultTopLevel());
26+
2027
_downloadService.ProgressChanged += stats => Statistics = stats;
2128
_downloadService.StatusChanged += status => Status = status;
29+
_downloadService.StatusChanged += OnDownloadCompleted;
2230

2331
Statistics = _downloadService.CurrentStatistics;
2432
Status = _downloadService.Status;
@@ -40,4 +48,23 @@ public MainViewViewModel(IDownloadService downloadService)
4048
private void Restart() => _downloadService.Restart();
4149

4250
#endregion
51+
52+
private void OnDownloadCompleted(DownloadStatus status)
53+
{
54+
if (status is not DownloadStatus.Completed) return;
55+
_notificationManager.Show(new Notification(
56+
"更新完成",
57+
$"已更新到最新版本,版本号:{Statistics.Version}",
58+
NotificationType.Success));
59+
}
60+
61+
private static TopLevel? ResolveDefaultTopLevel()
62+
{
63+
return Application.Current?.ApplicationLifetime switch
64+
{
65+
IClassicDesktopStyleApplicationLifetime desktop => desktop.MainWindow,
66+
ISingleViewApplicationLifetime singleView => TopLevel.GetTopLevel(singleView.MainView),
67+
_ => null
68+
};
69+
}
4370
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia.Controls;
2+
using Avalonia.Interactivity;
23
using Client.Avalonia.ViewModels;
34
using Microsoft.Extensions.DependencyInjection;
45

@@ -9,10 +10,11 @@ public partial class MainWindow : Window
910
public MainWindow()
1011
{
1112
InitializeComponent();
12-
var provider = App.ServiceProvider;
13-
if (provider is not null)
14-
{
15-
DataContext = provider.GetRequiredService<MainViewViewModel>();
16-
}
13+
}
14+
15+
protected override void OnLoaded(RoutedEventArgs e)
16+
{
17+
base.OnLoaded(e);
18+
DataContext = App.ServiceProvider?.GetRequiredService<MainViewViewModel>();
1719
}
1820
}

0 commit comments

Comments
 (0)