Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcapp committed Sep 28, 2024
1 parent 4ec259c commit 19e47b7
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 98 deletions.
43 changes: 3 additions & 40 deletions KindleMate2/FrmAboutBox.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using DarkModeForms;
Expand Down Expand Up @@ -76,7 +75,7 @@ private void FrmAboutBox_Load(object sender, EventArgs e) {
okButton.Text = Strings.Confirm_Button;

var bw = new BackgroundWorker();
bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = GetRepoInfo(); };
bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = StaticData.GetRepoInfo(); };
bw.RunWorkerAsync();
bw.RunWorkerCompleted += (_, workerCompletedEventArgs) => {
if (workerCompletedEventArgs.Result == null) {
Expand All @@ -89,46 +88,10 @@ private void FrmAboutBox_Load(object sender, EventArgs e) {
var tagName = string.IsNullOrWhiteSpace(release.tag_name) ? string.Empty : release.tag_name;
var toolTip = new ToolTip();
toolTip.SetToolTip(pictureBox1, Strings.New_Version + tagName);
pictureBox1.Visible = IsUpdate(assemblyVersion, tagName);
pictureBox1.Visible = _staticData.IsUpdate(assemblyVersion, tagName);
};
}

private static GitHubRelease GetRepoInfo() {
const string url = "https://api.github.com/repos/lzcapp/KindleMate2/releases";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
var response = httpClient.GetStringAsync(url).Result;
return JsonConvert.DeserializeObject<GitHubRelease[]>(response)?[0] ?? new GitHubRelease();
}

private bool IsUpdate(string current, string tagname) {
DateTime normalizeVersion = NormalizeVersion(current);
DateTime normalizeTagName = NormalizeVersion(tagname);
if (normalizeVersion != DateTime.MinValue || normalizeTagName != DateTime.MinValue) {
return normalizeVersion < normalizeTagName;
}
var splitVersion = current.Split('.');
var splitTagname = current.Split('.');
for (var i = 0; i < 3; i++) {
if (!int.TryParse(splitVersion[i], out var intVersion) || !int.TryParse(splitTagname[i], out var intTagname)) {
continue;
}
if (intVersion < intTagname) {
return true;
}
}
return false;
}

private static DateTime NormalizeVersion(string version) {
var date = DateTime.MinValue;
var parts = version.Split('.');
if (int.TryParse(parts[0], out var year) && int.TryParse(parts[1], out var month) && int.TryParse(parts[2], out var day)) {
date = new DateTime(year, month, day);
}
return date;
}

private void pictureBox1_Click(object sender, EventArgs e) {
const string repoUrl = "https://github.com/lzcapp/KindleMate2/releases/latest";
try {
Expand Down
33 changes: 31 additions & 2 deletions KindleMate2/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Data.SQLite;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DarkModeForms;
using KindleMate2.Entities;
using Markdig;
Expand Down Expand Up @@ -73,6 +75,29 @@ public FrmMain() {
_staticData.DisposeConnection();
};

var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
var bw = new BackgroundWorker();
bw.DoWork += (_, workEventArgs) => { workEventArgs.Result = StaticData.GetRepoInfo(); };
bw.RunWorkerAsync();
bw.RunWorkerCompleted += (_, workerCompletedEventArgs) => {
if (workerCompletedEventArgs.Result == null) {
return;
}
var release = (GitHubRelease)workerCompletedEventArgs.Result;
if (string.IsNullOrWhiteSpace(assemblyVersion)) {
return;
}
var tagName = string.IsNullOrWhiteSpace(release.tag_name) ? string.Empty : release.tag_name;
var toolTip = new ToolTip();
var isUpdate = _staticData.IsUpdate(assemblyVersion, tagName);
if (isUpdate) {
DialogResult resultUpdate = Messenger.MessageBox(Strings.New_Version + tagName, Strings.New_Version, MessageBoxButtons.OKCancel);
if (resultUpdate == DialogResult.OK) {
OpenUrl("https://github.com/lzcapp/KindleMate2/releases/latest");
}
}
};

_programsDirectory = Environment.CurrentDirectory;
_filePath = Path.Combine(_programsDirectory, "KM2.dat");
_kindleClippingsPath = Path.Combine("documents", "My Clippings.txt");
Expand Down Expand Up @@ -1464,13 +1489,17 @@ private void MenuImportKindleMate_Click(object sender, EventArgs e) {

private void MenuRepo_Click(object sender, EventArgs e) {
const string repoUrl = "https://github.com/lzcapp/KindleMate2";
OpenUrl(repoUrl);
}

private void OpenUrl(string url) {
try {
Process.Start(new ProcessStartInfo {
FileName = repoUrl,
FileName = url,
UseShellExecute = true
});
} catch (Exception) {
Clipboard.SetText(repoUrl);
Clipboard.SetText(url);
MessageBox(Strings.Repo_URL_Copied, Strings.Prompt, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Expand Down
Loading

0 comments on commit 19e47b7

Please sign in to comment.