Skip to content

Commit

Permalink
Add binding support for Count property (ObservableKeyGroupsCollection) (
Browse files Browse the repository at this point in the history
  • Loading branch information
wcoder authored Oct 25, 2023
1 parent 27f1b7a commit f87e2c8
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using Xunit;
using CollectionHelper = Softeq.XToolkit.Common.Tests.Collections.ObservableKeyGroupsCollectionTest.ObservableKeyGroupsCollectionHelper;

namespace Softeq.XToolkit.Common.Tests.Collections.ObservableKeyGroupsCollectionTest;

public class ObservableKeyGroupsCollectionTestsCountChanged
{
[Fact]
public void AddGroups_KeysOnlyForbidEmptyGroupCorrectKeys_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithoutEmpty();
var pairs = CollectionHelper.PairNotContainedKeyWithItems;

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.AddGroups(pairs);
});
}

[Fact]
public void InsertGroups_KeysOnlyForbidEmptyGroupCorrectKeys_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithoutEmpty();
var pairs = CollectionHelper.PairNotContainedKeyWithItems;

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.InsertGroups(0, pairs);
});
}

[Fact]
public void ReplaceAllGroups_KeysOnlyAllowEmptyGroupCorrectKeys_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithEmpty();
var keys = CollectionHelper.KeysEmpty;

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.ReplaceAllGroups(keys);
});
}

[Fact]
public void ReplaceAllGroups_KeysOnlyAllowEmptyGroupEmptyCollection_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithEmpty();
var pairs = CollectionHelper.PairsEmpty;

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.ReplaceAllGroups(pairs);
});
}

[Fact]
public void RemoveGroups_KeysOnlyForbidEmptyGroupContainsKey_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithoutEmpty();

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.RemoveGroups(CollectionHelper.KeysOneFill);
});
}

[Fact]
public void Clear_FilledCollection_RaisesPropertyChangedForCount()
{
var collection = CollectionHelper.CreateFilledGroupsWithoutEmpty();

Assert.PropertyChanged(collection, nameof(collection.Count), () =>
{
collection.Clear();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Softeq.XToolkit.Common.Collections.EventArgs;
using Softeq.XToolkit.Common.Extensions;

Expand All @@ -21,7 +23,8 @@ namespace Softeq.XToolkit.Common.Collections
public sealed class ObservableKeyGroupsCollection<TKey, TValue>
: IObservableKeyGroupsCollection<TKey, TValue>,
INotifyKeyGroupCollectionChanged<TKey, TValue>,
INotifyCollectionChanged
INotifyCollectionChanged,
INotifyPropertyChanged
where TKey : notnull
where TValue : notnull
{
Expand All @@ -42,6 +45,7 @@ public ObservableKeyGroupsCollection(bool allowEmptyGroups = true)

public event NotifyCollectionChangedEventHandler? CollectionChanged;
public event EventHandler<NotifyKeyGroupCollectionChangedEventArgs<TKey, TValue>>? ItemsChanged;
public event PropertyChangedEventHandler? PropertyChanged;

public IList<TKey> Keys => _groups.Select(item => item.Key).ToList();

Expand Down Expand Up @@ -161,14 +165,11 @@ public void ReplaceAllGroups(IEnumerable<KeyValuePair<TKey, IList<TValue>>> item
_groups.Clear();

var insertedGroups = InsertGroupsWithoutNotify(0, items, _emptyGroupsDisabled);
if (insertedGroups == null)
{
return;
}
var insertedGroupKeys = insertedGroups?.Select(x => x.Key).ToList() ?? new List<TKey>();

var newItems = new Collection<(int, IReadOnlyList<TKey>)>
{
(0, insertedGroups.Select(x => x.Key).ToList())
(0, insertedGroupKeys)
};

OnChanged(
Expand Down Expand Up @@ -534,6 +535,8 @@ private void RaiseEvents(NotifyKeyGroupCollectionChangedEventArgs<TKey, TValue>
}

ItemsChanged?.Invoke(this, args);

NotifyCountIfNeeded(args);
}

private IEnumerable<Group>? InsertGroupsWithoutNotify(
Expand Down Expand Up @@ -697,6 +700,29 @@ private void DecrementIndex<T>(IList<KeyValuePair<T, int>> indexes)
}
}

private void NotifyCountIfNeeded(NotifyKeyGroupCollectionChangedEventArgs<TKey, TValue> args)
{
var isCountOfGroupsChanged = IsActionCanModifyGroup(args.Action);
if (isCountOfGroupsChanged)
{
OnPropertyChanged(nameof(Count));
}
}

private bool IsActionCanModifyGroup(NotifyCollectionChangedAction? action)
{
return action
is NotifyCollectionChangedAction.Add
or NotifyCollectionChangedAction.Remove
or NotifyCollectionChangedAction.Replace
or NotifyCollectionChangedAction.Reset;
}

private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private class Group : List<TValue>, IGrouping<TKey, TValue>
{
public Group(KeyValuePair<TKey, IList<TValue>> keyValuePair)
Expand Down

0 comments on commit f87e2c8

Please sign in to comment.