diff --git a/src/App.preference.cs b/src/App.preference.cs index f00d297c..d51fe99a 100644 --- a/src/App.preference.cs +++ b/src/App.preference.cs @@ -106,6 +106,10 @@ namespace SourceGit { /// public bool CheckUpdate { get; set; } = true; /// + /// Last UNIX timestamp to check for update. + /// + public int LastCheckUpdate { get; set; } = 0; + /// /// Settings for executables. /// public ToolSetting Tools { get; set; } = new ToolSetting(); diff --git a/src/App.xaml.cs b/src/App.xaml.cs index d8fb11a1..c2c2f757 100644 --- a/src/App.xaml.cs +++ b/src/App.xaml.cs @@ -2,6 +2,11 @@ using Microsoft.Win32; using Newtonsoft.Json; using System; using System.IO; +using System.Net; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; using System.Windows; namespace SourceGit { @@ -105,8 +110,15 @@ namespace SourceGit { } // Show main window - Current.MainWindow = new UI.Launcher(); - Current.MainWindow.Show(); + MainWindow = new UI.Launcher(); + MainWindow.Show(); + + // Check for update. + if (Setting.CheckUpdate && Setting.LastCheckUpdate != DateTime.Now.DayOfYear) { + Setting.LastCheckUpdate = DateTime.Now.DayOfYear; + SaveSetting(); + Task.Run(CheckUpdate); + } } /// @@ -116,7 +128,7 @@ namespace SourceGit { /// private void OnAppDeactivated(object sender, EventArgs e) { GC.Collect(); - SaveSetting(); + SaveSetting(); } /// @@ -142,5 +154,32 @@ namespace SourceGit { return true; } + + /// + /// Check for update. + /// + private void CheckUpdate() { + try { + var web = new WebClient() { Encoding = Encoding.UTF8 }; + var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/SourceGit/releases/latest"); + var ver = JsonConvert.DeserializeObject(raw); + var cur = Assembly.GetExecutingAssembly().GetName().Version; + + var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*"); + if (!matches.Success) return; + + var major = int.Parse(matches.Groups[1].Value); + var minor = int.Parse(matches.Groups[2].Value); + if (major > cur.Major || (major == cur.Major && minor > cur.Minor)) { + Dispatcher.Invoke(() => { + var dialog = new UI.UpdateAvailable(ver); + dialog.Owner = MainWindow; + dialog.ShowDialog(); + }); + } + } catch { + // IGNORE + } + } } } diff --git a/src/UI/Launcher.xaml.cs b/src/UI/Launcher.xaml.cs index 2f30b00b..1cc6d1d8 100644 --- a/src/UI/Launcher.xaml.cs +++ b/src/UI/Launcher.xaml.cs @@ -1,16 +1,9 @@ -using Newtonsoft.Json; using System.Collections.ObjectModel; using System.ComponentModel; -using System.Net; -using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; -using System.Windows.Shapes; namespace SourceGit.UI { @@ -105,7 +98,6 @@ namespace SourceGit.UI { public Launcher() { InitializeComponent(); NewTab(null, null); - if (App.Setting.CheckUpdate) Task.Run(CheckUpdate); } /// @@ -135,33 +127,6 @@ namespace SourceGit.UI { } } - /// - /// Checking for update. - /// - public void CheckUpdate() { - try { - var web = new WebClient() { Encoding = Encoding.UTF8 }; - var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/SourceGit/releases/latest"); - var ver = JsonConvert.DeserializeObject(raw); - var cur = Assembly.GetExecutingAssembly().GetName().Version; - - var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*"); - if (!matches.Success) return; - - var major = int.Parse(matches.Groups[1].Value); - var minor = int.Parse(matches.Groups[2].Value); - if (major > cur.Major || (major == cur.Major && minor > cur.Minor)) { - Dispatcher.Invoke(() => { - var dialog = new UpdateAvailable(ver); - dialog.Owner = this; - dialog.ShowDialog(); - }); - } - } catch { - // IGNORE - } - } - #region LAYOUT_CONTENT /// /// Add new tab.