29
29
30
30
namespace NETworkManager . ViewModels ;
31
31
32
- public class IPScannerViewModel : ViewModelBase , IProfileManagerMinimal
32
+ public class IPScannerViewModel : ViewModelApplicationBase , IProfileManagerMinimal
33
33
{
34
34
#region Variables
35
35
private readonly IDialogCoordinator _dialogCoordinator ;
@@ -39,8 +39,6 @@ public class IPScannerViewModel : ViewModelBase, IProfileManagerMinimal
39
39
private readonly int _tabId ;
40
40
private bool _firstLoad = true ;
41
41
42
- private readonly bool _isLoading ;
43
-
44
42
private string _hosts ;
45
43
public string Hosts
46
44
{
@@ -72,16 +70,16 @@ public bool IsSubnetDetectionRunning
72
70
}
73
71
74
72
75
- private bool _isScanRunning ;
76
- public bool IsScanRunning
73
+ private bool _isRunning ;
74
+ public bool IsRunning
77
75
{
78
- get => _isScanRunning ;
76
+ get => _isRunning ;
79
77
set
80
78
{
81
- if ( value == _isScanRunning )
79
+ if ( value == _isRunning )
82
80
return ;
83
81
84
- _isScanRunning = value ;
82
+ _isRunning = value ;
85
83
OnPropertyChanged ( ) ;
86
84
}
87
85
}
@@ -106,7 +104,7 @@ public ObservableCollection<IPScannerHostInfo> Results
106
104
get => _results ;
107
105
set
108
106
{
109
- if ( value != null && value == _results )
107
+ if ( Equals ( value , _results ) )
110
108
return ;
111
109
112
110
_results = value ;
@@ -143,12 +141,6 @@ public IList SelectedResults
143
141
}
144
142
}
145
143
146
- public bool ResolveHostname => SettingsManager . Current . IPScanner_ResolveHostname ;
147
-
148
- public bool PortScanEnabled => SettingsManager . Current . IPScanner_PortScanEnabled ;
149
-
150
- public bool ResolveMACAddress => SettingsManager . Current . IPScanner_ResolveMACAddress ;
151
-
152
144
private int _hostsToScan ;
153
145
public int HostsToScan
154
146
{
@@ -209,7 +201,7 @@ public bool IsStatusMessageDisplayed
209
201
public string StatusMessage
210
202
{
211
203
get => _statusMessage ;
212
- set
204
+ private set
213
205
{
214
206
if ( value == _statusMessage )
215
207
return ;
@@ -219,14 +211,12 @@ public string StatusMessage
219
211
}
220
212
}
221
213
222
- public IEnumerable < CustomCommandInfo > CustomCommands => SettingsManager . Current . IPScanner_CustomCommands ;
214
+ public static IEnumerable < CustomCommandInfo > CustomCommands => SettingsManager . Current . IPScanner_CustomCommands ;
223
215
#endregion
224
216
225
217
#region Constructor, load settings, shutdown
226
218
public IPScannerViewModel ( IDialogCoordinator instance , int tabId , string hostOrIPRange )
227
219
{
228
- _isLoading = true ;
229
-
230
220
_dialogCoordinator = instance ;
231
221
232
222
_tabId = tabId ;
@@ -240,11 +230,6 @@ public IPScannerViewModel(IDialogCoordinator instance, int tabId, string hostOrI
240
230
ResultsView . SortDescriptions . Add ( new SortDescription ( nameof ( IPScannerHostInfo . PingInfo ) + "." + nameof ( PingInfo . IPAddressInt32 ) , ListSortDirection . Ascending ) ) ;
241
231
242
232
LoadSettings ( ) ;
243
-
244
- // Detect if settings have changed...
245
- SettingsManager . Current . PropertyChanged += SettingsManager_PropertyChanged ;
246
-
247
- _isLoading = false ;
248
233
}
249
234
250
235
public void OnLoaded ( )
@@ -253,7 +238,7 @@ public void OnLoaded()
253
238
return ;
254
239
255
240
if ( ! string . IsNullOrEmpty ( Hosts ) )
256
- StartScan ( ) ;
241
+ StartScan ( ) . ConfigureAwait ( false ) ;
257
242
258
243
_firstLoad = false ;
259
244
}
@@ -282,7 +267,7 @@ private void ScanAction()
282
267
283
268
private void DetectSubnetAction ( )
284
269
{
285
- DetectIPRange ( ) ;
270
+ DetectIPRange ( ) . ConfigureAwait ( false ) ;
286
271
}
287
272
288
273
public ICommand RedirectDataToApplicationCommand => new RelayCommand ( RedirectDataToApplicationAction ) ;
@@ -317,7 +302,7 @@ private void PerformDNSLookupHostnameAction()
317
302
318
303
private void CustomCommandAction ( object guid )
319
304
{
320
- CustomCommand ( guid ) ;
305
+ CustomCommand ( guid ) . ConfigureAwait ( false ) ;
321
306
}
322
307
323
308
public ICommand AddProfileSelectedHostCommand => new RelayCommand ( _ => AddProfileSelectedHostAction ( ) ) ;
@@ -336,55 +321,6 @@ private async void AddProfileSelectedHostAction()
336
321
await ProfileDialogManager . ShowAddProfileDialog ( this , _dialogCoordinator , profileInfo ) ;
337
322
}
338
323
339
- public ICommand CopySelectedStatusCommand => new RelayCommand ( _ => CopySelectedStatusAction ( ) ) ;
340
-
341
- private void CopySelectedStatusAction ( )
342
- {
343
- ClipboardHelper . SetClipboard ( SelectedResult . IsReachable . ToString ( ) ) ;
344
- }
345
-
346
- public ICommand CopySelectedIPAddressCommand => new RelayCommand ( _ => CopySelectedIPAddressAction ( ) ) ;
347
-
348
- private void CopySelectedIPAddressAction ( )
349
- {
350
- ClipboardHelper . SetClipboard ( SelectedResult . PingInfo . IPAddress . ToString ( ) ) ;
351
- }
352
-
353
- public ICommand CopySelectedHostnameCommand => new RelayCommand ( _ => CopySelectedHostnameAction ( ) ) ;
354
-
355
- private void CopySelectedHostnameAction ( )
356
- {
357
- ClipboardHelper . SetClipboard ( SelectedResult . Hostname ) ;
358
- }
359
-
360
- public ICommand CopySelectedPortStatusCommand => new RelayCommand ( _ => CopySelectedPortStatusAction ( ) ) ;
361
-
362
- private void CopySelectedPortStatusAction ( )
363
- {
364
- ClipboardHelper . SetClipboard ( ResourceTranslator . Translate ( ResourceIdentifier . PortState , SelectedResult . IsAnyPortOpen ? PortState . Open : PortState . Closed ) ) ;
365
- }
366
-
367
- public ICommand CopySelectedPingStatusCommand => new RelayCommand ( _ => CopySelectedPingStatusAction ( ) ) ;
368
-
369
- private void CopySelectedPingStatusAction ( )
370
- {
371
- ClipboardHelper . SetClipboard ( ResourceTranslator . Translate ( ResourceIdentifier . PortState , SelectedResult . PingInfo . Status ) ) ;
372
- }
373
-
374
- public ICommand CopySelectedMACAddressCommand => new RelayCommand ( _ => CopySelectedMACAddressAction ( ) ) ;
375
-
376
- private void CopySelectedMACAddressAction ( )
377
- {
378
- ClipboardHelper . SetClipboard ( MACAddressHelper . GetDefaultFormat ( SelectedResult . MACAddress . ToString ( ) ) ) ;
379
- }
380
-
381
- public ICommand CopySelectedVendorCommand => new RelayCommand ( _ => CopySelectedVendorAction ( ) ) ;
382
-
383
- private void CopySelectedVendorAction ( )
384
- {
385
- ClipboardHelper . SetClipboard ( SelectedResult . Vendor ) ;
386
- }
387
-
388
324
public ICommand CopySelectedPortsCommand => new RelayCommand ( _ => CopySelectedPortsAction ( ) ) ;
389
325
390
326
private void CopySelectedPortsAction ( )
@@ -399,48 +335,27 @@ private void CopySelectedPortsAction()
399
335
ClipboardHelper . SetClipboard ( stringBuilder . ToString ( ) ) ;
400
336
}
401
337
402
- public ICommand CopySelectedBytesCommand => new RelayCommand ( _ => CopySelectedBytesAction ( ) ) ;
403
-
404
- private void CopySelectedBytesAction ( )
405
- {
406
- ClipboardHelper . SetClipboard ( SelectedResult . PingInfo . Bytes . ToString ( ) ) ;
407
- }
408
-
409
- public ICommand CopySelectedTimeCommand => new RelayCommand ( _ => CopySelectedTimeAction ( ) ) ;
410
-
411
- private void CopySelectedTimeAction ( )
412
- {
413
- ClipboardHelper . SetClipboard ( SelectedResult . PingInfo . Time . ToString ( ) ) ;
414
- }
415
-
416
- public ICommand CopySelectedTTLCommand => new RelayCommand ( _ => CopySelectedTTLAction ( ) ) ;
417
-
418
- private void CopySelectedTTLAction ( )
419
- {
420
- ClipboardHelper . SetClipboard ( SelectedResult . PingInfo . TTL . ToString ( ) ) ;
421
- }
422
-
423
338
public ICommand ExportCommand => new RelayCommand ( _ => ExportAction ( ) ) ;
424
339
425
340
private void ExportAction ( )
426
341
{
427
- Export ( ) ;
342
+ Export ( ) . ConfigureAwait ( false ) ;
428
343
}
429
344
#endregion
430
345
431
346
#region Methods
432
347
private void Scan ( )
433
348
{
434
- if ( IsScanRunning )
349
+ if ( IsRunning )
435
350
StopScan ( ) ;
436
351
else
437
- StartScan ( ) ;
352
+ StartScan ( ) . ConfigureAwait ( false ) ;
438
353
}
439
354
440
355
private async Task StartScan ( )
441
356
{
442
357
IsStatusMessageDisplayed = false ;
443
- IsScanRunning = true ;
358
+ IsRunning = true ;
444
359
PreparingScan = true ;
445
360
446
361
Results . Clear ( ) ;
@@ -545,7 +460,7 @@ private async Task DetectIPRange()
545
460
546
461
Hosts = $ "{ localIP } /{ Subnetmask . ConvertSubnetmaskToCidr ( networkInterface . IPv4Address . First ( ) . Item2 ) } ";
547
462
548
- // Fix: If the user clears the textbox and then clicks again on the button, the textbox remains empty...
463
+ // Fix: If the user clears the TextBox and then clicks again on the button, the TextBox remains empty...
549
464
OnPropertyChanged ( nameof ( Hosts ) ) ;
550
465
551
466
break ;
@@ -633,7 +548,13 @@ private async Task Export()
633
548
634
549
SettingsManager . Current . IPScanner_ExportFileType = instance . FileType ;
635
550
SettingsManager . Current . IPScanner_ExportFilePath = instance . FilePath ;
636
- } , instance => { _dialogCoordinator . HideMetroDialogAsync ( this , customDialog ) ; } , new [ ] { ExportFileType . Csv , ExportFileType . Xml , ExportFileType . Json } , true , SettingsManager . Current . IPScanner_ExportFileType , SettingsManager . Current . IPScanner_ExportFilePath ) ;
551
+ } , _ =>
552
+ {
553
+ _dialogCoordinator . HideMetroDialogAsync ( this , customDialog ) ;
554
+ } , new [ ]
555
+ {
556
+ ExportFileType . Csv , ExportFileType . Xml , ExportFileType . Json
557
+ } , true , SettingsManager . Current . IPScanner_ExportFileType , SettingsManager . Current . IPScanner_ExportFilePath ) ;
637
558
638
559
customDialog . Content = new ExportDialog
639
560
{
@@ -646,7 +567,7 @@ private async Task Export()
646
567
public void OnClose ( )
647
568
{
648
569
// Stop scan
649
- if ( IsScanRunning )
570
+ if ( IsRunning )
650
571
StopScan ( ) ;
651
572
}
652
573
@@ -664,7 +585,7 @@ private void HostFound(object sender, IPScannerHostScannedArgs e)
664
585
private void ScanComplete ( object sender , EventArgs e )
665
586
{
666
587
CancelScan = false ;
667
- IsScanRunning = false ;
588
+ IsRunning = false ;
668
589
}
669
590
670
591
private void ProgressChanged ( object sender , ProgressChangedArgs e )
@@ -678,29 +599,13 @@ private void DnsResolveFailed(AggregateException e)
678
599
IsStatusMessageDisplayed = true ;
679
600
680
601
CancelScan = false ;
681
- IsScanRunning = false ;
602
+ IsRunning = false ;
682
603
}
683
604
684
605
private void UserHasCanceled ( object sender , EventArgs e )
685
606
{
686
607
StatusMessage = Localization . Resources . Strings . CanceledByUserMessage ;
687
608
IsStatusMessageDisplayed = true ;
688
609
}
689
-
690
- private void SettingsManager_PropertyChanged ( object sender , PropertyChangedEventArgs e )
691
- {
692
- switch ( e . PropertyName )
693
- {
694
- case nameof ( SettingsInfo . IPScanner_ResolveHostname ) :
695
- OnPropertyChanged ( nameof ( ResolveHostname ) ) ;
696
- break ;
697
- case nameof ( SettingsInfo . IPScanner_PortScanEnabled ) :
698
- OnPropertyChanged ( nameof ( PortScanEnabled ) ) ;
699
- break ;
700
- case nameof ( SettingsInfo . IPScanner_ResolveMACAddress ) :
701
- OnPropertyChanged ( nameof ( ResolveMACAddress ) ) ;
702
- break ;
703
- }
704
- }
705
610
#endregion
706
611
}
0 commit comments