diff --git a/GenshinLyreMidiPlayer/Models/GitVersion.cs b/GenshinLyreMidiPlayer/Models/GitVersion.cs new file mode 100644 index 0000000..04b19bd --- /dev/null +++ b/GenshinLyreMidiPlayer/Models/GitVersion.cs @@ -0,0 +1,16 @@ +using System; +using System.Text.Json.Serialization; + +namespace GenshinLyreMidiPlayer.Models +{ + public class GitVersion + { + public bool Draft { get; set; } + + public bool Prerelease { get; set; } + + [JsonPropertyName("tag_name")] public string TagName { get; set; } + + public Version Version => new(TagName.Replace("v", string.Empty)); + } +} \ No newline at end of file diff --git a/GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs b/GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs index c347ffe..2f4d48c 100644 --- a/GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs +++ b/GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs @@ -1,9 +1,15 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Reflection; +using System.Text.Json; +using System.Threading.Tasks; +using GenshinLyreMidiPlayer.Models; using GenshinLyreMidiPlayer.Views; using ModernWpf.Controls; using Stylet; -using StyletIoC; namespace GenshinLyreMidiPlayer.ViewModels { @@ -11,18 +17,58 @@ public class MainWindowViewModel : Conductor.StackNavigation { private readonly Stack _history = new(); - public MainWindowViewModel(IContainer ioc, IEventAggregator events) + public MainWindowViewModel(IEventAggregator events) { SettingsView = new SettingsPageViewModel(events); PlayerView = new LyrePlayerViewModel(events, SettingsView); + + Task.Run(async () => + { + var client = new HttpClient(); + UpdateString += "(Checking for updates)"; + IsCheckingUpdate = true; + + try + { + var request = new HttpRequestMessage(HttpMethod.Get, + "https://api.github.com/repos/sabihoshi/GenshinLyreMidiPlayer/releases/latest"); + + var productInfo = new ProductInfoHeaderValue("GenshinLyreMidiPlayer", ProgramVersion?.ToString()); + + request.Headers.UserAgent.Add(productInfo); + + var response = await client.SendAsync(request); + var version = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); + if (version.Version > ProgramVersion && !(version.Draft || version.Prerelease)) + UpdateString = $"(Update available! {version.TagName})"; + else + UpdateString = string.Empty; + } + catch (Exception) + { + // Ignored + } + finally + { + IsCheckingUpdate = false; + } + }); } + public bool IsCheckingUpdate { get; set; } + public LyrePlayerViewModel PlayerView { get; } public SettingsPageViewModel SettingsView { get; } public string Title { get; set; } = "Genshin Lyre MIDI Player"; + public string UpdateString { get; set; } + + public string VersionString { get; set; } = $"v{ProgramVersion?.ToString(3)}"; + + private static Version? ProgramVersion { get; } = Assembly.GetExecutingAssembly().GetName().Version; + protected override void OnViewLoaded() { // Work around because events do not conform to the signatures Stylet supports diff --git a/GenshinLyreMidiPlayer/Views/MainWindowView.xaml b/GenshinLyreMidiPlayer/Views/MainWindowView.xaml index 97f15a7..653c544 100644 --- a/GenshinLyreMidiPlayer/Views/MainWindowView.xaml +++ b/GenshinLyreMidiPlayer/Views/MainWindowView.xaml @@ -17,15 +17,20 @@ ui:WindowHelper.UseModernWindowStyle="True" ui:TitleBar.ExtendViewIntoTitleBar="True"> - - + Style="{DynamicResource CaptionTextBlockStyle}"> + + + + + + @@ -68,10 +68,24 @@ - + + + + + + + + Created by © 2021 sabihoshi under the + + MIT License + . + + © All rights reserved by miHoYo Co., Ltd. This project is not affiliated nor endorsed by miHoYo. Genshin Impact™ and other properties belong to their respective owners. +