|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
3 | 4 | using System.ComponentModel; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.Linq; |
@@ -32,8 +33,9 @@ namespace NETworkManager.ViewModels; |
32 | 33 | public class NetworkInterfaceViewModel : ViewModelBase, IProfileManager |
33 | 34 | { |
34 | 35 | #region Variables |
| 36 | + |
35 | 37 | private static readonly ILog Log = LogManager.GetLogger(typeof(NetworkInterfaceViewModel)); |
36 | | - |
| 38 | + |
37 | 39 | private readonly IDialogCoordinator _dialogCoordinator; |
38 | 40 | private readonly DispatcherTimer _searchDispatcherTimer = new(); |
39 | 41 | private BandwidthMeter _bandwidthMeter; |
@@ -118,9 +120,9 @@ private set |
118 | 120 |
|
119 | 121 | #region NetworkInterfaces, SelectedNetworkInterface |
120 | 122 |
|
121 | | - private List<NetworkInterfaceInfo> _networkInterfaces; |
| 123 | + private ObservableCollection<NetworkInterfaceInfo> _networkInterfaces = []; |
122 | 124 |
|
123 | | - public List<NetworkInterfaceInfo> NetworkInterfaces |
| 125 | + public ObservableCollection<NetworkInterfaceInfo> NetworkInterfaces |
124 | 126 | { |
125 | 127 | get => _networkInterfaces; |
126 | 128 | private set |
@@ -629,7 +631,8 @@ private async Task LoadNetworkInterfaces() |
629 | 631 | { |
630 | 632 | IsNetworkInterfaceLoading = true; |
631 | 633 |
|
632 | | - NetworkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync(); |
| 634 | + // Get all network interfaces... |
| 635 | + (await NetworkInterface.GetNetworkInterfacesAsync()).ForEach(NetworkInterfaces.Add); |
633 | 636 |
|
634 | 637 | // Get the last selected interface, if it is still available on this machine... |
635 | 638 | if (NetworkInterfaces.Count > 0) |
@@ -695,7 +698,7 @@ private async Task ExportAction() |
695 | 698 | catch (Exception ex) |
696 | 699 | { |
697 | 700 | Log.Error("Error while exporting data as " + instance.FileType, ex); |
698 | | - |
| 701 | + |
699 | 702 | var settings = AppearanceManager.MetroDialog; |
700 | 703 | settings.AffirmativeButtonText = Strings.OK; |
701 | 704 |
|
@@ -935,22 +938,36 @@ private async Task RemoveIPv4AddressAction() |
935 | 938 |
|
936 | 939 | private async void ReloadNetworkInterfaces() |
937 | 940 | { |
| 941 | + // Avoid multiple reloads |
| 942 | + if(IsNetworkInterfaceLoading) |
| 943 | + return; |
| 944 | + |
938 | 945 | IsNetworkInterfaceLoading = true; |
939 | 946 |
|
940 | 947 | // Make the user happy, let him see a reload animation (and he cannot spam the reload command) |
941 | 948 | await Task.Delay(2000); |
942 | 949 |
|
943 | | - var id = string.Empty; |
944 | | - |
945 | | - if (SelectedNetworkInterface != null) |
946 | | - id = SelectedNetworkInterface.Id; |
947 | | - |
948 | | - // Load network interfaces... |
949 | | - var networkItems = await Models.Network.NetworkInterface.GetNetworkInterfacesAsync(); |
950 | | - |
951 | | - // Change interface... |
952 | | - SelectedNetworkInterface = string.IsNullOrEmpty(id) ? networkItems.FirstOrDefault() : networkItems.FirstOrDefault(x => x.Id == id); |
953 | | - NetworkInterfaces = networkItems; |
| 950 | + // Store the last selected id |
| 951 | + var id = SelectedNetworkInterface?.Id ?? string.Empty; |
| 952 | + |
| 953 | + // Get all network interfaces... |
| 954 | + var networkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync(); |
| 955 | + |
| 956 | + // Invoke on UI thread synchronously |
| 957 | + Application.Current.Dispatcher.Invoke(() => |
| 958 | + { |
| 959 | + // Clear the list |
| 960 | + NetworkInterfaces.Clear(); |
| 961 | + |
| 962 | + // Add all network interfaces to the list |
| 963 | + networkInterfaces.ForEach(NetworkInterfaces.Add); |
| 964 | + }); |
| 965 | + |
| 966 | + // Set the last selected interface, if it is still available on this machine... |
| 967 | + SelectedNetworkInterface = string.IsNullOrEmpty(id) |
| 968 | + ? NetworkInterfaces.FirstOrDefault() |
| 969 | + : NetworkInterfaces.FirstOrDefault(x => x.Id == id); |
| 970 | + |
954 | 971 | IsNetworkInterfaceLoading = false; |
955 | 972 | } |
956 | 973 |
|
|
0 commit comments