12
12
using System . ComponentModel ;
13
13
using System . Net . Http ;
14
14
using System . Text ;
15
+ using SemanticCode . Services ;
16
+ using SemanticCode . Views ;
15
17
16
18
namespace SemanticCode . ViewModels ;
17
19
@@ -28,6 +30,9 @@ public class HomeViewModel : ViewModelBase
28
30
private IBrush _nodeStatusColor = Brushes . Orange ;
29
31
private IBrush _gitStatusColor = Brushes . Orange ;
30
32
private IBrush _envVarStatusColor = Brushes . Orange ;
33
+ private readonly UpdateService _updateService ;
34
+ private readonly UpdateConfigService _updateConfigService ;
35
+ private bool _hasCheckedForUpdatesThisSession = false ;
31
36
32
37
public string Title { get ; } = "Semantic Code" ;
33
38
public string WelcomeMessage { get ; } = "欢迎使用 Semantic Code 一款Claude Code工具。" ;
@@ -118,6 +123,9 @@ public IBrush EnvVarStatusColor
118
123
119
124
public HomeViewModel ( )
120
125
{
126
+ _updateService = new UpdateService ( ) ;
127
+ _updateConfigService = new UpdateConfigService ( ) ;
128
+
121
129
NavigateCommand = ReactiveCommand . Create < string > ( Navigate ) ;
122
130
OpenProjectCommand = ReactiveCommand . Create < string > ( OpenProject ) ;
123
131
RefreshStatusCommand = ReactiveCommand . CreateFromTask ( RefreshStatusAsync ) ;
@@ -127,8 +135,12 @@ public HomeViewModel()
127
135
InitializeQuickActions ( ) ;
128
136
InitializeRecentProjects ( ) ;
129
137
130
- // 启动时检查状态
131
- Task . Run ( async ( ) => await RefreshStatusAsync ( ) ) ;
138
+ // 启动时检查状态和版本更新(只检查一次)
139
+ Task . Run ( async ( ) =>
140
+ {
141
+ await RefreshStatusAsync ( ) ;
142
+ await CheckForUpdatesAsync ( ) ;
143
+ } ) ;
132
144
}
133
145
134
146
private void InitializeFeatures ( )
@@ -700,6 +712,62 @@ private async Task<CommandResult> RunCommandAsync(string command, string argumen
700
712
}
701
713
} ) ;
702
714
}
715
+
716
+ private async Task CheckForUpdatesAsync ( )
717
+ {
718
+ // 确保只在首次加载时检查更新,避免重复检查
719
+ if ( _hasCheckedForUpdatesThisSession )
720
+ return ;
721
+
722
+ _hasCheckedForUpdatesThisSession = true ;
723
+
724
+ try
725
+ {
726
+ var updateInfo = await _updateService . CheckForUpdatesAsync ( ) ;
727
+
728
+ if ( updateInfo != null && updateInfo . IsNewerVersion )
729
+ {
730
+ // 检查是否被用户忽略
731
+ if ( ! _updateConfigService . IsVersionIgnored ( updateInfo . Version ) )
732
+ {
733
+ // 在UI线程中显示更新对话框
734
+ await Avalonia . Threading . Dispatcher . UIThread . InvokeAsync ( async ( ) =>
735
+ {
736
+ await ShowUpdateNotification ( updateInfo ) ;
737
+ } ) ;
738
+ }
739
+ }
740
+ }
741
+ catch ( Exception )
742
+ {
743
+ // 静默处理更新检查失败
744
+ }
745
+ }
746
+
747
+ private async Task ShowUpdateNotification ( Models . UpdateInfo updateInfo )
748
+ {
749
+ var viewModel = new UpdateNotificationViewModel ( _updateService , updateInfo ) ;
750
+ var dialog = new UpdateNotificationDialog ( viewModel ) ;
751
+
752
+ // 设置对话框事件处理
753
+ viewModel . RemindLaterRequested += ( s , e ) => dialog . Close ( ) ;
754
+ viewModel . IgnoreVersionRequested += ( s , e ) =>
755
+ {
756
+ _updateConfigService . IgnoreVersion ( updateInfo . Version ) ;
757
+ dialog . Close ( ) ;
758
+ } ;
759
+ viewModel . UpdateCompleted += ( s , e ) => dialog . Close ( ) ;
760
+
761
+ // 显示对话框
762
+ var mainWindow = Avalonia . Application . Current ? . ApplicationLifetime is Avalonia . Controls . ApplicationLifetimes . IClassicDesktopStyleApplicationLifetime desktop
763
+ ? desktop . MainWindow
764
+ : null ;
765
+
766
+ if ( mainWindow != null )
767
+ {
768
+ await dialog . ShowDialog ( mainWindow ) ;
769
+ }
770
+ }
703
771
}
704
772
705
773
public class CommandResult
0 commit comments