optimize<AutoUpdate>: move Launcher.CheckUpdate to App.CheckUpdate; limitation for checking times

This commit is contained in:
leo 2021-01-07 20:34:41 +08:00
parent ae1224ded3
commit 4a334107e1
3 changed files with 46 additions and 38 deletions

View file

@ -106,6 +106,10 @@ namespace SourceGit {
/// </summary> /// </summary>
public bool CheckUpdate { get; set; } = true; public bool CheckUpdate { get; set; } = true;
/// <summary> /// <summary>
/// Last UNIX timestamp to check for update.
/// </summary>
public int LastCheckUpdate { get; set; } = 0;
/// <summary>
/// Settings for executables. /// Settings for executables.
/// </summary> /// </summary>
public ToolSetting Tools { get; set; } = new ToolSetting(); public ToolSetting Tools { get; set; } = new ToolSetting();

View file

@ -2,6 +2,11 @@ using Microsoft.Win32;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace SourceGit { namespace SourceGit {
@ -105,8 +110,15 @@ namespace SourceGit {
} }
// Show main window // Show main window
Current.MainWindow = new UI.Launcher(); MainWindow = new UI.Launcher();
Current.MainWindow.Show(); MainWindow.Show();
// Check for update.
if (Setting.CheckUpdate && Setting.LastCheckUpdate != DateTime.Now.DayOfYear) {
Setting.LastCheckUpdate = DateTime.Now.DayOfYear;
SaveSetting();
Task.Run(CheckUpdate);
}
} }
/// <summary> /// <summary>
@ -116,7 +128,7 @@ namespace SourceGit {
/// <param name="e"></param> /// <param name="e"></param>
private void OnAppDeactivated(object sender, EventArgs e) { private void OnAppDeactivated(object sender, EventArgs e) {
GC.Collect(); GC.Collect();
SaveSetting(); SaveSetting();
} }
/// <summary> /// <summary>
@ -142,5 +154,32 @@ namespace SourceGit {
return true; return true;
} }
/// <summary>
/// Check for update.
/// </summary>
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<Git.Version>(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
}
}
} }
} }

View file

@ -1,16 +1,9 @@
using Newtonsoft.Json;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; 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;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes;
namespace SourceGit.UI { namespace SourceGit.UI {
@ -105,7 +98,6 @@ namespace SourceGit.UI {
public Launcher() { public Launcher() {
InitializeComponent(); InitializeComponent();
NewTab(null, null); NewTab(null, null);
if (App.Setting.CheckUpdate) Task.Run(CheckUpdate);
} }
/// <summary> /// <summary>
@ -135,33 +127,6 @@ namespace SourceGit.UI {
} }
} }
/// <summary>
/// Checking for update.
/// </summary>
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<Git.Version>(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 #region LAYOUT_CONTENT
/// <summary> /// <summary>
/// Add new tab. /// Add new tab.