Skip to content

Commit f2e7c03

Browse files
authored
refactor: relay command review (#64)
changes: - changed `Action<object?>` to `Action` - changed `Predicate<object?>?` to `Func<bool>?` - small refactoring closes #63
1 parent a4a364d commit f2e7c03

File tree

6 files changed

+12
-15
lines changed

6 files changed

+12
-15
lines changed

src/BB84.Notifications/RelayCommand.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ namespace BB84.Notifications;
1212
/// </remarks>
1313
/// <param name="execute">The action to execute.</param>
1414
/// <param name="canExecute">The condition to execute.</param>
15-
public sealed class RelayCommand(Action<object?> execute, Predicate<object?>? canExecute) : IRelayCommand
15+
public sealed class RelayCommand(Action execute, Func<bool>? canExecute) : IRelayCommand
1616
{
1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="RelayCommand"/> class that can always execute.
1919
/// </summary>
2020
/// <param name="execute">The action to execute.</param>
21-
public RelayCommand(Action<object?> execute) : this(execute, null)
21+
public RelayCommand(Action execute) : this(execute, null)
2222
{ }
2323

2424
/// <inheritdoc/>
2525
public event EventHandler? CanExecuteChanged;
2626

2727
/// <inheritdoc/>
2828
public bool CanExecute(object? parameter)
29-
=> canExecute is null || canExecute.Invoke(parameter);
29+
=> canExecute is null || canExecute.Invoke();
3030

3131
/// <inheritdoc/>
3232
public void Execute(object? parameter)
33-
=> execute.Invoke(parameter);
33+
=> execute.Invoke();
3434

3535
/// <inheritdoc/>
3636
public void NotifyCanExecuteChanged()
@@ -48,7 +48,7 @@ public void NotifyCanExecuteChanged()
4848
/// <typeparam name="T">The type to wor with</typeparam>
4949
/// <param name="execute">The action to execute.</param>
5050
/// <param name="canExecute">The condition to execute.</param>
51-
public sealed class RelayCommand<T>(Action<T> execute, Predicate<T>? canExecute) : IRelayCommand<T>
51+
public sealed class RelayCommand<T>(Action<T> execute, Func<T, bool>? canExecute) : IRelayCommand<T>
5252
{
5353
/// <summary>
5454
/// Initializes a new instance of <see cref="RelayCommand{T}"/> class that can always execute.

tests/BB84.NotificationsTests/Attributes/NotifyChangedAttributeTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class NotifyChangedAttributeTests
99
[TestMethod]
1010
public void NotifyChangedAttributeTest()
1111
{
12-
List<string> propertiesNotified = new();
12+
List<string> propertiesNotified = [];
1313
TestClass testClass = new(3, 40);
1414
testClass.PropertyChanged += (sender, e) => propertiesNotified.Add(e.PropertyName!);
1515

tests/BB84.NotificationsTests/Attributes/NotifyChangingAttributeTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class NotifyChangingAttributeTests
99
[TestMethod]
1010
public void NotifyChangingAttributeTest()
1111
{
12-
List<string> propertiesNotified = new();
12+
List<string> propertiesNotified = [];
1313
TestClass testClass = new(3, 40);
1414
testClass.PropertyChanging += (sender, e) => propertiesNotified.Add(e.PropertyName!);
1515

tests/BB84.NotificationsTests/BindablePropertyTests.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ namespace BB84.NotificationsTests;
66
[TestClass]
77
public sealed partial class BindablePropertyTests
88
{
9-
private sealed class TestClass
9+
private sealed class TestClass(int bindableProperty)
1010
{
11-
public TestClass(int bindableProperty)
12-
=> BindableProperty = new BindableProperty<int>(bindableProperty);
13-
14-
public IBindableProperty<int> BindableProperty { get; set; }
11+
public IBindableProperty<int> BindableProperty { get; set; } = new BindableProperty<int>(bindableProperty);
1512
}
1613
}

tests/BB84.NotificationsTests/NotificationCollectionTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private sealed class MyCollection : NotificationCollection, ICollection<string>
1616
private readonly Collection<string> _collection;
1717

1818
public MyCollection()
19-
=> _collection = new Collection<string>();
19+
=> _collection = [];
2020

2121
public int Count => _collection.Count;
2222
public bool IsReadOnly => false;

tests/BB84.NotificationsTests/RelayCommandTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public TestClass()
2020
}
2121

2222
public IRelayCommand Command
23-
=> _command ??= new RelayCommand(x => CommandExecution++);
23+
=> _command ??= new RelayCommand(() => CommandExecution++);
2424

2525
public IRelayCommand CondCommand
26-
=> _condCommand ??= new RelayCommand(x => CommandExecution++, x => false);
26+
=> _condCommand ??= new RelayCommand(() => CommandExecution++, () => false);
2727

2828
public IRelayCommand<int> IntCommand
2929
=> _intCommand ??= new RelayCommand<int>(x => IntegerCommandExecution = x);

0 commit comments

Comments
 (0)