Skip to content

Commit 8fb80b5

Browse files
committed
Added update checker button
1 parent eef715d commit 8fb80b5

4 files changed

Lines changed: 89 additions & 17 deletions

File tree

ZuneModdingHelper/App.xaml.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
72
using System.Windows;
83

94
namespace ZuneModdingHelper
@@ -13,5 +8,23 @@ namespace ZuneModdingHelper
138
/// </summary>
149
public partial class App : Application
1510
{
11+
public static string Title => "Zune Modding Helper";
12+
13+
public static Version VersionNum => new(2021, 4, 26, 1);
14+
public static string VersionStatus => "alpha";
15+
public static string Version => VersionNum.ToString() + (VersionStatus != string.Empty ? "-" + VersionStatus : string.Empty);
16+
17+
public static bool CheckIfNewerVersion(string otherStr)
18+
{
19+
int idxSplit = otherStr.IndexOf('-');
20+
Version otherNum = new(otherStr[..idxSplit]);
21+
//string otherStatus = otherStr[(idxSplit + 1)..];
22+
23+
// TODO: This assumes that the VersionStatus is "alpha"
24+
//bool isNotAlpha = otherStatus != VersionStatus;
25+
bool isNotAlpha = !otherStr.EndsWith(VersionStatus);
26+
bool isNewer = otherNum > VersionNum;
27+
return isNotAlpha || isNewer;
28+
}
1629
}
1730
}

ZuneModdingHelper/MainWindow.xaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
xmlns:core="clr-namespace:ZuneModCore;assembly=ZuneModCore"
88
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
99
mc:Ignorable="d"
10-
Title="Zune Modding Helper" Height="450" Width="800"
10+
Title="{x:Static local:App.Title}" Height="450" Width="800" WindowStartupLocation="CenterScreen"
1111
Loaded="Window_Loaded">
1212

1313
<mah:MetroWindow.RightWindowCommands>
1414
<mah:WindowCommands>
15+
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="UpdatesButton_Click">
16+
<mah:FontIcon Glyph="&#xE895;" FontFamily="Segoe MDL2 Assets" FontSize="18"/>
17+
</Button>
1518
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="AboutButton_Click">
1619
<mah:FontIcon Glyph="&#xE946;" FontFamily="Segoe MDL2 Assets"/>
1720
</Button>
@@ -20,10 +23,10 @@
2023

2124
<mah:MetroWindow.Flyouts>
2225
<mah:FlyoutsControl>
23-
<mah:Flyout x:Name="AboutFlyout" Header="About" Position="Right">
26+
<mah:Flyout x:Name="AboutFlyout" Header="About" Position="Right" IsModal="True" Theme="Adapt">
2427
<TextBlock FontSize="14" Padding="16" TextWrapping="Wrap">
25-
<Run Text="Zune Modding Helper" FontWeight="Bold" FontSize="16"/><LineBreak/>
26-
<Run Text="2021.4.26-alpha"/><LineBreak/>
28+
<Run Text="{x:Static local:App.Title}" FontWeight="Bold" FontSize="16"/><LineBreak/>
29+
<Run Text="{x:Static local:App.Version}"/><LineBreak/>
2730
<Hyperlink NavigateUri="https://github.com/yoshiask/ZuneModdingHelper" RequestNavigate="Link_RequestNavigate">
2831
<Run Text="View source"/>
2932
</Hyperlink><LineBreak/>

ZuneModdingHelper/MainWindow.xaml.cs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
using ControlzEx.Theming;
2+
using Flurl.Http;
23
using MahApps.Metro.Controls;
34
using MahApps.Metro.Controls.Dialogs;
5+
using Newtonsoft.Json.Linq;
46
using OwlCore.AbstractUI.ViewModels;
57
using System;
68
using System.Collections.Generic;
79
using System.Diagnostics;
10+
using System.IO;
811
using System.Linq;
9-
using System.Text;
1012
using System.Threading.Tasks;
1113
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;
1814
using System.Windows.Navigation;
19-
using System.Windows.Shapes;
2015
using ZuneModCore;
2116

2217
namespace ZuneModdingHelper
@@ -139,5 +134,65 @@ private void Link_RequestNavigate(object sender, RequestNavigateEventArgs e)
139134
});
140135
e.Handled = true;
141136
}
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+
}
142197
}
143198
}

ZuneModdingHelper/ZuneModdingHelper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="Flurl.Http" Version="3.0.1" />
1516
<PackageReference Include="MahApps.Metro" Version="2.4.5" />
1617
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" />
1718
<PackageReference Include="OwlCore" Version="0.0.1" />

0 commit comments

Comments
 (0)