1111using System . Threading . Tasks ;
1212using System . Windows . Data ;
1313using System . Windows . Input ;
14+ using System . Windows . Threading ;
1415using Windows . Devices . WiFi ;
1516
1617namespace NETworkManager . ViewModels
@@ -19,6 +20,8 @@ public class WiFiViewModel : ViewModelBase
1920 {
2021 #region Variables
2122 private readonly bool _isLoading ;
23+ private readonly DispatcherTimer _autoRefreshTimer = new DispatcherTimer ( ) ;
24+ private bool _isTimerPaused ;
2225
2326 private bool _isAdaptersLoading ;
2427 public bool IsAdaptersLoading
@@ -84,6 +87,56 @@ public bool IsNetworksLoading
8487 }
8588 }
8689
90+ private bool _autoRefresh ;
91+ public bool AutoRefresh
92+ {
93+ get => _autoRefresh ;
94+ set
95+ {
96+ if ( value == _autoRefresh )
97+ return ;
98+
99+ if ( ! _isLoading )
100+ SettingsManager . Current . WiFi_AutoRefresh = value ;
101+
102+ _autoRefresh = value ;
103+
104+ // Start timer to refresh automatically
105+ if ( ! _isLoading )
106+ {
107+ if ( value )
108+ StartAutoRefreshTimer ( ) ;
109+ else
110+ StopAutoRefreshTimer ( ) ;
111+ }
112+
113+ OnPropertyChanged ( ) ;
114+ }
115+ }
116+
117+ public ICollectionView AutoRefreshTimes { get ; }
118+
119+ private AutoRefreshTimeInfo _selectedAutoRefreshTime ;
120+ public AutoRefreshTimeInfo SelectedAutoRefreshTime
121+ {
122+ get => _selectedAutoRefreshTime ;
123+ set
124+ {
125+ if ( value == _selectedAutoRefreshTime )
126+ return ;
127+
128+ if ( ! _isLoading )
129+ SettingsManager . Current . WiFi_AutoRefreshTime = value ;
130+
131+ _selectedAutoRefreshTime = value ;
132+
133+ if ( AutoRefresh )
134+ ChangeAutoRefreshTimerInterval ( AutoRefreshTime . CalculateTimeSpan ( value ) ) ;
135+
136+ OnPropertyChanged ( ) ;
137+ }
138+ }
139+
87140 private string _search ;
88141 public string Search
89142 {
@@ -101,7 +154,6 @@ public string Search
101154 }
102155 }
103156
104-
105157 private bool _show2dot4GHzNetworks ;
106158 public bool Show2dot4GHzNetworks
107159 {
@@ -210,6 +262,11 @@ public WiFiViewModel()
210262 }
211263 } ;
212264
265+ AutoRefreshTimes = CollectionViewSource . GetDefaultView ( AutoRefreshTime . Defaults ) ;
266+ SelectedAutoRefreshTime = AutoRefreshTimes . SourceCollection . Cast < AutoRefreshTimeInfo > ( ) . FirstOrDefault ( x => ( x . Value == SettingsManager . Current . WiFi_AutoRefreshTime . Value && x . TimeUnit == SettingsManager . Current . WiFi_AutoRefreshTime . TimeUnit ) ) ;
267+
268+ _autoRefreshTimer . Tick += AutoRefreshTimer_Tick ;
269+
213270 LoadSettings ( ) ;
214271
215272 LoadAdapters ( ) ;
@@ -238,9 +295,9 @@ private void ReloadAdapterAction()
238295
239296 private bool ScanNetworks_CanExecute ( object obj ) => ! IsAdaptersLoading && ! IsNetworksLoading ;
240297
241- private void ScanNetworksAction ( )
298+ private async void ScanNetworksAction ( )
242299 {
243- ScanNetworks ( SelectedAdapter . WiFiAdapter ) ;
300+ await ScanNetworks ( SelectedAdapter . WiFiAdapter ) ;
244301 }
245302 #endregion
246303
@@ -276,15 +333,12 @@ private async void ReloadAdapter()
276333 Adapters = await WiFi . GetAdapterAsync ( ) ;
277334
278335 if ( Adapters . Count > 0 )
279- {
280- // Change interface...
281336 SelectedAdapter = string . IsNullOrEmpty ( id ) ? Adapters . FirstOrDefault ( ) : Adapters . FirstOrDefault ( x => x . NetworkInterfaceInfo . Id == id ) ;
282- }
283337
284338 IsAdaptersLoading = false ;
285339 }
286340
287- private async void ScanNetworks ( WiFiAdapter adapter )
341+ private async Task ScanNetworks ( WiFiAdapter adapter )
288342 {
289343 IsNetworksLoading = true ;
290344
@@ -357,21 +411,67 @@ private void AddNetworkToRadio2Chart(WiFiNetworkInfo network)
357411 } ) ;
358412 }
359413
360- public void OnViewVisible ( )
414+ private void ChangeAutoRefreshTimerInterval ( TimeSpan timeSpan )
415+ {
416+ _autoRefreshTimer . Interval = timeSpan ;
417+ }
418+
419+ private void StartAutoRefreshTimer ( )
361420 {
421+ ChangeAutoRefreshTimerInterval ( AutoRefreshTime . CalculateTimeSpan ( SelectedAutoRefreshTime ) ) ;
362422
423+ _autoRefreshTimer . Start ( ) ;
363424 }
364425
365- public void OnViewHide ( )
426+ private void StopAutoRefreshTimer ( )
427+ {
428+ _autoRefreshTimer . Stop ( ) ;
429+ }
430+
431+ private void PauseAutoRefreshTimer ( )
432+ {
433+ if ( ! _autoRefreshTimer . IsEnabled )
434+ return ;
435+
436+ _autoRefreshTimer . Stop ( ) ;
437+ _isTimerPaused = true ;
438+ }
439+
440+ private void ResumeAutoRefreshTimer ( )
366441 {
442+ if ( ! _isTimerPaused )
443+ return ;
444+
445+ _autoRefreshTimer . Start ( ) ;
446+ _isTimerPaused = false ;
447+ }
367448
449+ public void OnViewVisible ( )
450+ {
451+ ResumeAutoRefreshTimer ( ) ;
368452 }
453+
454+ public void OnViewHide ( )
455+ {
456+ PauseAutoRefreshTimer ( ) ;
457+ }
458+
369459 #endregion
370460
371461 #region Events
372- private void SettingsManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
462+ private async void AutoRefreshTimer_Tick ( object sender , EventArgs e )
373463 {
464+ // Stop timer...
465+ _autoRefreshTimer . Stop ( ) ;
466+
467+ // Scan networks
468+ await ScanNetworks ( SelectedAdapter . WiFiAdapter ) ;
374469
470+ // Restart timer...
471+ _autoRefreshTimer . Start ( ) ;
472+ }
473+ private void SettingsManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
474+ {
375475 }
376476 #endregion
377477 }
0 commit comments