|
1 | 1 | using ControlzEx.Theming; |
| 2 | +using Flurl.Http; |
2 | 3 | using MahApps.Metro.Controls; |
3 | 4 | using MahApps.Metro.Controls.Dialogs; |
| 5 | +using Newtonsoft.Json.Linq; |
4 | 6 | using OwlCore.AbstractUI.ViewModels; |
5 | 7 | using System; |
6 | 8 | using System.Collections.Generic; |
7 | 9 | using System.Diagnostics; |
| 10 | +using System.IO; |
8 | 11 | using System.Linq; |
9 | | -using System.Text; |
10 | 12 | using System.Threading.Tasks; |
11 | 13 | using System.Windows; |
12 | | -using System.Windows.Controls; |
13 | | -using System.Windows.Data; |
14 | | -using System.Windows.Documents; |
15 | | -using System.Windows.Input; |
16 | | -using System.Windows.Media; |
17 | | -using System.Windows.Media.Imaging; |
18 | 14 | using System.Windows.Navigation; |
19 | | -using System.Windows.Shapes; |
20 | 15 | using ZuneModCore; |
21 | 16 |
|
22 | 17 | namespace ZuneModdingHelper |
@@ -139,5 +134,65 @@ private void Link_RequestNavigate(object sender, RequestNavigateEventArgs e) |
139 | 134 | }); |
140 | 135 | e.Handled = true; |
141 | 136 | } |
| 137 | + |
| 138 | + private async void UpdatesButton_Click(object sender, RoutedEventArgs e) |
| 139 | + { |
| 140 | + var checkDialog = await this.ShowProgressAsync("Checking for updates...", "Please wait.", settings: defaultMetroDialogSettings); |
| 141 | + checkDialog.SetIndeterminate(); |
| 142 | + |
| 143 | + // Get releases list from GitHub |
| 144 | + List<JObject> releases = await "https://api.github.com/repos/yoshiask/ZuneModdingHelper/releases" |
| 145 | + .WithHeader("User-Agent", App.Title).GetJsonAsync<List<JObject>>(); |
| 146 | + JObject latest = releases[0]; |
| 147 | + if (!App.CheckIfNewerVersion(latest["tag_name"].Value<string>())) |
| 148 | + { |
| 149 | + // Already up-to-date |
| 150 | + await checkDialog.CloseAsync(); |
| 151 | + await this.ShowMessageAsync("No updates available", "You're already using the latest version.", settings: defaultMetroDialogSettings); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + // Newer version available, prompt user to download |
| 156 | + MetroDialogSettings promptSettings = new() |
| 157 | + { |
| 158 | + ColorScheme = MetroDialogColorScheme.Accented, |
| 159 | + AnimateShow = true, |
| 160 | + AnimateHide = true, |
| 161 | + AffirmativeButtonText = "Download", |
| 162 | + NegativeButtonText = "Later" |
| 163 | + }; |
| 164 | + await checkDialog.CloseAsync(); |
| 165 | + var promptResult = await this.ShowMessageAsync("Update available", $"Relase {latest["name"]} is available. Would you like to download it now?", |
| 166 | + MessageDialogStyle.AffirmativeAndNegative, promptSettings); |
| 167 | + |
| 168 | + if (promptResult == MessageDialogResult.Affirmative) |
| 169 | + { |
| 170 | + var progDialog = await this.ShowProgressAsync("Downloading update...", "This may take a few minutes.", settings: defaultMetroDialogSettings); |
| 171 | + progDialog.SetIndeterminate(); |
| 172 | + |
| 173 | + // Download new version to AppData |
| 174 | + JObject asset = latest["assets"].ToObject<List<JObject>>()[0]; |
| 175 | + string assetName = asset["name"].Value<string>(); |
| 176 | + string downloadedFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), assetName); |
| 177 | + if (File.Exists(downloadedFile)) File.Delete(downloadedFile); |
| 178 | + using (var client = new System.Net.WebClient()) |
| 179 | + { |
| 180 | + await client.DownloadFileTaskAsync(new Uri(asset["browser_download_url"].Value<string>()), downloadedFile); |
| 181 | + } |
| 182 | + |
| 183 | + // Ask user to save file |
| 184 | + Microsoft.Win32.SaveFileDialog saveFileDialog = new() |
| 185 | + { |
| 186 | + FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", assetName) |
| 187 | + }; |
| 188 | + if (saveFileDialog.ShowDialog() == true) |
| 189 | + { |
| 190 | + File.Copy(downloadedFile, saveFileDialog.FileName, true); |
| 191 | + |
| 192 | + await progDialog.CloseAsync(); |
| 193 | + await this.ShowMessageAsync("Update complete", "You may now exit this program and open the new version.", settings: defaultMetroDialogSettings); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
142 | 197 | } |
143 | 198 | } |
0 commit comments