Skip to content

Commit

Permalink
#362: Can now compare the users app version to the latest version on …
Browse files Browse the repository at this point in the history
…GitHub
  • Loading branch information
jameljoseph committed Nov 28, 2024
1 parent 9ec0cba commit 701698a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion GSCFieldApp/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.Below="ProjectURLsTextBlock" TextWrapping="Wrap">
<Run x:Uid="DownloadLinkTextBlock" />
<Hyperlink NavigateUri="https://github.com/NRCan/GSC-Field-Application/releases">
<Hyperlink NavigateUri="https://github.com/NRCan/GSC-Field-Application/releases" Click="OnDownloadLinkClicked">
<Run x:Uid="RepoLinkTextBlock" />
</Hyperlink>
</TextBlock>
Expand Down
57 changes: 57 additions & 0 deletions GSCFieldApp/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using Template10.Controls;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Navigation;


Expand Down Expand Up @@ -120,5 +125,57 @@ private void AboutTeamPicture_Tapped(object sender, Windows.UI.Xaml.Input.Tapped
this.AboutTeamPicture.Visibility = Visibility.Collapsed;
}

//Check to see if the user is using the latest app version
private async void OnDownloadLinkClicked(Hyperlink sender, HyperlinkClickEventArgs args)
{
try
{
string CurrentVersion = GetCurrentVersion();
string LatestVersion = await GetLatestVersionFromGitHubAsync();

if (string.IsNullOrWhiteSpace(LatestVersion))
{
ShowMessage("Unable to retrieve the latest version. Please try again later.");
return;
}

if (CurrentVersion == LatestVersion)
{
ShowMessage($"You have the latest version: {CurrentVersion}.");
}
else
{
ShowMessage($"A new version is available: {LatestVersion}. Your version: {CurrentVersion}.");
}
}
catch (Exception ex)
{
ShowMessage($"Error checking for updates: {ex.Message}");
}
}

private string GetCurrentVersion()
{
var version = Package.Current.Id.Version;
return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}

private async Task<string> GetLatestVersionFromGitHubAsync()
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AppVersionChecker/1.0)");
var response = await client.GetStringAsync("https://api.github.com/repos/NRCan/GSC-Field-Application/releases/latest");
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(response); // Install Newtonsoft.Json
return json?.tag_name; // Assuming 'tag_name' holds the release version, adjust if necessary
}
}

private void ShowMessage(string message)
{
var dialog = new Windows.UI.Popups.MessageDialog(message);
dialog.ShowAsync();
}

}
}

0 comments on commit 701698a

Please sign in to comment.