Skip to content

Commit 4a46753

Browse files
committed
Fix: Use ObservableCollection / avoid multiple calls
1 parent 80728f6 commit 4a46753

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

Source/NETworkManager/ViewModels/NetworkInterfaceViewModel.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.ComponentModel;
45
using System.Diagnostics;
56
using System.Linq;
@@ -32,8 +33,9 @@ namespace NETworkManager.ViewModels;
3233
public class NetworkInterfaceViewModel : ViewModelBase, IProfileManager
3334
{
3435
#region Variables
36+
3537
private static readonly ILog Log = LogManager.GetLogger(typeof(NetworkInterfaceViewModel));
36-
38+
3739
private readonly IDialogCoordinator _dialogCoordinator;
3840
private readonly DispatcherTimer _searchDispatcherTimer = new();
3941
private BandwidthMeter _bandwidthMeter;
@@ -118,9 +120,9 @@ private set
118120

119121
#region NetworkInterfaces, SelectedNetworkInterface
120122

121-
private List<NetworkInterfaceInfo> _networkInterfaces;
123+
private ObservableCollection<NetworkInterfaceInfo> _networkInterfaces = [];
122124

123-
public List<NetworkInterfaceInfo> NetworkInterfaces
125+
public ObservableCollection<NetworkInterfaceInfo> NetworkInterfaces
124126
{
125127
get => _networkInterfaces;
126128
private set
@@ -629,7 +631,8 @@ private async Task LoadNetworkInterfaces()
629631
{
630632
IsNetworkInterfaceLoading = true;
631633

632-
NetworkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync();
634+
// Get all network interfaces...
635+
(await NetworkInterface.GetNetworkInterfacesAsync()).ForEach(NetworkInterfaces.Add);
633636

634637
// Get the last selected interface, if it is still available on this machine...
635638
if (NetworkInterfaces.Count > 0)
@@ -695,7 +698,7 @@ private async Task ExportAction()
695698
catch (Exception ex)
696699
{
697700
Log.Error("Error while exporting data as " + instance.FileType, ex);
698-
701+
699702
var settings = AppearanceManager.MetroDialog;
700703
settings.AffirmativeButtonText = Strings.OK;
701704

@@ -935,22 +938,36 @@ private async Task RemoveIPv4AddressAction()
935938

936939
private async void ReloadNetworkInterfaces()
937940
{
941+
// Avoid multiple reloads
942+
if(IsNetworkInterfaceLoading)
943+
return;
944+
938945
IsNetworkInterfaceLoading = true;
939946

940947
// Make the user happy, let him see a reload animation (and he cannot spam the reload command)
941948
await Task.Delay(2000);
942949

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+
954971
IsNetworkInterfaceLoading = false;
955972
}
956973

0 commit comments

Comments
 (0)