@@ -10,13 +10,14 @@ public partial class MainWindowViewModel : ViewModelBase
1010 [ ObservableProperty ] private DownloadStatistics _statistics ;
1111 [ ObservableProperty ] private bool _isCompleted ;
1212 private readonly DispatcherTimer _timer ;
13+ private readonly Random _random ;
1314
1415 public MainWindowViewModel ( )
1516 {
1617 Statistics = new DownloadStatistics
1718 {
1819 Version = "1.0.0" ,
19- Speed = "1MB /s" ,
20+ Speed = "0MB /s" ,
2021 Remaining = TimeSpan . Zero ,
2122 TotalBytesToReceive = 1024 * 1024 * 10 ,
2223 BytesReceived = 1024 * 1024 * 0 ,
@@ -29,22 +30,36 @@ public MainWindowViewModel()
2930 } ;
3031 _timer . Tick += UpdateProgress ;
3132 _timer . Start ( ) ;
33+
34+ _random = new Random ( ) ;
3235 }
3336
3437 private void UpdateProgress ( object ? sender , EventArgs e )
3538 {
36- if ( Statistics . BytesReceived < Statistics . TotalBytesToReceive )
39+ var received = Statistics . BytesReceived ;
40+ var total = Statistics . TotalBytesToReceive ;
41+ if ( received < total )
3742 {
38- Statistics . BytesReceived += 1024 * 1024 ;
39- Statistics . ProgressPercentage =
40- ( double ) Statistics . BytesReceived / Statistics . TotalBytesToReceive * 100 ;
43+ var increment = _random . Next ( 1024 * 1024 * 2 , 1024 * 1024 * 3 ) ;
44+ received += increment ;
45+
46+ if ( received > total )
47+ {
48+ received = total ;
49+ }
50+
51+ Statistics . BytesReceived = received ;
52+ Statistics . ProgressPercentage = ( double ) received / total * 100 ;
53+
54+ var currentSpeed = increment / _timer . Interval . TotalSeconds ;
55+ Statistics . Speed = $ "{ currentSpeed / 1024 / 1024 : F1} MB/s";
4156
42- Statistics . Remaining = TimeSpan . FromSeconds (
43- ( Statistics . TotalBytesToReceive - Statistics . BytesReceived ) / ( 1024 * 1024 ) ) ;
57+ var remainingBytes = total - received ;
58+ Statistics . Remaining = TimeSpan . FromSeconds ( remainingBytes / currentSpeed ) ;
4459
4560 OnPropertyChanged ( nameof ( Statistics ) ) ;
4661
47- if ( Statistics . BytesReceived >= Statistics . TotalBytesToReceive )
62+ if ( received >= total )
4863 {
4964 _timer . Stop ( ) ;
5065 IsCompleted = true ;
0 commit comments