1+ using Avalonia ;
2+ using Avalonia . Controls ;
3+ using Avalonia . Controls . ApplicationLifetimes ;
4+ using Avalonia . Controls . Notifications ;
15using Client . Avalonia . Models ;
26using Client . Avalonia . Services ;
37using CommunityToolkit . Mvvm . ComponentModel ;
@@ -8,6 +12,7 @@ namespace Client.Avalonia.ViewModels;
812public 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}
0 commit comments