1- using CommunityToolkit . Mvvm . DependencyInjection ;
2- using CommunityToolkit . Mvvm . Input ;
3- using CommunityToolkit . Mvvm . Messaging ;
4- using Syroot . Windows . IO ;
5- using System ;
6- using System . Threading . Tasks ;
7- using System . Windows . Controls ;
1+ using System . Windows . Controls ;
82using System . Windows . Navigation ;
9- using ZuneModdingHelper . Messages ;
103using ZuneModdingHelper . Services ;
114
125namespace ZuneModdingHelper . Pages
@@ -16,19 +9,13 @@ namespace ZuneModdingHelper.Pages
169 /// </summary>
1710 public partial class AboutPage : UserControl
1811 {
19- const string UPDATES_DIALOG_TITLE = "UPDATES" ;
20-
21- private readonly IUpdateService ? _updateService = Ioc . Default . GetService < IUpdateService > ( ) ;
22- private UpdateAvailableInfo _updateInfo ;
23- private string _downloadedUpdatePath ;
24-
2512 public AboutPage ( )
2613 {
2714 DataContext = this ;
2815 InitializeComponent ( ) ;
2916 }
3017
31- public bool EnableCheckForUpdates => _updateService is not null ;
18+ public bool EnableCheckForUpdates => UpdateHelper . Instance . Enabled ;
3219
3320 private void Link_RequestNavigate ( object sender , RequestNavigateEventArgs e )
3421 {
@@ -38,110 +25,7 @@ private void Link_RequestNavigate(object sender, RequestNavigateEventArgs e)
3825
3926 private async void UpdateCheckButton_Click ( object sender , System . Windows . RoutedEventArgs e )
4027 {
41- if ( _updateService is null )
42- return ;
43-
44- WeakReferenceMessenger . Default . Send ( new ShowDialogMessage ( new ProgressDialogViewModel
45- {
46- Title = UPDATES_DIALOG_TITLE ,
47- Description = "Checking for updates, please wait..." ,
48- IsIndeterminate = true ,
49- ShowAffirmativeButton = false ,
50- } ) ) ;
51-
52- try
53- {
54- _updateInfo = await _updateService . FetchAvailableUpdate ( ) ;
55- if ( _updateInfo is null )
56- {
57- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
58- WeakReferenceMessenger . Default . Send ( new ShowDialogMessage ( new DialogViewModel
59- {
60- Title = UPDATES_DIALOG_TITLE ,
61- Description = "No updates available.\r \n \r \n You're already using the latest version." ,
62- ShowAffirmativeButton = true ,
63- } ) ) ;
64- return ;
65- }
66-
67- // Newer version available, prompt user to download
68-
69- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
70- WeakReferenceMessenger . Default . Send ( new ShowDialogMessage ( new DialogViewModel
71- {
72- Title = UPDATES_DIALOG_TITLE ,
73- Description = $ "Release { _updateInfo . Name } is available. Would you like to download it now?",
74- AffirmativeText = "YES" ,
75- NegativeText = "NO" ,
76- ShowAffirmativeButton = true ,
77- ShowNegativeButton = true ,
78- OnAction = new AsyncRelayCommand < bool > ( OnUpdateDialogResult )
79- } ) ) ;
80- }
81- catch
82- {
83- App . OpenInBrowser ( "https://github.com/ZuneDev/ZuneModdingHelper/releases" ) ;
84- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
85- }
86- }
87-
88- private async Task OnUpdateDialogResult ( bool accepted )
89- {
90- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
91- if ( ! accepted )
92- return ;
93-
94- ProgressDialogViewModel prog = new ( )
95- {
96- Title = UPDATES_DIALOG_TITLE ,
97- Description = "Downloading update...\r \n This may take a few minutes." ,
98- IsIndeterminate = true ,
99- ShowAffirmativeButton = false ,
100- ShowNegativeButton = false ,
101- Maximum = 1.0 ,
102- } ;
103- WeakReferenceMessenger . Default . Send ( new ShowDialogMessage ( prog ) ) ;
104-
105- // Ask user to save file
106- Microsoft . Win32 . SaveFileDialog saveFileDialog = new ( )
107- {
108- FileName = _updateInfo . Name ,
109- InitialDirectory = new KnownFolder ( KnownFolderType . Downloads ) . Path
110- } ;
111- bool dialogResult = saveFileDialog . ShowDialog ( ) ?? false ;
112- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
113-
114- if ( dialogResult )
115- {
116- _downloadedUpdatePath = saveFileDialog . FileName ;
117-
118- // Download new version to requested folder with progress updates
119- Progress < double > progress = new ( p => prog . Progress = p ) ;
120- prog . Progress = 0 ;
121- prog . IsIndeterminate = false ;
122- await _updateService . DownloadUpdate ( _updateInfo , _downloadedUpdatePath , progress ) ;
123-
124- WeakReferenceMessenger . Default . Send ( new ShowDialogMessage ( new DialogViewModel
125- {
126- Title = UPDATES_DIALOG_TITLE ,
127- Description = "Update downloaded. Would you like to launch the new version?" ,
128- AffirmativeText = "YES" ,
129- NegativeText = "NO" ,
130- ShowAffirmativeButton = true ,
131- ShowNegativeButton = true ,
132- OnAction = new AsyncRelayCommand < bool > ( OnLaunchUpdateDialogResult )
133- } ) ) ;
134- }
135- }
136-
137- private async Task OnLaunchUpdateDialogResult ( bool accepted )
138- {
139- WeakReferenceMessenger . Default . Send < CloseDialogMessage > ( ) ;
140- if ( ! accepted )
141- return ;
142-
143- // In the future, this should launch an installer
144- System . Diagnostics . Process . Start ( "explorer" , _downloadedUpdatePath ) ;
28+ await UpdateHelper . Instance . CheckForUpdatesAsync ( true ) ;
14529 }
14630 }
14731}
0 commit comments