1- using System . ComponentModel ;
1+ using NETworkManager . Models . Network ;
2+ using NETworkManager . Models . Settings ;
3+ using NETworkManager . Utilities ;
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . ComponentModel ;
7+ using System . Linq ;
8+ using System . Threading . Tasks ;
9+ using System . Windows . Input ;
210
311namespace NETworkManager . ViewModels
412{
513 public class WiFiViewModel : ViewModelBase
614 {
715 #region Variables
16+ private readonly bool _isLoading ;
817
18+ private bool _isWiFiAdaptersLoading ;
19+ public bool IsWiFiAdaptersLoading
20+ {
21+ get => _isWiFiAdaptersLoading ;
22+ set
23+ {
24+ if ( value == _isWiFiAdaptersLoading )
25+ return ;
26+
27+ _isWiFiAdaptersLoading = value ;
28+ OnPropertyChanged ( ) ;
29+ }
30+ }
31+
32+ private List < WiFiAdapterInfo > _wiFiAdapters ;
33+ public List < WiFiAdapterInfo > WiFiAdapters
34+ {
35+ get => _wiFiAdapters ;
36+ set
37+ {
38+ if ( value == _wiFiAdapters )
39+ return ;
40+
41+ _wiFiAdapters = value ;
42+ OnPropertyChanged ( ) ;
43+ }
44+ }
45+
46+ private WiFiAdapterInfo _selectedWiFiAdapters ;
47+ public WiFiAdapterInfo SelectedWiFiAdapter
48+ {
49+ get => _selectedWiFiAdapters ;
50+ set
51+ {
52+ if ( value == _selectedWiFiAdapters )
53+ return ;
54+
55+ if ( value != null )
56+ {
57+ //if (!_isLoading)
58+ // SettingsManager.Current.NetworkInterface_SelectedInterfaceId = value.Id;
59+ }
60+
61+ _selectedWiFiAdapters = value ;
62+ OnPropertyChanged ( ) ;
63+ }
64+ }
965 #endregion
1066
1167 #region Constructor, load settings
1268
1369 public WiFiViewModel ( )
1470 {
71+ _isLoading = true ;
72+
1573 LoadSettings ( ) ;
74+
75+ LoadAdapters ( ) ;
76+
77+ _isLoading = false ;
1678 }
1779
1880 private void LoadSettings ( )
@@ -22,11 +84,53 @@ private void LoadSettings()
2284 #endregion
2385
2486 #region ICommands & Actions
87+ public ICommand ReloadWiFiAdaptersCommand => new RelayCommand ( p => ReloadAdapterAction ( ) , ReloadWiFiAdapters_CanExecute ) ;
2588
89+ private bool ReloadWiFiAdapters_CanExecute ( object obj ) => ! IsWiFiAdaptersLoading ;
90+
91+ private void ReloadAdapterAction ( )
92+ {
93+ ReloadAdapter ( ) ;
94+ }
2695 #endregion
2796
2897 #region Methods
98+ private async void LoadAdapters ( )
99+ {
100+ IsWiFiAdaptersLoading = true ;
101+
102+ WiFiAdapters = await WiFi . GetWiFiAdapterAsync ( ) ;
103+
104+ // Get the last selected interface, if it is still available on this machine...
105+ if ( WiFiAdapters . Count > 0 )
106+ {
107+ var info = WiFiAdapters . FirstOrDefault ( s => s . Id . ToString ( ) == SettingsManager . Current . NetworkInterface_SelectedInterfaceId ) ;
108+
109+ SelectedWiFiAdapter = info ?? WiFiAdapters [ 0 ] ;
110+ }
111+
112+ IsWiFiAdaptersLoading = false ;
113+ }
114+
115+ private async void ReloadAdapter ( )
116+ {
117+ IsWiFiAdaptersLoading = true ;
118+
119+ await Task . Delay ( 2000 ) ; // Make the user happy, let him see a reload animation (and he cannot spam the reload command)
120+
121+ Guid id = Guid . Empty ;
122+
123+ if ( SelectedWiFiAdapter != null )
124+ id = SelectedWiFiAdapter . Id ;
29125
126+ WiFiAdapters = await WiFi . GetWiFiAdapterAsync ( ) ;
127+
128+ // Change interface...
129+ SelectedWiFiAdapter = id == Guid . Empty ? WiFiAdapters . FirstOrDefault ( ) : WiFiAdapters . FirstOrDefault ( x => x . Id == id ) ;
130+
131+ IsWiFiAdaptersLoading = false ;
132+ }
133+
30134 public void OnViewVisible ( )
31135 {
32136
@@ -41,7 +145,7 @@ public void OnViewHide()
41145 #region Events
42146 private void SettingsManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
43147 {
44-
148+
45149 }
46150 #endregion
47151 }
0 commit comments