Skip to content

Commit

Permalink
啟動時自動檢查更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Flier committed Jul 26, 2018
1 parent db849dc commit cca50be
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 29 deletions.
20 changes: 18 additions & 2 deletions ConvertZZ/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using ConvertZZ.Moudle;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace ConvertZZ
{
/// <summary>
Expand All @@ -20,6 +22,20 @@ public App()

nIcon.Icon = ConvertZZ.Properties.Resources.icon;
nIcon.Visible = true;
if (Settings.CheckVersion)
{
Task.Run(() =>
{
var versionReport = UpdateChecker.ChecktVersion();
if (versionReport != null && !versionReport.HaveNew)
{
if (MessageBox.Show(String.Format("發現新版本{0}(目前版本:{1})\r\n前往官網下載更新?", versionReport.Newst.ToString(), versionReport.Current.ToString()), "發現更新", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Process.Start("https://github.com/flier268/ConvertZZ/releases");
}
}
});
}
}

public static ChineseConverter ChineseConverter { get; set; } = new ChineseConverter();
Expand Down
1 change: 1 addition & 0 deletions ConvertZZ/ConvertZZ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="Moudle\FastReplace.cs" />
<Compile Include="Moudle\Hotkey.cs" />
<Compile Include="Moudle\TransferEncodeHelper.cs" />
<Compile Include="Moudle\UpdateChecker.cs" />
<Compile Include="Moudle\Window_MessageBoxEx.xaml.cs">
<DependentUpon>Window_MessageBoxEx.xaml</DependentUpon>
</Compile>
Expand Down
45 changes: 45 additions & 0 deletions ConvertZZ/Moudle/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ConvertZZ.Moudle
{
public class UpdateChecker
{
public static VersionReport ChecktVersion()
{
try
{
using (var client = new HttpClient())
{
var response = client.GetAsync("https://raw.githubusercontent.com/flier268/ConvertZZ/master/ConvertZZ/Properties/AssemblyInfo.cs").Result;

if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
string pattern = @"^\[assembly: AssemblyVersion\(""(.*?)""\)\]";
var m = Regex.Match(responseString, pattern,RegexOptions.Multiline);
if (m.Success)
{
Version ver = new Version(m.Groups[1].ToString().ToString());
Version version = Assembly.GetEntryAssembly().GetName().Version;
int tm = version.CompareTo(ver);
VersionReport versionReport = new VersionReport();
return new VersionReport() { Current = version, Newst = ver, HaveNew = tm < 0 };
}
}
}
}
catch { }
return null;
}
public class VersionReport
{
public Version Current { get; set; }
public Version Newst { get; set; }
public bool HaveNew { get; set; }
}
}
}
6 changes: 6 additions & 0 deletions ConvertZZ/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Settings()
AssistiveTouch = true;
HotKey = new HotKey();
FileConvert = new FileConvert();
CheckVersion = true;
}
/// <summary>
/// 試圖自動辨識編碼
Expand Down Expand Up @@ -91,6 +92,11 @@ public Settings()
/// </summary>
[JsonProperty("FileConvert")]
public FileConvert FileConvert { get; set; }
/// <summary>
/// 啟動時檢查更新
/// </summary>
[JsonProperty("CheckVersion")]
public bool CheckVersion { get; set; }
}

public partial class FileConvert
Expand Down
2 changes: 2 additions & 0 deletions ConvertZZ/Window_Setting.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<ToggleButton IsChecked="{Binding PromptEnable}" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" Grid.Row="2" Grid.Column="1" />
<Label Content="自動判定編碼" Grid.Row="3"/>
<ToggleButton IsChecked="{Binding RecognitionEncodingEnable}" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" Grid.Row="3" Grid.Column="1" />
<Label Content="檢查版本更新" Grid.Row="4"/>
<ToggleButton IsChecked="{Binding CheckVersion}" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" Grid.Row="4" Grid.Column="1" />
<Separator Style="{StaticResource MaterialDesignLightSeparator}" Grid.Row="4" Grid.ColumnSpan="2" />
<Label Content="預覽最大長度(k)" Grid.Row="5" VerticalAlignment="Bottom"/>
<TextBox Text="{Binding MaxPriviewLength}" materialDesign:HintAssist.Hint="預覽最大長度" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Row="5" Grid.Column="1" />
Expand Down
58 changes: 31 additions & 27 deletions ConvertZZ/Window_Setting.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Window_Setting()
{
LoadSetting();
DataContext = this;
InitializeComponent();
InitializeComponent();
}
private void LoadSetting()
{
Expand All @@ -26,6 +26,7 @@ private void LoadSetting()
_PromptEnable = App.Settings.Prompt;
_RecognitionEncodingEnable = App.Settings.RecognitionEncoding;
_MaxPriviewLength = App.Settings.MaxLengthPreview.ToString();
_CheckVersion = App.Settings.CheckVersion;

_DefaultPath = App.Settings.FileConvert.DefaultPath;
_FixLabel = App.Settings.FileConvert.FixLabel;
Expand Down Expand Up @@ -71,6 +72,7 @@ private void SaveSetting()
App.Settings.Prompt = PromptEnable;
App.Settings.RecognitionEncoding = RecognitionEncodingEnable;
App.Settings.MaxLengthPreview = int.Parse(MaxPriviewLength);
App.Settings.CheckVersion = CheckVersion;

App.Settings.FileConvert.DefaultPath = DefaultPath;
App.Settings.FileConvert.FixLabel = FixLabel;
Expand All @@ -93,7 +95,7 @@ private void SaveSetting()
App.Settings.HotKey.AutoCopy = AutoCopy;
App.Settings.HotKey.AutoPaste = AutoPaste;
App.Settings.HotKey.Feature1.Enable = ShortCut1_IsActived;
App.Settings.HotKey.Feature1.Action =ShortCut1_Action.Value;
App.Settings.HotKey.Feature1.Action = ShortCut1_Action.Value;
App.Settings.HotKey.Feature1.Key = ShortCut1_Key;
App.Settings.HotKey.Feature1.Modift = ShortCut1_ModifyKey;
App.Settings.HotKey.Feature2.Enable = ShortCut2_IsActived;
Expand All @@ -114,29 +116,30 @@ private void SaveSetting()
}
public static Dictionary<string, string> Action { get; } = new Dictionary<string, string>()
{
{ "無" , "0" } ,
{ "隱藏/顯示懸浮球" , "1" } ,
{ "GBK>Big5" , "a1" } ,
{ "Big5>GBK" , "a2" } ,
{ "Unicod簡>Unicode繁" , "a3" } ,
{ "Unicode繁>Unicode簡" , "a4" } ,
{ "Unicode>Html Code十進制" , "za1" } ,
{ "Unicode>Html Code十六進制" , "za2" } ,
{ "HTML Code>Unicode" , "za3" } ,
{ "Unicode>GBK" , "zb1" } ,
{ "Unicode>Big5" , "zb2" } ,
{ "Unicode>Shift-JIS" , "zb3" } ,
{ "Shift-JIS>GBK" , "zc1" } ,
{ "Shift-JIS>Big5" , "zc2" } ,
{ "GBK>Shift-JIS" , "zc3" } ,
{ "Big5>Shift-JIS" , "zc4" } ,
{ "HZ>GBK" , "zd1" } ,
{ "HZ>Big5" , "zd1" } ,
{ "GBK>HZ" , "zd1" } ,
{ "無" , "0" } ,
{ "隱藏/顯示懸浮球" , "1" } ,
{ "GBK>Big5" , "a1" } ,
{ "Big5>GBK" , "a2" } ,
{ "Unicod簡>Unicode繁" , "a3" } ,
{ "Unicode繁>Unicode簡" , "a4" } ,
{ "Unicode>Html Code十進制" , "za1" } ,
{ "Unicode>Html Code十六進制" , "za2" } ,
{ "HTML Code>Unicode" , "za3" } ,
{ "Unicode>GBK" , "zb1" } ,
{ "Unicode>Big5" , "zb2" } ,
{ "Unicode>Shift-JIS" , "zb3" } ,
{ "Shift-JIS>GBK" , "zc1" } ,
{ "Shift-JIS>Big5" , "zc2" } ,
{ "GBK>Shift-JIS" , "zc3" } ,
{ "Big5>Shift-JIS" , "zc4" } ,
{ "HZ>GBK" , "zd1" } ,
{ "HZ>Big5" , "zd1" } ,
{ "GBK>HZ" , "zd1" } ,
{ "Big5>HZ" , "zd1" }
};
private bool _AssistiveTouchEnable, _VocabularyCorrenctionEnable, _PromptEnable, _RecognitionEncodingEnable;
private string _MaxPriviewLength;
private bool _CheckVersion;
private KeyValuePair<string, string> _Quick_L1, _Quick_L2, _Quick_L3, _Quick_L4, _Quick_L5, _Quick_L6;
private KeyValuePair<string, string> _Quick_R1, _Quick_R2, _Quick_R3, _Quick_R4, _Quick_R5, _Quick_R6;
private bool _UnicodeAddBom;
Expand All @@ -153,26 +156,27 @@ private void SaveSetting()
public bool RecognitionEncodingEnable { get => _RecognitionEncodingEnable; set { _RecognitionEncodingEnable = value; OnPropertyChanged(); SaveSetting(); } }

public string MaxPriviewLength { get => _MaxPriviewLength; set { _MaxPriviewLength = value; OnPropertyChanged(); SaveSetting(); } }
public bool CheckVersion { get => _CheckVersion; set { _CheckVersion = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_L1 { get => _Quick_L1; set { _Quick_L1 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_L2 { get => _Quick_L2; set { _Quick_L2 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_L3 { get => _Quick_L3; set { _Quick_L3 = value; OnPropertyChanged(); SaveSetting(); } }

private void TextBox_ShortCut_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
TextBox textbox = sender as TextBox;
textbox.Text = (e.Key == Key.System ? e.SystemKey : e.Key== Key.ImeProcessed?e.ImeProcessedKey:e.Key).ToString();
TextBox textbox = sender as TextBox;
textbox.Text = (e.Key == Key.System ? e.SystemKey : e.Key == Key.ImeProcessed ? e.ImeProcessedKey : e.Key).ToString();
}
private void TextBox_ShortCut_Modify_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
TextBox textbox = sender as TextBox;
textbox.Text = e.KeyboardDevice.Modifiers.ToString();
textbox.Text = e.KeyboardDevice.Modifiers.ToString();
}
public KeyValuePair<string, string> Quick_L4 { get => _Quick_L4; set { _Quick_L4 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_L5 { get => _Quick_L5; set { _Quick_L5 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_L6 { get => _Quick_L6; set { _Quick_L6 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_R1 { get => _Quick_R1; set { _Quick_R1 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_R1 { get => _Quick_R1; set { _Quick_R1 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_R2 { get => _Quick_R2; set { _Quick_R2 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_R3 { get => _Quick_R3; set { _Quick_R3 = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> Quick_R4 { get => _Quick_R4; set { _Quick_R4 = value; OnPropertyChanged(); SaveSetting(); } }
Expand Down Expand Up @@ -200,9 +204,9 @@ private void TextBox_ShortCut_Modify_PreviewKeyDown(object sender, KeyEventArgs
public bool ShortCut4_IsActived { get => _ShortCut4_IsActived; set { _ShortCut4_IsActived = value; OnPropertyChanged(); SaveSetting(); } }

public KeyValuePair<string, string> ShortCut1_Action { get => _ShortCut1_Action; set { _ShortCut1_Action = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> ShortCut2_Action { get => _ShortCut2_Action; set {_ShortCut2_Action = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> ShortCut2_Action { get => _ShortCut2_Action; set { _ShortCut2_Action = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> ShortCut3_Action { get => _ShortCut3_Action; set { _ShortCut3_Action = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> ShortCut4_Action { get => _ShortCut4_Action; set{ _ShortCut4_Action = value; OnPropertyChanged(); SaveSetting(); } }
public KeyValuePair<string, string> ShortCut4_Action { get => _ShortCut4_Action; set { _ShortCut4_Action = value; OnPropertyChanged(); SaveSetting(); } }

public bool UnicodeAddBom { get => _UnicodeAddBom; set { _UnicodeAddBom = value; OnPropertyChanged(); SaveSetting(); } }

Expand Down

0 comments on commit cca50be

Please sign in to comment.