Skip to content
Draft
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
21 changes: 20 additions & 1 deletion Source/NETworkManager.Utilities/PropertyChangedBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace NETworkManager.Utilities;
Expand All @@ -11,4 +12,22 @@ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

/// <summary>
/// Sets the property value and raises PropertyChanged event if the value has changed.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="field">Reference to the backing field.</param>
/// <param name="value">The new value to set.</param>
/// <param name="propertyName">Name of the property (automatically captured).</param>
/// <returns>True if the value changed and PropertyChanged was raised; otherwise false.</returns>
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;

field = value;
OnPropertyChanged(propertyName);
return true;
}
}
33 changes: 33 additions & 0 deletions Source/NETworkManager/ViewModels/ConnectDialogViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Windows.Input;
using NETworkManager.Utilities;

namespace NETworkManager.ViewModels;

/// <summary>
/// Base class for connect dialog ViewModels that have Connect and Cancel commands.
/// </summary>
/// <typeparam name="T">The type of the derived ViewModel.</typeparam>
public abstract class ConnectDialogViewModelBase<T> : ViewModelBase where T : ConnectDialogViewModelBase<T>
{
/// <summary>
/// Command executed when Connect button is clicked.
/// </summary>
public ICommand ConnectCommand { get; }

/// <summary>
/// Command executed when Cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ConnectDialogViewModelBase{T}"/> class.
/// </summary>
/// <param name="connectCommand">Action to execute when Connect is clicked.</param>
/// <param name="cancelHandler">Action to execute when Cancel is clicked.</param>
protected ConnectDialogViewModelBase(Action<T> connectCommand, Action<T> cancelHandler)
{
ConnectCommand = new RelayCommand(_ => connectCommand((T)this));
CancelCommand = new RelayCommand(_ => cancelHandler((T)this));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Security;
using System.Windows.Input;
using NETworkManager.Utilities;

namespace NETworkManager.ViewModels;

public class CredentialsChangePasswordViewModel : ViewModelBase
public class CredentialsChangePasswordViewModel : DialogViewModelBase<CredentialsChangePasswordViewModel>
{
/// <summary>
/// Private variable for <see cref="IsPasswordEmpty" />.
Expand Down Expand Up @@ -40,21 +39,10 @@ public class CredentialsChangePasswordViewModel : ViewModelBase
/// <param name="cancelHandler"><see cref="CancelCommand" /> which is executed on cancel click.</param>
public CredentialsChangePasswordViewModel(Action<CredentialsChangePasswordViewModel> okCommand,
Action<CredentialsChangePasswordViewModel> cancelHandler)
: base(okCommand, cancelHandler)
{
OKCommand = new RelayCommand(_ => okCommand(this));
CancelCommand = new RelayCommand(_ => cancelHandler(this));
}

/// <summary>
/// Command which is called when the OK button is clicked.
/// </summary>
public ICommand OKCommand { get; }

/// <summary>
/// Command which is called when the cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Password as <see cref="SecureString" />.
/// </summary>
Expand Down Expand Up @@ -118,14 +106,7 @@ public SecureString NewPasswordRepeat
public bool IsPasswordEmpty
{
get => _isPasswordEmpty;
set
{
if (value == _isPasswordEmpty)
return;

_isPasswordEmpty = value;
OnPropertyChanged();
}
set => SetProperty(ref _isPasswordEmpty, value);
}

/// <summary>
Expand All @@ -134,14 +115,7 @@ public bool IsPasswordEmpty
public bool IsRepeatedPasswordEqual
{
get => _isRepeatedPasswordEqual;
set
{
if (value == _isRepeatedPasswordEqual)
return;

_isRepeatedPasswordEqual = value;
OnPropertyChanged();
}
set => SetProperty(ref _isRepeatedPasswordEqual, value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using NETworkManager.Utilities;
using System;
using System;
using System.Security;
using System.Windows.Input;

namespace NETworkManager.ViewModels;

public class CredentialsPasswordProfileFileViewModel : ViewModelBase
public class CredentialsPasswordProfileFileViewModel : DialogViewModelBase<CredentialsPasswordProfileFileViewModel>
{
/// <summary>
/// Private variable for <see cref="IsPasswordEmpty" />.
Expand Down Expand Up @@ -38,38 +36,19 @@ public class CredentialsPasswordProfileFileViewModel : ViewModelBase
public CredentialsPasswordProfileFileViewModel(Action<CredentialsPasswordProfileFileViewModel> okCommand,
Action<CredentialsPasswordProfileFileViewModel> cancelHandler, string profileName,
bool showWrongPassword = false)
: base(okCommand, cancelHandler)
{
OKCommand = new RelayCommand(_ => okCommand(this));
CancelCommand = new RelayCommand(_ => cancelHandler(this));

ProfileName = profileName;
ShowWrongPassword = showWrongPassword;
}

/// <summary>
/// Command which is called when the OK button is clicked.
/// </summary>
public ICommand OKCommand { get; }

/// <summary>
/// Command which is called when the cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Name of the profile file.
/// </summary>
public string ProfileName
{
get => _profileName;
set
{
if (value == _profileName)
return;

_profileName = value;
OnPropertyChanged();
}
set => SetProperty(ref _profileName, value);
}

/// <summary>
Expand All @@ -78,14 +57,7 @@ public string ProfileName
public bool ShowWrongPassword
{
get => _showWrongPassword;
set
{
if (value == _showWrongPassword)
return;

_showWrongPassword = value;
OnPropertyChanged();
}
set => SetProperty(ref _showWrongPassword, value);
}

/// <summary>
Expand Down Expand Up @@ -113,14 +85,7 @@ public SecureString Password
public bool IsPasswordEmpty
{
get => _isPasswordEmpty;
set
{
if (value == _isPasswordEmpty)
return;

_isPasswordEmpty = value;
OnPropertyChanged();
}
set => SetProperty(ref _isPasswordEmpty, value);
}

/// <summary>
Expand Down
26 changes: 3 additions & 23 deletions Source/NETworkManager/ViewModels/CredentialsPasswordViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Security;
using System.Windows.Input;
using NETworkManager.Utilities;

namespace NETworkManager.ViewModels;

public class CredentialsPasswordViewModel : ViewModelBase
public class CredentialsPasswordViewModel : DialogViewModelBase<CredentialsPasswordViewModel>
{
/// <summary>
/// Private variable for <see cref="IsPasswordEmpty" />.
Expand All @@ -25,21 +23,10 @@ public class CredentialsPasswordViewModel : ViewModelBase
/// <param name="cancelHandler"><see cref="CancelCommand" /> which is executed on cancel click.</param>
public CredentialsPasswordViewModel(Action<CredentialsPasswordViewModel> okCommand,
Action<CredentialsPasswordViewModel> cancelHandler)
: base(okCommand, cancelHandler)
{
OKCommand = new RelayCommand(_ => okCommand(this));
CancelCommand = new RelayCommand(_ => cancelHandler(this));
}

/// <summary>
/// Command which is called when the OK button is clicked.
/// </summary>
public ICommand OKCommand { get; }

/// <summary>
/// Command which is called when the cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Password as <see cref="SecureString" />.
/// </summary>
Expand All @@ -65,14 +52,7 @@ public SecureString Password
public bool IsPasswordEmpty
{
get => _isPasswordEmpty;
set
{
if (value == _isPasswordEmpty)
return;

_isPasswordEmpty = value;
OnPropertyChanged();
}
set => SetProperty(ref _isPasswordEmpty, value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Security;
using System.Windows.Input;
using NETworkManager.Utilities;

namespace NETworkManager.ViewModels;

public class CredentialsSetPasswordViewModel : ViewModelBase
public class CredentialsSetPasswordViewModel : DialogViewModelBase<CredentialsSetPasswordViewModel>
{
/// <summary>
/// Private variable for <see cref="IsPasswordEmpty" />.
Expand Down Expand Up @@ -35,21 +34,10 @@ public class CredentialsSetPasswordViewModel : ViewModelBase
/// <param name="cancelHandler"><see cref="CancelCommand" /> which is executed on cancel click.</param>
public CredentialsSetPasswordViewModel(Action<CredentialsSetPasswordViewModel> okCommand,
Action<CredentialsSetPasswordViewModel> cancelHandler)
: base(okCommand, cancelHandler)
{
OKCommand = new RelayCommand(_ => okCommand(this));
CancelCommand = new RelayCommand(_ => cancelHandler(this));
}

/// <summary>
/// Command which is called when the OK button is clicked.
/// </summary>
public ICommand OKCommand { get; }

/// <summary>
/// Command which is called when the cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Password as <see cref="SecureString" />.
/// </summary>
Expand Down Expand Up @@ -94,14 +82,7 @@ public SecureString PasswordRepeat
public bool IsPasswordEmpty
{
get => _isPasswordEmpty;
set
{
if (value == _isPasswordEmpty)
return;

_isPasswordEmpty = value;
OnPropertyChanged();
}
set => SetProperty(ref _isPasswordEmpty, value);
}

/// <summary>
Expand All @@ -110,14 +91,7 @@ public bool IsPasswordEmpty
public bool IsRepeatedPasswordEqual
{
get => _isRepeatedPasswordEqual;
set
{
if (value == _isRepeatedPasswordEqual)
return;

_isRepeatedPasswordEqual = value;
OnPropertyChanged();
}
set => SetProperty(ref _isRepeatedPasswordEqual, value);
}

/// <summary>
Expand Down
33 changes: 33 additions & 0 deletions Source/NETworkManager/ViewModels/DialogViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Windows.Input;
using NETworkManager.Utilities;

namespace NETworkManager.ViewModels;

/// <summary>
/// Base class for dialog ViewModels that have OK and Cancel commands.
/// </summary>
/// <typeparam name="T">The type of the derived ViewModel.</typeparam>
public abstract class DialogViewModelBase<T> : ViewModelBase where T : DialogViewModelBase<T>
{
/// <summary>
/// Command executed when OK button is clicked.
/// </summary>
public ICommand OKCommand { get; }

/// <summary>
/// Command executed when Cancel button is clicked.
/// </summary>
public ICommand CancelCommand { get; }

/// <summary>
/// Initializes a new instance of the <see cref="DialogViewModelBase{T}"/> class.
/// </summary>
/// <param name="okCommand">Action to execute when OK is clicked.</param>
/// <param name="cancelHandler">Action to execute when Cancel is clicked.</param>
protected DialogViewModelBase(Action<T> okCommand, Action<T> cancelHandler)
{
OKCommand = new RelayCommand(_ => okCommand((T)this));
CancelCommand = new RelayCommand(_ => cancelHandler((T)this));
}
}
Loading