1- using System ;
2- using System . ComponentModel ;
3- using Avalonia . Threading ;
1+ using Client . Avalonia . Models ;
2+ using Client . Avalonia . Services ;
43using CommunityToolkit . Mvvm . ComponentModel ;
54using CommunityToolkit . Mvvm . Input ;
65
76namespace Client . Avalonia . ViewModels ;
87
98public partial class MainWindowViewModel : ViewModelBase
109{
10+ private readonly IDownloadService _downloadService ;
11+
1112 [ ObservableProperty ] private DownloadStatistics _statistics ;
1213
1314 [ ObservableProperty ] [ NotifyCanExecuteChangedFor ( nameof ( StartCommand ) , nameof ( StopCommand ) ) ]
1415 private DownloadStatus _status ;
1516
16- private readonly DispatcherTimer _timer ;
17- private readonly Random _random ;
18-
19- public MainWindowViewModel ( )
17+ public MainWindowViewModel ( IDownloadService downloadService )
2018 {
21- Statistics = new DownloadStatistics
22- {
23- Version = "11.2.2" ,
24- Speed = 0 ,
25- Remaining = TimeSpan . Zero ,
26- TotalBytesToReceive = 1024 * 1024 * 10 ,
27- BytesReceived = 1024 * 1024 * 0 ,
28- ProgressPercentage = 0
29- } ;
30- Status = DownloadStatus . NotStarted ;
19+ _downloadService = downloadService ;
20+ _downloadService . ProgressChanged += stats => Statistics = stats ;
21+ _downloadService . StatusChanged += status => Status = status ;
3122
32- _timer = new DispatcherTimer
33- {
34- Interval = TimeSpan . FromMilliseconds ( 1000 )
35- } ;
36- _timer . Tick += UpdateProgress ;
37-
38- _random = new Random ( ) ;
23+ Statistics = _downloadService . CurrentStatistics ;
24+ Status = _downloadService . Status ;
3925 }
4026
4127 #region Buttons
4228
4329 private bool CanStart => Status is DownloadStatus . NotStarted or DownloadStatus . Downloading or DownloadStatus . Paused ;
4430
4531 [ RelayCommand ( CanExecute = nameof ( CanStart ) ) ]
46- private void Start ( )
47- {
48- if ( ! _timer . IsEnabled )
49- {
50- _timer . Start ( ) ;
51- Status = DownloadStatus . Downloading ;
52- }
53- else
54- {
55- _timer . Stop ( ) ;
56- Status = DownloadStatus . Paused ;
57- Statistics . Speed = 0 ;
58- Statistics . Remaining = TimeSpan . Zero ;
59- }
60- }
32+ private void Start ( ) => _downloadService . Start ( ) ;
6133
6234 private bool CanStop => Status is DownloadStatus . Downloading or DownloadStatus . Paused ;
6335
6436 [ RelayCommand ( CanExecute = nameof ( CanStop ) ) ]
65- private void Stop ( )
66- {
67- _timer . Stop ( ) ;
68- Status = DownloadStatus . NotStarted ;
69- Statistics . BytesReceived = 0 ;
70- Statistics . ProgressPercentage = 0 ;
71- Statistics . Speed = 0 ;
72- Statistics . Remaining = TimeSpan . Zero ;
73- OnPropertyChanged ( nameof ( Statistics ) ) ;
74- }
37+ private void Stop ( ) => _downloadService . Stop ( ) ;
7538
7639 [ RelayCommand ]
77- private void Restart ( )
78- {
79- Stop ( ) ;
80- Start ( ) ;
81- }
40+ private void Restart ( ) => _downloadService . Restart ( ) ;
8241
8342 #endregion
84-
85-
86- private void UpdateProgress ( object ? sender , EventArgs e )
87- {
88- var received = Statistics . BytesReceived ;
89- var total = Statistics . TotalBytesToReceive ;
90- if ( received < total )
91- {
92- var increment = _random . Next ( 1024 * 1024 * 2 , 1024 * 1024 * 3 ) ;
93- received += increment ;
94-
95- if ( received > total )
96- {
97- received = total ;
98- }
99-
100- Statistics . BytesReceived = received ;
101- Statistics . ProgressPercentage = ( double ) received / total * 100 ;
102-
103- var currentSpeed = increment / _timer . Interval . TotalSeconds ;
104- Statistics . Speed = currentSpeed / 1024 / 1024 ;
105-
106- var remainingBytes = total - received ;
107- Statistics . Remaining = TimeSpan . FromSeconds ( remainingBytes / currentSpeed ) ;
108-
109- OnPropertyChanged ( nameof ( Statistics ) ) ;
110-
111- if ( received >= total )
112- {
113- _timer . Stop ( ) ;
114- Status = DownloadStatus . Completed ;
115- Statistics . Speed = 0 ;
116- Statistics . Remaining = TimeSpan . Zero ;
117- }
118- }
119- }
120- }
121-
122- public partial class DownloadStatistics : ObservableObject
123- {
124- [ ObservableProperty ] [ Description ( "当前下载版本" ) ]
125- private object ? _version ;
126-
127- [ ObservableProperty ] [ Description ( "下载速度" ) ]
128- private double _speed ;
129-
130- [ ObservableProperty ] [ Description ( "剩余下载时间" ) ]
131- private TimeSpan _remaining ;
132-
133- [ ObservableProperty ] [ Description ( "总大小" ) ] [ NotifyPropertyChangedFor ( nameof ( TotalBytesToReceiveInMB ) ) ]
134- private long _totalBytesToReceive ;
135-
136- [ ObservableProperty ] [ Description ( "已下载大小" ) ] [ NotifyPropertyChangedFor ( nameof ( BytesReceivedInMB ) ) ]
137- private long _bytesReceived ;
138-
139- [ ObservableProperty ] [ Description ( "进度百分比" ) ]
140- private double _progressPercentage ;
141-
142- public double BytesReceivedInMB => ( double ) BytesReceived / 1024 / 1024 ;
143- public double TotalBytesToReceiveInMB => ( double ) TotalBytesToReceive / 1024 / 1024 ;
144- }
145-
146- [ Flags ]
147- public enum DownloadStatus
148- {
149- [ Description ( "未开始" ) ] NotStarted ,
150- [ Description ( "下载中" ) ] Downloading ,
151- [ Description ( "已暂停" ) ] Paused ,
152- [ Description ( "已完成" ) ] Completed
15343}
0 commit comments