Skip to content

Commit 0333640

Browse files
committed
feat: check for updates
1 parent 2e3b84b commit 0333640

File tree

6 files changed

+128
-5
lines changed

6 files changed

+128
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace CrossLaunch.Features.GitHub;
4+
5+
public record GitHubRelease
6+
{
7+
[JsonPropertyName("tag_name")]
8+
public required string TagName { get; init; }
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Net.Http.Json;
4+
using System.Threading.Tasks;
5+
using CrossLaunch.Utils;
6+
using Serilog;
7+
8+
namespace CrossLaunch.Features.GitHub;
9+
10+
public class GitHubService
11+
{
12+
private readonly HttpClient _http;
13+
14+
public GitHubService()
15+
{
16+
_http = new HttpClient { BaseAddress = new Uri("https://api.github.com") };
17+
18+
_http.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
19+
}
20+
21+
public async Task<string?> GetLatestReleaseVersion(string owner, string repo)
22+
{
23+
try
24+
{
25+
var response = await _http.GetAsync($"repos/{owner}/{repo}/releases/latest");
26+
27+
if (!response.IsSuccessStatusCode)
28+
return null;
29+
30+
var release = await response.Content.ReadFromJsonAsync<GitHubRelease>(
31+
CustomJsonSerializerContext.Default.GitHubRelease
32+
);
33+
34+
return release?.TagName.Replace("v", string.Empty);
35+
}
36+
catch (Exception e)
37+
{
38+
Log.Error(e, "Latest release version of {Owner}/{Repo} could not be fetched", owner, repo);
39+
return null;
40+
}
41+
}
42+
}

CrossLaunch/GameSelection.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public GameSelection()
3535

3636
_gameService = new GameService();
3737

38-
Foo();
38+
Load();
3939
}
4040

4141
public bool IsGameRunning
@@ -56,7 +56,7 @@ public ObservableCollection<GameItemVm> Items
5656
set => SetValue(ItemsProperty, value);
5757
}
5858

59-
private async void Foo()
59+
private async void Load()
6060
{
6161
var games = await _gameService.GetGames();
6262

CrossLaunch/MainWindow.axaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
WindowState="Maximized"
1111
TransparencyLevelHint="AcrylicBlur"
1212
Background="Transparent"
13-
ExtendClientAreaToDecorationsHint="True">
13+
ExtendClientAreaToDecorationsHint="True"
14+
x:DataType="crossLaunch:MainWindow">
1415
<Panel>
1516
<ExperimentalAcrylicBorder IsHitTestVisible="False">
1617
<ExperimentalAcrylicBorder.Material>
@@ -21,14 +22,22 @@
2122
MaterialOpacity="0.65" />
2223
</ExperimentalAcrylicBorder.Material>
2324
</ExperimentalAcrylicBorder>
24-
<Grid RowDefinitions="Auto,*" Margin="40,50,40,40">
25-
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="20,0,20,20" VerticalAlignment="Center">
25+
<Grid RowDefinitions="Auto,*,Auto" Margin="40,50,40,40">
26+
<StackPanel Grid.Row="0" Margin="20,0,20,20" VerticalAlignment="Center" Orientation="Horizontal">
2627
<Image Source="Assets/Images/CrossLaunch-Logo7_80x80.png" Width="80" Height="80" />
2728
<TextBlock FontWeight="Bold" FontSize="36" TextAlignment="Center" Padding="20" Foreground="White">
2829
CrossLaunch
2930
</TextBlock>
3031
</StackPanel>
3132
<crossLaunch:GameSelection Grid.Row="1" IsHitTestVisible="True" />
33+
<Grid Grid.Row="2" ColumnDefinitions="*,*" Margin="20,0,20,0" VerticalAlignment="Bottom">
34+
<TextBlock Grid.Column="0" Text="{Binding CurrentVersion}" VerticalAlignment="Bottom" Foreground="LightGray"
35+
FontSize="18" HorizontalAlignment="Left" />
36+
<HyperlinkButton Grid.Column="1" IsVisible="{Binding IsUpdateAvailable}"
37+
Content="{Binding LatestVersion, StringFormat= 'Update available: \{0\}'}"
38+
NavigateUri="https://github.com/DerStimmler/CrossLaunch/releases/latest"
39+
HorizontalAlignment="Right" VerticalAlignment="Bottom" Foreground="LightGray" FontSize="18" />
40+
</Grid>
3241
</Grid>
3342
</Panel>
3443
</Window>

CrossLaunch/MainWindow.axaml.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
1+
using System;
2+
using System.Reflection;
3+
using Avalonia;
14
using Avalonia.Controls;
5+
using CrossLaunch.Features.GitHub;
26

37
namespace CrossLaunch;
48

59
public partial class MainWindow : Window
610
{
11+
private readonly GitHubService _gitHubService;
12+
713
public MainWindow()
814
{
915
InitializeComponent();
16+
DataContext = this;
17+
18+
_gitHubService = new GitHubService();
19+
20+
Load();
21+
}
22+
23+
private async void Load()
24+
{
25+
var currentVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
26+
currentVersion = currentVersion[..currentVersion.LastIndexOf('.')];
27+
CurrentVersion = $"v{currentVersion}";
28+
29+
var latestVersion = await _gitHubService.GetLatestReleaseVersion("DerStimmler", "CrossLaunch");
30+
31+
if (latestVersion is null)
32+
{
33+
IsUpdateAvailable = false;
34+
return;
35+
}
36+
37+
LatestVersion = $"v{latestVersion}";
38+
39+
if (latestVersion != currentVersion)
40+
IsUpdateAvailable = true;
41+
}
42+
43+
public static readonly StyledProperty<bool> IsUpdateAvailableProperty = AvaloniaProperty.Register<MainWindow, bool>(
44+
nameof(IsUpdateAvailable)
45+
);
46+
47+
public bool IsUpdateAvailable
48+
{
49+
get => GetValue(IsUpdateAvailableProperty);
50+
set => SetValue(IsUpdateAvailableProperty, value);
51+
}
52+
53+
public static readonly StyledProperty<string> CurrentVersionProperty = AvaloniaProperty.Register<MainWindow, string>(
54+
nameof(CurrentVersion)
55+
);
56+
57+
public string CurrentVersion
58+
{
59+
get => GetValue(CurrentVersionProperty);
60+
set => SetValue(CurrentVersionProperty, value);
61+
}
62+
63+
public static readonly StyledProperty<string> LatestVersionProperty = AvaloniaProperty.Register<MainWindow, string>(
64+
nameof(LatestVersion)
65+
);
66+
67+
public string LatestVersion
68+
{
69+
get => GetValue(LatestVersionProperty);
70+
set => SetValue(LatestVersionProperty, value);
1071
}
1172
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System.Collections.Generic;
22
using System.Text.Json.Serialization;
33
using CrossLaunch.Features.Epic;
4+
using CrossLaunch.Features.GitHub;
45

56
namespace CrossLaunch.Utils;
67

78
[JsonSourceGenerationOptions(WriteIndented = true)]
89
[JsonSerializable(typeof(EgDataItem))]
10+
[JsonSerializable(typeof(GitHubRelease))]
911
[JsonSerializable(typeof(EpicManifest))]
1012
[JsonSerializable(typeof(List<EgDataItem>))]
1113
public partial class CustomJsonSerializerContext : JsonSerializerContext { }

0 commit comments

Comments
 (0)