Skip to content

Commit 188f58c

Browse files
committed
add: 有新版本就显示个小红点
1 parent ca77e02 commit 188f58c

File tree

5 files changed

+120
-44
lines changed

5 files changed

+120
-44
lines changed

llcomNext/LLCOM/Services/Utils.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia;
66
using Avalonia.Controls;
77
using Avalonia.Controls.ApplicationLifetimes;
8+
using RestSharp;
89

910
namespace LLCOM.Services;
1011

@@ -97,4 +98,54 @@ public static void OpenWebLink(string url)
9798
// ignored
9899
}
99100
}
101+
102+
private static string? _updateUrl = null;
103+
/// <summary>
104+
/// 检查更新
105+
/// </summary>
106+
/// <returns>是否有新版本,有的话返回下载地址</returns>
107+
public static async Task<string?> CheckUpdate()
108+
{
109+
if(_updateUrl != null)
110+
return _updateUrl;
111+
112+
var versionNow = Services.Utils.Version;
113+
//获取https://api.github.com/repos/chenxuuu/llcom/releases/latest的json结果
114+
var client = new RestClient("https://api.github.com/repos/chenxuuu/llcom/releases/latest");
115+
var request = new RestRequest();
116+
var response = await client.ExecuteAsync(request);
117+
if (!response.IsSuccessful || response.Content is null)
118+
return null;
119+
120+
var json = System.Text.Json.JsonDocument.Parse(response.Content);
121+
var version = json.RootElement.GetProperty("tag_name").GetString();
122+
if(version == null)
123+
return null;
124+
125+
//判断在线版本是否新于本地版本,按点分割后比较
126+
var isBigger = true;
127+
if(version == versionNow)
128+
isBigger = false;
129+
else
130+
{
131+
var versionNowSplit = versionNow.Split('.');
132+
var versionSplit = version.Split('.');
133+
for (int i = 0; i < versionNowSplit.Length; i++)
134+
{
135+
if (int.Parse(versionNowSplit[i]) > int.Parse(versionSplit[i]))
136+
{
137+
isBigger = false;
138+
break;
139+
}
140+
}
141+
}
142+
143+
//更新信息
144+
if(isBigger)
145+
_updateUrl = json.RootElement.GetProperty("html_url").GetString();
146+
147+
return _updateUrl;
148+
}
149+
150+
public static bool HasUpdate() => _updateUrl != null;
100151
}

llcomNext/LLCOM/ViewModels/MainViewModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public MainViewModel(Func<Type, ViewModelBase> getService)
3939
_getService = getService;
4040
CurrentPage = _getService(typeof(DataPageViewModel));
4141
CurrentDataPage = _getService(typeof(PacketDataViewModel));
42+
43+
Task.Run(async () =>
44+
{
45+
//检查下是否有新版本吧
46+
if (await Services.Utils.CheckUpdate() != null)
47+
ShowSettingDot = true;
48+
});
4249
}
4350

4451
[RelayCommand]
@@ -53,7 +60,10 @@ public MainViewModel(Func<Type, ViewModelBase> getService)
5360
private void LogPageButton() => CurrentPage = _getService(typeof(LogPageViewModel));
5461
[RelayCommand]
5562
private void SettingPageButton() => CurrentPage = _getService(typeof(SettingPageViewModel));
56-
63+
64+
65+
[ObservableProperty]
66+
private bool _showSettingDot = false;
5767

5868
//以下代码用于切换DataPage中的子页面
5969
[ObservableProperty]
@@ -71,4 +81,5 @@ public MainViewModel(Func<Type, ViewModelBase> getService)
7181
[RelayCommand]
7282
private void WaveformButton() => CurrentDataPage = _getService(typeof(WaveformViewModel));
7383

84+
7485
}

llcomNext/LLCOM/ViewModels/Pages/SettingPageViewModel.cs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ public SettingPageViewModel(Func<Type, ViewModelBase> getService)
1919
_getService = getService;
2020

2121
//初始化系统信息
22-
Task.Run(() =>
22+
Task.Run(async () =>
2323
{
24+
//是否已经检查过更新?
25+
if (Services.Utils.HasUpdate())
26+
{
27+
_updateUrl = await Services.Utils.CheckUpdate();
28+
HasCheckedUpdate = true;
29+
}
30+
2431
var packagesInfo = new List<string>();
2532
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
2633

@@ -44,11 +51,13 @@ public SettingPageViewModel(Func<Type, ViewModelBase> getService)
4451
$"Packages \n{string.Join("\n", packagesInfo)}\n" +
4552
$"Environment: {Environment.GetEnvironmentVariable("PATH")}";
4653
});
54+
4755
}
4856

4957

5058
#region About
51-
[ObservableProperty] private string _systemInfo = "Loading...";
59+
[ObservableProperty]
60+
private string _systemInfo = "Loading...";
5261

5362
[RelayCommand]
5463
private async Task CopySystemInfo()
@@ -76,40 +85,11 @@ private async Task CheckUpdate()
7685
return;
7786
}
7887

79-
var versionNow = Services.Utils.Version;
80-
//获取https://api.github.com/repos/chenxuuu/llcom/releases/latest的json结果
81-
var client = new RestClient("https://api.github.com/repos/chenxuuu/llcom/releases/latest");
82-
var request = new RestRequest();
83-
var response = await client.ExecuteAsync(request);
84-
if (!response.IsSuccessful || response.Content is null)
85-
return;
86-
87-
var json = System.Text.Json.JsonDocument.Parse(response.Content);
88-
var version = json.RootElement.GetProperty("tag_name").GetString();
89-
if(version == null)
90-
return;
91-
92-
//判断在线版本是否新于本地版本,按点分割后比较
93-
var isBigger = true;
94-
if(version == versionNow)
95-
isBigger = false;
96-
else
97-
{
98-
var versionNowSplit = versionNow.Split('.');
99-
var versionSplit = version.Split('.');
100-
for (int i = 0; i < versionNowSplit.Length; i++)
101-
{
102-
if (int.Parse(versionNowSplit[i]) > int.Parse(versionSplit[i]))
103-
{
104-
isBigger = false;
105-
break;
106-
}
107-
}
108-
}
88+
var url = await Services.Utils.CheckUpdate();
10989

11090
//更新信息
111-
if(isBigger)
112-
_updateUrl = json.RootElement.GetProperty("html_url").GetString();
91+
if(url != null)
92+
_updateUrl = url;
11393

11494
HasCheckedUpdate = true;
11595
}

llcomNext/LLCOM/Views/MainView.axaml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,27 @@
5252
Content="&#xE94E;"
5353
ToolTip.Tip="历史日志" />
5454
</StackPanel>
55-
<Button
56-
VerticalAlignment="Bottom"
57-
Classes="IconButton Tertiary"
58-
Classes.IconButtonActive="{Binding IsSettingPageActive}"
59-
Command="{Binding SettingPageButtonCommand}"
60-
Content="&#xE270;"
61-
ToolTip.Tip="软件设置" />
55+
<Panel VerticalAlignment="Bottom">
56+
<Button
57+
Classes="IconButton Tertiary"
58+
Classes.IconButtonActive="{Binding IsSettingPageActive}"
59+
Command="{Binding SettingPageButtonCommand}"
60+
ToolTip.Tip="软件设置">
61+
<Panel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
62+
<TextBlock
63+
HorizontalAlignment="Center"
64+
VerticalAlignment="Center"
65+
Text="&#xE270;" />
66+
<u:Badge
67+
HorizontalAlignment="Right"
68+
VerticalAlignment="Top"
69+
Classes="Danger"
70+
Dot="True"
71+
IsVisible="{Binding ShowSettingDot}" />
72+
</Panel>
73+
</Button>
74+
75+
</Panel>
6276
</Panel>
6377
</Border>
6478
<UserControl Grid.Column="1" Content="{Binding CurrentPage}" />

llcomNext/LLCOM/Views/Pages/SettingPageView.axaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:u="https://irihi.tech/ursa"
78
xmlns:vm="using:LLCOM.ViewModels"
89
d:DesignHeight="800"
910
d:DesignWidth="381"
@@ -23,7 +24,19 @@
2324
</TabItem>
2425
<TabItem Header="外观与字体" />
2526
<TabItem Header="日志与缓存" />
26-
<TabItem Header="关于软件">
27+
<TabItem>
28+
<TabItem.Header>
29+
<Panel>
30+
<TextBlock>关于软件</TextBlock>
31+
<u:Badge
32+
Margin="0,0,-8,0"
33+
HorizontalAlignment="Right"
34+
VerticalAlignment="Top"
35+
Classes="Danger"
36+
Dot="True"
37+
IsVisible="{Binding HasNewVersion}" />
38+
</Panel>
39+
</TabItem.Header>
2740
<ScrollViewer>
2841
<StackPanel Margin="10">
2942
<Border
@@ -44,7 +57,14 @@
4457
FontSize="15"
4558
Text="多功能调试工具" />
4659

47-
<Panel Margin="0,10,0,0">
60+
<Panel Margin="0,10,0,0" HorizontalAlignment="Center">
61+
<u:Badge
62+
Margin="0,-8,-20,0"
63+
HorizontalAlignment="Right"
64+
VerticalAlignment="Top"
65+
Classes="Danger"
66+
Header="NEW"
67+
IsVisible="{Binding HasNewVersion}" />
4868
<Button Name="CheckUpdateButton" Command="{Binding CheckUpdateCommand}">
4969
<Panel>
5070
<TextBlock HorizontalAlignment="Center" IsVisible="{Binding !HasCheckedUpdate}">检查更新</TextBlock>

0 commit comments

Comments
 (0)