diff --git a/ObservatoryCore/UI/HttpClient.cs b/ObservatoryCore/UI/HttpClient.cs new file mode 100644 index 0000000..9e51e79 --- /dev/null +++ b/ObservatoryCore/UI/HttpClient.cs @@ -0,0 +1,36 @@ +using System; +using System.Net.Http; + +namespace Observatory +{ + public sealed class HttpClient + { + private HttpClient() + { } + + private static readonly Lazy lazy = new Lazy(() => new System.Net.Http.HttpClient()); + + public static System.Net.Http.HttpClient Client + { + get + { + return lazy.Value; + } + } + + public static string GetString(string url) + { + return lazy.Value.GetStringAsync(url).Result; + } + + public static HttpResponseMessage SendRequest(HttpRequestMessage request) + { + return lazy.Value.SendAsync(request).Result; + } + + public static System.Threading.Tasks.Task SendRequestAsync(HttpRequestMessage request) + { + return lazy.Value.SendAsync(request); + } + } +} \ No newline at end of file diff --git a/ObservatoryCore/UI/ViewModels/CoreViewModel.cs b/ObservatoryCore/UI/ViewModels/CoreViewModel.cs index 710c64c..9521e9a 100644 --- a/ObservatoryCore/UI/ViewModels/CoreViewModel.cs +++ b/ObservatoryCore/UI/ViewModels/CoreViewModel.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Diagnostics; using System.Linq; +using System.Net.Http; using Avalonia.Controls; using Observatory.Framework.Interfaces; using Observatory.UI.Models; @@ -16,9 +18,12 @@ namespace Observatory.UI.ViewModels private readonly ObservableCollection workers; private readonly ObservableCollection tabs; private string toggleButtonText; + private bool _UpdateAvailable; public CoreViewModel(IEnumerable<(IObservatoryWorker plugin, PluginManagement.PluginManager.PluginStatus signed)> workers, IEnumerable<(IObservatoryNotifier plugin, PluginManagement.PluginManager.PluginStatus signed)> notifiers) { + _UpdateAvailable = CheckUpdate(); + this.notifiers = new ObservableCollection(notifiers.Select(p => p.plugin)); this.workers = new ObservableCollection(workers.Select(p => p.plugin)); ToggleButtonText = "Start Monitor"; @@ -50,7 +55,7 @@ namespace Observatory.UI.ViewModels tabs.Add(new CoreModel() { Name = "Core", UI = new BasicUIViewModel(new ObservableCollection()) { UIType = Framework.PluginUI.UIType.Core } }); - + } public void ReadAll() @@ -93,6 +98,13 @@ namespace Observatory.UI.ViewModels Process.Start(donateOpen); } + public void GetUpdate() + { + ProcessStartInfo githubOpen = new("https://github.com/Xjph/ObservatoryCore/releases"); + githubOpen.UseShellExecute = true; + Process.Start(githubOpen); + } + public string ToggleButtonText { get => toggleButtonText; @@ -136,5 +148,51 @@ namespace Observatory.UI.ViewModels } } + private bool CheckUpdate() + { + try + { + string releasesResponse; + + var request = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri("https://api.github.com/repos/xjph/ObservatoryCore/releases"), + Headers = { { "User-Agent", "Xjph/ObservatoryCore" } } + }; + + releasesResponse = HttpClient.SendRequest(request).Content.ReadAsStringAsync().Result; + + if (!string.IsNullOrEmpty(releasesResponse)) + { + var releases = System.Text.Json.JsonDocument.Parse(releasesResponse).RootElement.EnumerateArray(); + + foreach (var release in releases) + { + if (release.GetProperty("tag_name").ToString().CompareTo("v" + System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString()) > 0) + { + return true; + } + } + } + + } + catch + { + return false; + } + + return false; + } + + private bool UpdateAvailable + { + get => _UpdateAvailable; + set + { + this.RaiseAndSetIfChanged(ref _UpdateAvailable, value); + } + } + } } diff --git a/ObservatoryCore/UI/Views/CoreView.axaml b/ObservatoryCore/UI/Views/CoreView.axaml index e8312e8..7af7236 100644 --- a/ObservatoryCore/UI/Views/CoreView.axaml +++ b/ObservatoryCore/UI/Views/CoreView.axaml @@ -42,18 +42,48 @@ - - - + -