Skip to content

Commit 44f2c17

Browse files
committed
feat: add Status enum and Semi pic.
1 parent ab9f714 commit 44f2c17

File tree

4 files changed

+106
-25
lines changed

4 files changed

+106
-25
lines changed
Lines changed: 56 additions & 0 deletions
Loading

src/Client.Avalonia/Client.Avalonia.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
2222
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
2323
</PackageReference>
24+
<PackageReference Include="Avalonia.Svg" Version="11.2.0.2" />
2425
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2"/>
2526
<PackageReference Include="Semi.Avalonia" Version="11.2.1.1" />
2627
</ItemGroup>

src/Client.Avalonia/ViewModels/MainWindowViewModel.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ public partial class MainWindowViewModel : ViewModelBase
1010
{
1111
[ObservableProperty] private DownloadStatistics _statistics;
1212

13-
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(StartCommand))]
14-
private bool _isCompleted;
13+
[ObservableProperty]
14+
[NotifyCanExecuteChangedFor(nameof(StartCommand), nameof(StopCommand))]
15+
[NotifyPropertyChangedFor(nameof(IsDownloading), nameof(IsCompleted))]
16+
private DownloadStatus _status;
1517

16-
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(StartCommand), nameof(StopCommand))]
17-
private bool _isUpdating;
18+
public bool IsDownloading => Status is DownloadStatus.Downloading;
19+
public bool IsCompleted => Status is DownloadStatus.Completed;
1820

1921
private readonly DispatcherTimer _timer;
2022
private readonly Random _random;
@@ -24,12 +26,13 @@ public MainWindowViewModel()
2426
Statistics = new DownloadStatistics
2527
{
2628
Version = "11.2.2",
27-
Speed = "0MB/s",
29+
Speed = 0,
2830
Remaining = TimeSpan.Zero,
2931
TotalBytesToReceive = 1024 * 1024 * 10,
3032
BytesReceived = 1024 * 1024 * 0,
3133
ProgressPercentage = 0
3234
};
35+
Status = DownloadStatus.NotStarted;
3336

3437
_timer = new DispatcherTimer
3538
{
@@ -42,33 +45,36 @@ public MainWindowViewModel()
4245

4346
#region Buttons
4447

45-
private bool CanStart => !IsCompleted;
48+
private bool CanStart => Status is DownloadStatus.NotStarted or DownloadStatus.Downloading or DownloadStatus.Paused;
4649

4750
[RelayCommand(CanExecute = nameof(CanStart))]
4851
private void Start()
4952
{
5053
if (!_timer.IsEnabled)
5154
{
5255
_timer.Start();
53-
IsUpdating = true;
56+
Status = DownloadStatus.Downloading;
5457
}
5558
else
5659
{
5760
_timer.Stop();
58-
IsUpdating = false;
61+
Status = DownloadStatus.Paused;
62+
Statistics.Speed = 0;
63+
Statistics.Remaining = TimeSpan.Zero;
5964
}
6065
}
6166

62-
[RelayCommand(CanExecute = nameof(IsUpdating))]
67+
private bool CanStop => Status is DownloadStatus.Downloading or DownloadStatus.Paused;
68+
69+
[RelayCommand(CanExecute = nameof(CanStop))]
6370
private void Stop()
6471
{
6572
_timer.Stop();
73+
Status = DownloadStatus.NotStarted;
6674
Statistics.BytesReceived = 0;
6775
Statistics.ProgressPercentage = 0;
68-
Statistics.Speed = "0 MB/s";
76+
Statistics.Speed = 0;
6977
Statistics.Remaining = TimeSpan.Zero;
70-
IsCompleted = false;
71-
IsUpdating = false;
7278
OnPropertyChanged(nameof(Statistics));
7379
}
7480

@@ -100,7 +106,7 @@ private void UpdateProgress(object? sender, EventArgs e)
100106
Statistics.ProgressPercentage = (double)received / total * 100;
101107

102108
var currentSpeed = increment / _timer.Interval.TotalSeconds;
103-
Statistics.Speed = $"{currentSpeed / 1024 / 1024:F2} MB/s";
109+
Statistics.Speed = currentSpeed / 1024 / 1024;
104110

105111
var remainingBytes = total - received;
106112
Statistics.Remaining = TimeSpan.FromSeconds(remainingBytes / currentSpeed);
@@ -110,8 +116,9 @@ private void UpdateProgress(object? sender, EventArgs e)
110116
if (received >= total)
111117
{
112118
_timer.Stop();
113-
IsUpdating = false;
114-
IsCompleted = true;
119+
Status = DownloadStatus.Completed;
120+
Statistics.Speed = 0;
121+
Statistics.Remaining = TimeSpan.Zero;
115122
}
116123
}
117124
}
@@ -123,7 +130,7 @@ [ObservableProperty] [Description("当前下载版本")]
123130
private object? _version;
124131

125132
[ObservableProperty] [Description("下载速度")]
126-
private string? _speed;
133+
private double _speed;
127134

128135
[ObservableProperty] [Description("剩余下载时间")]
129136
private TimeSpan _remaining;
@@ -139,4 +146,13 @@ [ObservableProperty] [Description("进度百分比")]
139146

140147
public double BytesReceivedInMB => (double)BytesReceived / 1024 / 1024;
141148
public double TotalBytesToReceiveInMB => (double)TotalBytesToReceive / 1024 / 1024;
149+
}
150+
151+
[Flags]
152+
public enum DownloadStatus
153+
{
154+
NotStarted,
155+
Downloading,
156+
Paused,
157+
Completed
142158
}

src/Client.Avalonia/Views/MainWindow.axaml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Icon="/Assets/avalonia-logo.ico"
1010
RequestedThemeVariant="Dark"
1111
Width="800" Height="450"
12-
Background="{DynamicResource SemiBackground3Color}"
1312
Title="{Binding Statistics.Version,StringFormat='General Update {0} 版本更新'}">
1413
<Window.Resources>
1514
<ResourceDictionary>
@@ -27,9 +26,18 @@
2726
<vm:MainWindowViewModel />
2827
</Design.DataContext>
2928
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10" MinWidth="550">
30-
<Image
31-
Margin="30"
32-
Source="../Assets/GeneralUpdate.png" Width="150" Height="150" />
29+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
30+
<Image
31+
Margin="30"
32+
Width="150"
33+
Height="150"
34+
Source="/Assets/GeneralUpdate.png" />
35+
<Image
36+
Margin="30"
37+
Width="150"
38+
Height="150"
39+
Source="{SvgImage /Assets/Semi.svg}" />
40+
</StackPanel>
3341

3442
<SelectableTextBlock
3543
HorizontalAlignment="Center"
@@ -43,7 +51,7 @@
4351
<Panel>
4452
<SelectableTextBlock
4553
HorizontalAlignment="Left"
46-
Text="{Binding Statistics.Speed, StringFormat='下载速度:{0}'}" />
54+
Text="{Binding Statistics.Speed, StringFormat='下载速度:{0:F2} MB/s'}" />
4755
<SelectableTextBlock
4856
HorizontalAlignment="Center">
4957
<Run Text="已下载:" />
@@ -75,14 +83,14 @@
7583
</ProgressBar>
7684
<Border
7785
Padding="10"
78-
Background="{DynamicResource SemiBackground3Color}"
86+
Background="{DynamicResource SemiBackground0Color}"
7987
Theme="{StaticResource CardBorder}">
8088
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Spacing="10">
8189
<Button
8290
Name="StopButton"
8391
Padding="8"
8492
Theme="{StaticResource BorderlessButton}"
85-
Classes="Tertiary"
93+
Classes="Danger"
8694
ToolTip.Tip="停止"
8795
Command="{Binding StopCommand}">
8896
<PathIcon
@@ -92,7 +100,7 @@
92100
<ToggleSwitch
93101
Name="StartButton"
94102
Padding="8"
95-
IsChecked="{Binding IsUpdating}"
103+
IsChecked="{Binding IsDownloading}"
96104
Theme="{StaticResource ButtonToggleSwitch}"
97105
Command="{Binding StartCommand}">
98106
<ToolTip.Tip>
@@ -117,7 +125,7 @@
117125
Padding="8"
118126
Theme="{StaticResource BorderlessButton}"
119127
Classes="Tertiary"
120-
ToolTip.Tip="重新开始"
128+
ToolTip.Tip="重新下载"
121129
Command="{Binding RestartCommand}">
122130
<PathIcon
123131
Theme="{StaticResource InnerPathIcon}"

0 commit comments

Comments
 (0)