Skip to content

Commit

Permalink
Merge pull request #17 from BoBoBaSs84/feature/raise-collection-chang…
Browse files Browse the repository at this point in the history
…ed-with-action

feat: raise collection changed with action
  • Loading branch information
BoBoBaSs84 authored Oct 23, 2023
2 parents 0453bec + 3c1ff58 commit 374f073
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionMinor>2</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="$(Configuration.Equals('Debug'))">Development</VersionSuffix>
Expand Down
6 changes: 1 addition & 5 deletions src/BB84.Notifications/Enumerators/CollectionChangeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,5 @@ public enum CollectionChangeAction
/// <summary>
/// An item was replaced in the collection.
/// </summary>
Replace,
/// <summary>
/// An item was updated in the collection.
/// </summary>
Update
Replace
}
88 changes: 84 additions & 4 deletions src/BB84.Notifications/NotifyCollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,99 @@ public abstract class NotifyCollectionBase : NotifyPropertyBase, INotifyCollecti
/// <inheritdoc/>
public event CollectionChangingEventHandler? CollectionChanging;

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event with the
/// <see cref="CollectionChangeAction.Add"/> action.
/// </summary>
/// <param name="value">The object that is changed.</param>
protected void RaiseCollectionAdded(object? value = null)
=> RaiseCollectionChanged(CollectionChangeAction.Add, value);

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event with the
/// <see cref="CollectionChangeAction.Add"/> action.
/// </summary>
/// <param name="value">The object that is changing.</param>
protected void RaiseCollectionAdding(object? value = null)
=> RaiseCollectionChanging(CollectionChangeAction.Add, value);

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event with the
/// <see cref="CollectionChangeAction.Clear"/> action.
/// </summary>
/// <param name="value">The object that is changed.</param>
protected void RaiseCollectionCleared(object? value = null)
=> RaiseCollectionChanged(CollectionChangeAction.Clear, value);

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event with the
/// <see cref="CollectionChangeAction.Clear"/> action.
/// </summary>
/// <param name="value">The object that is changing.</param>
protected void RaiseCollectionClearing(object? value = null)
=> RaiseCollectionChanging(CollectionChangeAction.Clear, value);

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event with the
/// <see cref="CollectionChangeAction.Move"/> action.
/// </summary>
/// <param name="value">The object that is changed.</param>
protected void RaiseCollectionMoved(object? value = null)
=> RaiseCollectionChanged(CollectionChangeAction.Move, value);

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event with the
/// <see cref="CollectionChangeAction.Move"/> action.
/// </summary>
/// <param name="value">The object that is changing.</param>
protected void RaiseCollectionMoving(object? value = null)
=> RaiseCollectionChanging(CollectionChangeAction.Move, value);

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event with the
/// <see cref="CollectionChangeAction.Remove"/> action.
/// </summary>
/// <param name="value">The object that is changed.</param>
protected void RaiseCollectionRemoved(object? value = null)
=> RaiseCollectionChanged(CollectionChangeAction.Remove, value);

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event with the
/// <see cref="CollectionChangeAction.Remove"/> action.
/// </summary>
/// <param name="value">The object that is changing.</param>
protected void RaiseCollectionRemoving(object? value = null)
=> RaiseCollectionChanging(CollectionChangeAction.Remove, value);

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event with the
/// <see cref="CollectionChangeAction.Replace"/> action.
/// </summary>
/// <param name="value">The object that is changed.</param>
protected void RaiseCollectionReplaced(object? value = null)
=> RaiseCollectionChanged(CollectionChangeAction.Replace, value);

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event with the
/// <see cref="CollectionChangeAction.Replace"/> action.
/// </summary>
/// <param name="value">The object that is changing.</param>
protected void RaiseCollectionReplacing(object? value = null)
=> RaiseCollectionChanging(CollectionChangeAction.Replace, value);

/// <summary>
/// Raises the <see cref="CollectionChanged"/> event.
/// </summary>
/// <param name="action">The action that causes the change.</param>
/// <param name="value">The item that is changed.</param>
protected void RaiseCollectionChanged(CollectionChangeAction action, object? value = null)
/// <param name="value">The object that is changed.</param>
private void RaiseCollectionChanged(CollectionChangeAction action, object? value = null)
=> CollectionChanged?.Invoke(this, new(action, value));

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event.
/// </summary>
/// <param name="action">The action that causes the change.</param>
/// <param name="value">The item that is changing.</param>
protected void RaiseCollectionChanging(CollectionChangeAction action, object? value = null)
/// <param name="value">The object that is changing.</param>
private void RaiseCollectionChanging(CollectionChangeAction action, object? value = null)
=> CollectionChanging?.Invoke(this, new(action, value));
}
34 changes: 34 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Add.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Adding()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new();
strings.CollectionChanging += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Add(UnitTestString);

Assert.AreEqual(CollectionChangeAction.Add, action);
Assert.AreEqual(UnitTestString, item);
}

[TestMethod]
public void Added()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new();
strings.CollectionChanged += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Add(UnitTestString);

Assert.AreEqual(CollectionChangeAction.Add, action);
Assert.AreEqual(UnitTestString, item);
}
}
20 changes: 0 additions & 20 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Changed.cs

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Clear.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Clearing()
{
CollectionChangeAction action = default!;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanging += (sender, e) => action = e.Action;

strings.Clear();

Assert.AreEqual(CollectionChangeAction.Clear, action);
}

[TestMethod]
public void Cleared()
{
CollectionChangeAction action = default!;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanged += (sender, e) => action = e.Action;

strings.Clear();

Assert.AreEqual(CollectionChangeAction.Clear, action);
}
}
34 changes: 34 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Move.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Moving()
{
CollectionChangeAction action = default!;
int item = default;
MyCollection strings = new() { "A", "B", "C" };
strings.CollectionChanging += (sender, e) => { item = (int)e.Item!; action = e.Action; };

strings.Move(0, 1);

Assert.AreEqual(CollectionChangeAction.Move, action);
Assert.AreEqual(0, item);
}

[TestMethod]
public void Moved()
{
CollectionChangeAction action = default!;
int item = default;
MyCollection strings = new() { "A", "B", "C" };
strings.CollectionChanged += (sender, e) => { item = (int)e.Item!; action = e.Action; };

strings.Move(0, 1);

Assert.AreEqual(CollectionChangeAction.Move, action);
Assert.AreEqual(1, item);
}
}
34 changes: 34 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Remove.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Removing()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanging += (sender, e) => { item = (string)e.Item!; action = e.Action; };

_ = strings.Remove(UnitTestString);

Assert.AreEqual(CollectionChangeAction.Remove, action);
Assert.AreEqual(UnitTestString, item);
}

[TestMethod]
public void Removed()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanged += (sender, e) => { item = (string)e.Item!; action = e.Action; };

_ = strings.Remove(UnitTestString);

Assert.AreEqual(CollectionChangeAction.Remove, action);
Assert.AreEqual(UnitTestString, item);
}
}
34 changes: 34 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Replace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Replacing()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanging += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Update(UnitTestString, "UnitTest");

Assert.AreEqual(CollectionChangeAction.Replace, action);
Assert.AreEqual(UnitTestString, item);
}

[TestMethod]
public void Replaced()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new() { UnitTestString };
strings.CollectionChanged += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Update(UnitTestString, "UnitTest");

Assert.AreEqual(CollectionChangeAction.Replace, action);
Assert.AreEqual("UnitTest", item);
}
}
Loading

0 comments on commit 374f073

Please sign in to comment.