Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Rework ServerConnectionInfoProfile view/viewmodel (Fix DNS, SNTP app crash) #2282

Merged
merged 2 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3541,4 +3541,10 @@ Changes to this value will take effect after the application is restarted. Wheth
<data name="NewSubnetMask" xml:space="preserve">
<value>New subnet mask</value>
</data>
<data name="AddServer" xml:space="preserve">
<value>Add server</value>
</data>
<data name="SNTPServer" xml:space="preserve">
<value>SNTP server</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace NETworkManager.Models.Network;

Expand Down
15 changes: 7 additions & 8 deletions Source/NETworkManager/ViewModels/DNSLookupSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public class DNSLookupSettingsViewModel : ViewModelBase
{
#region Variables
private readonly bool _isLoading;
private readonly ServerConnectionInfo _profileDialog_DefaultValues = new("10.0.0.1", 53, TransportProtocol.UDP);

private readonly IDialogCoordinator _dialogCoordinator;

private (string Server, int Port, TransportProtocol TransportProtocol) _profileDialog_NewItemsOptions = ("1.1.1.1", 53, TransportProtocol.UDP);

public ICollectionView DNSServers { get; }

private DNSServerConnectionInfoProfile _selectedDNSServer = new();
Expand Down Expand Up @@ -287,13 +286,13 @@ public async Task AddDNSServer()
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

SettingsManager.Current.DNSLookup_DNSServers_v2.Add(new DNSServerConnectionInfoProfile(instance.Name, instance.Servers));
SettingsManager.Current.DNSLookup_DNSServers_v2.Add(new DNSServerConnectionInfoProfile(instance.Name, instance.Servers.ToList()));
}, instance =>
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
}, (ServerInfoProfileNames, false, true));
}, (ServerInfoProfileNames, false, true), _profileDialog_DefaultValues);

customDialog.Content = new ServerConnectionInfoProfileDialog(_profileDialog_NewItemsOptions)
customDialog.Content = new ServerConnectionInfoProfileDialog()
{
DataContext = viewModel
};
Expand All @@ -313,13 +312,13 @@ public async Task EditDNSServer()
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

SettingsManager.Current.DNSLookup_DNSServers_v2.Remove(SelectedDNSServer);
SettingsManager.Current.DNSLookup_DNSServers_v2.Add(new DNSServerConnectionInfoProfile(instance.Name, instance.Servers));
SettingsManager.Current.DNSLookup_DNSServers_v2.Add(new DNSServerConnectionInfoProfile(instance.Name, instance.Servers.ToList()));
}, instance =>
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
}, (ServerInfoProfileNames, true, true), SelectedDNSServer);
}, (ServerInfoProfileNames, true, true), _profileDialog_DefaultValues, SelectedDNSServer);

customDialog.Content = new ServerConnectionInfoProfileDialog(_profileDialog_NewItemsOptions)
customDialog.Content = new ServerConnectionInfoProfileDialog()
{
DataContext = viewModel
};
Expand Down
17 changes: 8 additions & 9 deletions Source/NETworkManager/ViewModels/SNTPLookupSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public class SNTPLookupSettingsViewModel : ViewModelBase
{
#region Variables
private readonly bool _isLoading;

private readonly ServerConnectionInfo _profileDialog_DefaultValues = new("time.example.com", 123, TransportProtocol.TCP);

private readonly IDialogCoordinator _dialogCoordinator;

private (string Server, int Port, TransportProtocol TransportProtocol) _profileDialog_NewItemsOptions = ("time.example.com", 123, TransportProtocol.TCP);

private ICollectionView _sntpServers;
public ICollectionView SNTPServers
{
Expand Down Expand Up @@ -130,13 +129,13 @@ public async Task AddServer()
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

SettingsManager.Current.SNTPLookup_SNTPServers.Add(new ServerConnectionInfoProfile(instance.Name, instance.Servers));
SettingsManager.Current.SNTPLookup_SNTPServers.Add(new ServerConnectionInfoProfile(instance.Name, instance.Servers.ToList()));
}, instance =>
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
}, (ServerInfoProfileNames, false, false));
}, (ServerInfoProfileNames, false, false), _profileDialog_DefaultValues);

customDialog.Content = new ServerConnectionInfoProfileDialog(_profileDialog_NewItemsOptions)
customDialog.Content = new ServerConnectionInfoProfileDialog()
{
DataContext = viewModel
};
Expand All @@ -156,13 +155,13 @@ public async Task EditServer()
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);

SettingsManager.Current.SNTPLookup_SNTPServers.Remove(SelectedSNTPServer);
SettingsManager.Current.SNTPLookup_SNTPServers.Add(new ServerConnectionInfoProfile(instance.Name, instance.Servers));
SettingsManager.Current.SNTPLookup_SNTPServers.Add(new ServerConnectionInfoProfile(instance.Name, instance.Servers.ToList()));
}, instance =>
{
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
}, (ServerInfoProfileNames, true, false), SelectedSNTPServer);
}, (ServerInfoProfileNames, true, false), _profileDialog_DefaultValues, SelectedSNTPServer);

customDialog.Content = new ServerConnectionInfoProfileDialog(_profileDialog_NewItemsOptions)
customDialog.Content = new ServerConnectionInfoProfileDialog()
{
DataContext = viewModel
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
using System.Windows.Input;
using NETworkManager.Models.Network;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Diagnostics;

namespace NETworkManager.ViewModels;

public class ServerConnectionInfoProfileViewModel : ViewModelBase
{
private readonly bool _isLoading;

private ServerConnectionInfo _defaultValues;

#region Commands
public ICommand SaveCommand { get; }

Expand Down Expand Up @@ -90,13 +91,12 @@ public string Name
return;

_name = value;

OnPropertyChanged();
}
}

private List<ServerConnectionInfo> _servers;
public List<ServerConnectionInfo> Servers
private ObservableCollection<ServerConnectionInfo> _servers;
public ObservableCollection<ServerConnectionInfo> Servers
{
get => _servers;
set
Expand All @@ -108,9 +108,55 @@ public List<ServerConnectionInfo> Servers
OnPropertyChanged();
}
}

private IList _selectedServers = new ArrayList();
public IList SelectedServers
{
get => _selectedServers;
set
{
if (Equals(value, _selectedServers))
return;

_selectedServers = value;
OnPropertyChanged();
}
}

public string ServerWatermark { get; private set; }

private string _server;
public string Server
{
get => _server;
set
{
if (_server == value)
return;

_server = value;
OnPropertyChanged();
}
}

public string PortWatermark { get; private set; }

private int _port;
public int Port
{
get => _port;
set
{
if (_port == value)
return;

_port = value;
OnPropertyChanged();
}
}
#endregion

public ServerConnectionInfoProfileViewModel(Action<ServerConnectionInfoProfileViewModel> saveCommand, Action<ServerConnectionInfoProfileViewModel> cancelHandler, (List<string> UsedNames, bool IsEdited, bool allowOnlyIPAddress) options, ServerConnectionInfoProfile info = null)
public ServerConnectionInfoProfileViewModel(Action<ServerConnectionInfoProfileViewModel> saveCommand, Action<ServerConnectionInfoProfileViewModel> cancelHandler, (List<string> UsedNames, bool IsEdited, bool allowOnlyIPAddress) options, ServerConnectionInfo defaultValues, ServerConnectionInfoProfile info = null)
{
_isLoading = true;

Expand All @@ -120,15 +166,42 @@ public ServerConnectionInfoProfileViewModel(Action<ServerConnectionInfoProfileVi
UsedNames = options.UsedNames;
AllowOnlyIPAddress = options.allowOnlyIPAddress;
IsEdited = options.IsEdited;

_defaultValues = defaultValues;

CurrentProfile = info ?? new ServerConnectionInfoProfile();

// Remove the current profile name from the list
if (IsEdited)
UsedNames.Remove(CurrentProfile.Name);

Name = _currentProfile.Name;
Servers = _currentProfile.Servers;
Servers = new ObservableCollection<ServerConnectionInfo>(_currentProfile.Servers);

ServerWatermark = _defaultValues.Server;
PortWatermark = _defaultValues.Port.ToString();
Port = _defaultValues.Port;

_isLoading = false;
}

public ICommand AddServerCommand => new RelayCommand(p => AddServerAction());

private void AddServerAction()
{
Servers.Add(new ServerConnectionInfo(Server, Port, _defaultValues.TransportProtocol));

Server = string.Empty;
}

public ICommand DeleteServerCommand => new RelayCommand(p => DeleteServerAction());

private void DeleteServerAction()
{
// Cast to list to avoid exception: Collection was modified; enumeration operation may not execute.
// This will create a copy of the list and allow us to remove items from the original list (SelectedServers
// is modified when Servers changes).
foreach (var item in SelectedServers.Cast<ServerConnectionInfo>().ToList())
Servers.Remove(item);
}
}
2 changes: 1 addition & 1 deletion Source/NETworkManager/Views/SNTPLookupSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</UserControl.Resources>
<StackPanel>
<TextBlock Text="{x:Static localization:Strings.SNTPLookup}" Style="{StaticResource HeaderTextBlock}" />
<TextBlock Text="{x:Static localization:Strings.SNTPServers}" Style="{DynamicResource DefaultTextBlock}" Margin="0,0,0,10" />
<TextBlock Text="{x:Static localization:Strings.SNTPServer}" Style="{DynamicResource DefaultTextBlock}" Margin="0,0,0,10" />
<DataGrid x:Name="DataGridSNTPServers" ItemsSource="{Binding SNTPServers}" SelectionMode="Single" SelectedItem="{Binding SelectedSNTPServer}" Height="200" Margin="0,0,0,10">
<DataGrid.Resources>
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DataGridScrollBar}" />
Expand Down
Loading