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>
public bool CheckUpdate { get; set; } = true;
/// <summary>
/// Last UNIX timestamp to check for update.
/// </summary>
public int LastCheckUpdate { get; set; } = 0;
/// <summary>
/// Settings for executables.
/// </summary>
public ToolSetting Tools { get; set; } = new ToolSetting();

View file

@ -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);
}
}
/// <summary>
@ -142,5 +154,32 @@ namespace SourceGit {
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.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);
}
/// <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
/// <summary>
/// Add new tab.