Skip to content

Commit

Permalink
Check for updates through github
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercamp committed May 9, 2024
1 parent 5d1b693 commit 9d5eb37
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions PalCalc.SaveReader/SaveFile/LevelSaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ private LevelSaveData BuildResult(PalDB db, List<GvasCharacterInstance> characte
// references a container ID not in this list. the cause is not known, but I've seen "effective"
// container sizes from 1 to 40. there's no clear answer to "where" this container is (or its
// pals), so we won't bother referencing it
//
// (might be due to butchered pals? https://github.com/tylercamp/palcalc/issues/12#issuecomment-2101688781)
logger.Warning("unrecognized pal container id '{id}', skipping", gvasInstance.ContainerId);
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion PalCalc.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace PalCalc.UI
/// </summary>
public partial class App : Application
{
public static string Version => "v1.0.1";
public static string Version => "v1.0.3";
public static string RepositoryUrl => "https://github.com/tylercamp/palcalc/";

private static ILogger logger;

Expand Down
10 changes: 7 additions & 3 deletions PalCalc.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@
<v:BreedingResultListView DataContext="{Binding PalTarget.CurrentPalSpecifier.CurrentResults}" />
</GroupBox>

<Grid Grid.Row="1" Margin="0,5,0,0">
<Button Width="50" Click="AboutButton_Click">About</Button>
</Grid>
<StackPanel Grid.Row="1">
<Button Width="50" Click="AboutButton_Click" Margin="0,5,0,5">About</Button>
<TextBlock Visibility="{Binding UpdatesMessageVisibility}" TextWrapping="Wrap" TextAlignment="Center">
<!-- NavigateUri unused, value must be set for event to trigger -->
<Hyperlink NavigateUri="http://github.com" RequestNavigate="DownloadUpdateLink_RequestNavigate">An update is available!</Hyperlink>
</TextBlock>
</StackPanel>
</Grid>
</DockPanel>
</Grid>
Expand Down
5 changes: 5 additions & 0 deletions PalCalc.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ private void AboutButton_Click(object sender, RoutedEventArgs e)
window.Owner = this;
window.ShowDialog();
}

private void DownloadUpdateLink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
ViewModel.TryDownloadLatestVersion();
}
}
}
55 changes: 55 additions & 0 deletions PalCalc.UI/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -149,6 +151,8 @@ public MainWindowViewModel(Dispatcher dispatcher)
if (settings.SelectedGameIdentifier != null) SaveSelection.TrySelectSaveGame(settings.SelectedGameIdentifier);

dispatcher.BeginInvoke(UpdateFromSaveProperties, DispatcherPriority.Background);

CheckForUpdates();
}

private void SaveSelection_CustomSaveAdded(ManualSavesLocationViewModel manualSaves, ISaveGame save)
Expand Down Expand Up @@ -450,6 +454,57 @@ public bool IsEditable

public Visibility ProgressBarVisibility => string.IsNullOrEmpty(SolverStatusMsg) ? Visibility.Collapsed : Visibility.Visible;

[ObservableProperty]
private Visibility updatesMessageVisibility = Visibility.Collapsed;

private string VersionFromUrl(string url) => url.Split('/').Last();
private string latestVersionUrl;

private void CheckForUpdates()
{
Task.Run(async () =>
{
try
{
var latestReleaseUrl = $"{App.RepositoryUrl}/releases/latest";
using (var client = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }))
using (var response = await client.GetAsync(latestReleaseUrl))
{
if (response.StatusCode == HttpStatusCode.Found)
{
var location = response.Headers?.Location;
if (location != null)
{
latestVersionUrl = location.AbsoluteUri;
var latestVersion = VersionFromUrl(latestVersionUrl);
if (latestVersion != App.Version)
{
dispatcher.BeginInvoke(() => UpdatesMessageVisibility = Visibility.Visible);
}
}
else
{
logger.Warning("releases response did not include redirect URL, unable to determine latest version");
}
}
else
{
logger.Warning("did not receive FOUND status code, unable to determine latest version");
}
}
}
catch (Exception e)
{
logger.Warning(e, "error fetching latest version");
}
});
}

public void TryDownloadLatestVersion()
{
Process.Start(new ProcessStartInfo { FileName = latestVersionUrl, UseShellExecute = true });
}

public PalDB DB => db;
}
}

0 comments on commit 9d5eb37

Please sign in to comment.