mirror of
https://github.com/ZuneDev/ZuneModdingHelper.git
synced 2026-07-27 13:12:21 -07:00
Added update checker button
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace ZuneModdingHelper
|
||||
@@ -13,5 +8,23 @@ namespace ZuneModdingHelper
|
||||
/// </summary>
|
||||
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,11 +7,14 @@
|
||||
xmlns:core="clr-namespace:ZuneModCore;assembly=ZuneModCore"
|
||||
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
|
||||
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">
|
||||
|
||||
<mah:MetroWindow.RightWindowCommands>
|
||||
<mah:WindowCommands>
|
||||
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="UpdatesButton_Click">
|
||||
<mah:FontIcon Glyph="" FontFamily="Segoe MDL2 Assets" FontSize="18"/>
|
||||
</Button>
|
||||
<Button Style="{StaticResource MahApps.Styles.Button.WindowCommands}" Click="AboutButton_Click">
|
||||
<mah:FontIcon Glyph="" FontFamily="Segoe MDL2 Assets"/>
|
||||
</Button>
|
||||
@@ -20,10 +23,10 @@
|
||||
|
||||
<mah:MetroWindow.Flyouts>
|
||||
<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">
|
||||
<Run Text="Zune Modding Helper" FontWeight="Bold" FontSize="16"/><LineBreak/>
|
||||
<Run Text="2021.4.26-alpha"/><LineBreak/>
|
||||
<Run Text="{x:Static local:App.Title}" FontWeight="Bold" FontSize="16"/><LineBreak/>
|
||||
<Run Text="{x:Static local:App.Version}"/><LineBreak/>
|
||||
<Hyperlink NavigateUri="https://github.com/yoshiask/ZuneModdingHelper" RequestNavigate="Link_RequestNavigate">
|
||||
<Run Text="View source"/>
|
||||
</Hyperlink><LineBreak/>
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
using ControlzEx.Theming;
|
||||
using Flurl.Http;
|
||||
using MahApps.Metro.Controls;
|
||||
using MahApps.Metro.Controls.Dialogs;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OwlCore.AbstractUI.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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.Shapes;
|
||||
using ZuneModCore;
|
||||
|
||||
namespace ZuneModdingHelper
|
||||
@@ -139,5 +134,65 @@ namespace ZuneModdingHelper
|
||||
});
|
||||
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>
|
||||
<PackageReference Include="Flurl.Http" Version="3.0.1" />
|
||||
<PackageReference Include="MahApps.Metro" Version="2.4.5" />
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" />
|
||||
<PackageReference Include="OwlCore" Version="0.0.1" />
|
||||
|
||||
Reference in New Issue
Block a user