Skip to content

Commit 5f0b00c

Browse files
committed
Check for updates on startup
1 parent d8f9a3c commit 5f0b00c

File tree

3 files changed

+161
-119
lines changed

3 files changed

+161
-119
lines changed

ZuneModdingHelper/AppWindow.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
4545
{
4646
await _settings.LoadAsync();
4747

48+
// Check for updates in the background
49+
UpdateHelper.Instance.CheckForUpdatesAsync(false);
50+
4851
// Show a warning if Zune isn't found. Usually happens if Zune isn't installed,
4952
// is installed in an unusual directory, or the user is running ZMH 32-bit
5053
// on a 64-bit system.
Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
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;
82
using System.Windows.Navigation;
9-
using ZuneModdingHelper.Messages;
103
using ZuneModdingHelper.Services;
114

125
namespace 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\nYou'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\nThis 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
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using ZuneModdingHelper.Messages;
12+
13+
namespace ZuneModdingHelper.Services;
14+
15+
public class UpdateHelper
16+
{
17+
public const string UPDATES_DIALOG_TITLE = "UPDATES";
18+
19+
private readonly IUpdateService? _updateService = Ioc.Default.GetService<IUpdateService>();
20+
private bool _isRunning = false;
21+
private UpdateAvailableInfo _updateInfo;
22+
private string _downloadedUpdatePath;
23+
24+
public static UpdateHelper Instance { get; } = new();
25+
26+
public bool Enabled => _updateService is not null;
27+
28+
public async Task CheckForUpdatesAsync(bool isUserTriggered)
29+
{
30+
if (_isRunning)
31+
return;
32+
33+
_isRunning = true;
34+
35+
if (isUserTriggered)
36+
{
37+
WeakReferenceMessenger.Default.Send(new ShowDialogMessage(new ProgressDialogViewModel
38+
{
39+
Title = UPDATES_DIALOG_TITLE,
40+
Description = "Checking for updates, please wait...",
41+
IsIndeterminate = true,
42+
ShowAffirmativeButton = false,
43+
}));
44+
}
45+
46+
try
47+
{
48+
await Task.Delay(5000);
49+
_updateInfo = await _updateService.FetchAvailableUpdate();
50+
51+
if (isUserTriggered)
52+
WeakReferenceMessenger.Default.Send<CloseDialogMessage>();
53+
54+
if (_updateInfo is null)
55+
{
56+
if (isUserTriggered)
57+
{
58+
WeakReferenceMessenger.Default.Send(new ShowDialogMessage(new DialogViewModel
59+
{
60+
Title = UPDATES_DIALOG_TITLE,
61+
Description = "No updates available.\r\n\r\nYou're already using the latest version.",
62+
ShowAffirmativeButton = true,
63+
}));
64+
}
65+
66+
_isRunning = false;
67+
return;
68+
}
69+
70+
// Newer version available, prompt user to download
71+
72+
WeakReferenceMessenger.Default.Send(new ShowDialogMessage(new DialogViewModel
73+
{
74+
Title = UPDATES_DIALOG_TITLE,
75+
Description = $"Release {_updateInfo.Name} is available. Would you like to download it now?",
76+
AffirmativeText = "YES",
77+
NegativeText = "NO",
78+
ShowAffirmativeButton = true,
79+
ShowNegativeButton = true,
80+
OnAction = new AsyncRelayCommand<bool>(OnUpdateDialogResult)
81+
}));
82+
}
83+
catch
84+
{
85+
if (isUserTriggered)
86+
{
87+
App.OpenInBrowser($"{App.RepoUri}/releases");
88+
WeakReferenceMessenger.Default.Send<CloseDialogMessage>();
89+
}
90+
91+
_isRunning = false;
92+
}
93+
}
94+
95+
private async Task OnUpdateDialogResult(bool accepted)
96+
{
97+
WeakReferenceMessenger.Default.Send<CloseDialogMessage>();
98+
if (!accepted)
99+
return;
100+
101+
ProgressDialogViewModel prog = new()
102+
{
103+
Title = UPDATES_DIALOG_TITLE,
104+
Description = "Downloading update...\r\nThis may take a few minutes.",
105+
IsIndeterminate = true,
106+
ShowAffirmativeButton = false,
107+
ShowNegativeButton = false,
108+
Maximum = 1.0,
109+
};
110+
WeakReferenceMessenger.Default.Send(new ShowDialogMessage(prog));
111+
112+
// Ask user to save file
113+
Microsoft.Win32.SaveFileDialog saveFileDialog = new()
114+
{
115+
FileName = _updateInfo.Name,
116+
InitialDirectory = new KnownFolder(KnownFolderType.Downloads).Path
117+
};
118+
bool dialogResult = saveFileDialog.ShowDialog() ?? false;
119+
WeakReferenceMessenger.Default.Send<CloseDialogMessage>();
120+
121+
if (dialogResult)
122+
{
123+
_downloadedUpdatePath = saveFileDialog.FileName;
124+
125+
// Download new version to requested folder with progress updates
126+
Progress<double> progress = new(p => prog.Progress = p);
127+
prog.Progress = 0;
128+
prog.IsIndeterminate = false;
129+
await _updateService.DownloadUpdate(_updateInfo, _downloadedUpdatePath, progress);
130+
131+
WeakReferenceMessenger.Default.Send(new ShowDialogMessage(new DialogViewModel
132+
{
133+
Title = UPDATES_DIALOG_TITLE,
134+
Description = "Update downloaded. Would you like to launch the new version?",
135+
AffirmativeText = "YES",
136+
NegativeText = "NO",
137+
ShowAffirmativeButton = true,
138+
ShowNegativeButton = true,
139+
OnAction = new AsyncRelayCommand<bool>(OnLaunchUpdateDialogResult)
140+
}));
141+
}
142+
143+
_isRunning = false;
144+
}
145+
146+
private async Task OnLaunchUpdateDialogResult(bool accepted)
147+
{
148+
WeakReferenceMessenger.Default.Send<CloseDialogMessage>();
149+
if (!accepted)
150+
return;
151+
152+
// In the future, this should launch an installer
153+
System.Diagnostics.Process.Start("explorer", _downloadedUpdatePath);
154+
}
155+
}

0 commit comments

Comments
 (0)