Skip to content

Commit 0336f02

Browse files
authored
FilterCollection now triggers FilterChanged event (#50)
Previously `FilterCollection` would not trigger a `FilterChanged` event even when one of the contained filters changed.
1 parent 963b93b commit 0336f02

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Float.Core/Collections/FilterCollection.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Collections.Specialized;
5+
using System.ComponentModel;
46
using System.Linq;
57

68
namespace Float.Core.Collections
@@ -28,6 +30,11 @@ public FilterCollection(IEnumerable<IFilter<T>> filterOptions)
2830
options = new ObservableElementCollection<IFilter<T>>(filterOptions);
2931
options.ChildPropertyChanged += HandleOptionChanged;
3032
options.CollectionChanged += HandleOptionChanged;
33+
34+
foreach (var filter in filterOptions)
35+
{
36+
filter.FilterChanged += HandleFilterChanged;
37+
}
3138
}
3239

3340
/// <summary>
@@ -109,15 +116,65 @@ IEnumerator<IFilter<T>> IEnumerable<IFilter<T>>.GetEnumerator()
109116
return options.GetEnumerator();
110117
}
111118

119+
/// <summary>
120+
/// Invoked when filter options are added or removed.
121+
/// </summary>
122+
/// <param name="sender">The options collection.</param>
123+
/// <param name="args">The change event to the collection.</param>
124+
protected virtual void HandleOptionsChanged(object sender, NotifyCollectionChangedEventArgs args)
125+
{
126+
if (sender is not IEnumerable)
127+
{
128+
return;
129+
}
130+
131+
if (args.OldItems is IEnumerable<IFilter<T>> oldFilters)
132+
{
133+
foreach (var filter in oldFilters)
134+
{
135+
filter.FilterChanged -= HandleFilterChanged;
136+
}
137+
}
138+
139+
var newItems = args.NewItems ?? sender;
140+
if (newItems is IEnumerable<IFilter<T>> newFilters)
141+
{
142+
foreach (var filter in newFilters)
143+
{
144+
filter.FilterChanged -= HandleFilterChanged;
145+
filter.FilterChanged += HandleFilterChanged;
146+
}
147+
}
148+
}
149+
112150
/// <summary>
113151
/// Invoked when the filter options have changed.
114152
/// </summary>
115153
/// <param name="sender">The initiator of the change.</param>
116154
/// <param name="args">Contains the proeprty that was changed.</param>
117155
protected virtual void HandleOptionChanged(object sender, EventArgs args)
118156
{
157+
if (args is PropertyChangedEventArgs eventArgs && eventArgs.PropertyName == nameof(IFilter<T>.IsEnabled))
158+
{
159+
// If the only think that changed was an `IsEnabled` property
160+
// then there's nothing to do since `HandleFilterChanged`
161+
// will take care of bubbling up the notification of the filter change.
162+
return;
163+
}
164+
119165
NotifyPropertyChanged(nameof(Options));
120166
NotifyFilterChanged();
121167
}
168+
169+
/// <summary>
170+
/// Invoked when one of the filters in the collection changes.
171+
/// </summary>
172+
/// <param name="sender">The filter that changed.</param>
173+
/// <param name="args">The event.</param>
174+
protected virtual void HandleFilterChanged(object sender, EventArgs args)
175+
{
176+
// When a filter in the collection changes, then the collection changes.
177+
NotifyFilterChanged();
178+
}
122179
}
123180
}

0 commit comments

Comments
 (0)