Added update checker button

This commit is contained in:
Joshua Askharoun
2021-04-26 22:19:52 -05:00
parent eef715da97
commit 8fb80b55e6
4 changed files with 89 additions and 17 deletions
+18 -5
View File
@@ -1,9 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace ZuneModdingHelper namespace ZuneModdingHelper
@@ -13,5 +8,23 @@ namespace ZuneModdingHelper
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
public static string Title => "Zune Modding Helper";
public static Version VersionNum => new(2021, 4, 26, 1);
public static string VersionStatus => "alpha";
public static string Version => VersionNum.ToString() + (VersionStatus != string.Empty ? "-" + VersionStatus : string.Empty);
public static bool CheckIfNewerVersion(string otherStr)
{
int idxSplit = otherStr.IndexOf('-');
Version otherNum = new(otherStr[..idxSplit]);
//string otherStatus = otherStr[(idxSplit + 1)..];
// TODO: This assumes that the VersionStatus is "alpha"
//bool isNotAlpha = otherStatus != VersionStatus;
bool isNotAlpha = !otherStr.EndsWith(VersionStatus);
bool isNewer = otherNum > VersionNum;
return isNotAlpha || isNewer;
}
} }
} }
+7 -4
View File
@@ -7,11 +7,14 @@
xmlns:core="clr-namespace:ZuneModCore;assembly=ZuneModCore" xmlns:core="clr-namespace:ZuneModCore;assembly=ZuneModCore"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d" mc:Ignorable="d"
Title="Zune Modding Helper" Height="450" Width="800" Title="{x:Static local:App.Title}" Height="450" Width="800" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded"> Loaded="Window_Loaded">
<mah:MetroWindow.RightWindowCommands> <mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands> <mah:WindowCommands>
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="UpdatesButton_Click">
<mah:FontIcon Glyph="&#xE895;" FontFamily="Segoe MDL2 Assets" FontSize="18"/>
</Button>
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="AboutButton_Click"> <Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="AboutButton_Click">
<mah:FontIcon Glyph="&#xE946;" FontFamily="Segoe MDL2 Assets"/> <mah:FontIcon Glyph="&#xE946;" FontFamily="Segoe MDL2 Assets"/>
</Button> </Button>
@@ -20,10 +23,10 @@
<mah:MetroWindow.Flyouts> <mah:MetroWindow.Flyouts>
<mah:FlyoutsControl> <mah:FlyoutsControl>
<mah:Flyout x:Name="AboutFlyout" Header="About" Position="Right"> <mah:Flyout x:Name="AboutFlyout" Header="About" Position="Right" IsModal="True" Theme="Adapt">
<TextBlock FontSize="14" Padding="16" TextWrapping="Wrap"> <TextBlock FontSize="14" Padding="16" TextWrapping="Wrap">
<Run Text="Zune Modding Helper" FontWeight="Bold" FontSize="16"/><LineBreak/> <Run Text="{x:Static local:App.Title}" FontWeight="Bold" FontSize="16"/><LineBreak/>
<Run Text="2021.4.26-alpha"/><LineBreak/> <Run Text="{x:Static local:App.Version}"/><LineBreak/>
<Hyperlink NavigateUri="https://github.com/yoshiask/ZuneModdingHelper" RequestNavigate="Link_RequestNavigate"> <Hyperlink NavigateUri="https://github.com/yoshiask/ZuneModdingHelper" RequestNavigate="Link_RequestNavigate">
<Run Text="View source"/> <Run Text="View source"/>
</Hyperlink><LineBreak/> </Hyperlink><LineBreak/>
+63 -8
View File
@@ -1,22 +1,17 @@
using ControlzEx.Theming; using ControlzEx.Theming;
using Flurl.Http;
using MahApps.Metro.Controls; using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs; using MahApps.Metro.Controls.Dialogs;
using Newtonsoft.Json.Linq;
using OwlCore.AbstractUI.ViewModels; using OwlCore.AbstractUI.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes;
using ZuneModCore; using ZuneModCore;
namespace ZuneModdingHelper namespace ZuneModdingHelper
@@ -139,5 +134,65 @@ namespace ZuneModdingHelper
}); });
e.Handled = true; e.Handled = true;
} }
private async void UpdatesButton_Click(object sender, RoutedEventArgs e)
{
var checkDialog = await this.ShowProgressAsync("Checking for updates...", "Please wait.", settings: defaultMetroDialogSettings);
checkDialog.SetIndeterminate();
// Get releases list from GitHub
List<JObject> releases = await "https://api.github.com/repos/yoshiask/ZuneModdingHelper/releases"
.WithHeader("User-Agent", App.Title).GetJsonAsync<List<JObject>>();
JObject latest = releases[0];
if (!App.CheckIfNewerVersion(latest["tag_name"].Value<string>()))
{
// Already up-to-date
await checkDialog.CloseAsync();
await this.ShowMessageAsync("No updates available", "You're already using the latest version.", settings: defaultMetroDialogSettings);
return;
}
// Newer version available, prompt user to download
MetroDialogSettings promptSettings = new()
{
ColorScheme = MetroDialogColorScheme.Accented,
AnimateShow = true,
AnimateHide = true,
AffirmativeButtonText = "Download",
NegativeButtonText = "Later"
};
await checkDialog.CloseAsync();
var promptResult = await this.ShowMessageAsync("Update available", $"Relase {latest["name"]} is available. Would you like to download it now?",
MessageDialogStyle.AffirmativeAndNegative, promptSettings);
if (promptResult == MessageDialogResult.Affirmative)
{
var progDialog = await this.ShowProgressAsync("Downloading update...", "This may take a few minutes.", settings: defaultMetroDialogSettings);
progDialog.SetIndeterminate();
// Download new version to AppData
JObject asset = latest["assets"].ToObject<List<JObject>>()[0];
string assetName = asset["name"].Value<string>();
string downloadedFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), assetName);
if (File.Exists(downloadedFile)) File.Delete(downloadedFile);
using (var client = new System.Net.WebClient())
{
await client.DownloadFileTaskAsync(new Uri(asset["browser_download_url"].Value<string>()), downloadedFile);
}
// Ask user to save file
Microsoft.Win32.SaveFileDialog saveFileDialog = new()
{
FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", assetName)
};
if (saveFileDialog.ShowDialog() == true)
{
File.Copy(downloadedFile, saveFileDialog.FileName, true);
await progDialog.CloseAsync();
await this.ShowMessageAsync("Update complete", "You may now exit this program and open the new version.", settings: defaultMetroDialogSettings);
}
}
}
} }
} }
@@ -12,6 +12,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.0.1" />
<PackageReference Include="MahApps.Metro" Version="2.4.5" /> <PackageReference Include="MahApps.Metro" Version="2.4.5" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" />
<PackageReference Include="OwlCore" Version="0.0.1" /> <PackageReference Include="OwlCore" Version="0.0.1" />