@@ -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}
0 commit comments