diff --git a/Float.Core/Collections/FilterCollection.cs b/Float.Core/Collections/FilterCollection.cs index 0826540..0d8e05b 100644 --- a/Float.Core/Collections/FilterCollection.cs +++ b/Float.Core/Collections/FilterCollection.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.Specialized; +using System.ComponentModel; using System.Linq; namespace Float.Core.Collections @@ -28,6 +30,11 @@ public FilterCollection(IEnumerable> filterOptions) options = new ObservableElementCollection>(filterOptions); options.ChildPropertyChanged += HandleOptionChanged; options.CollectionChanged += HandleOptionChanged; + + foreach (var filter in filterOptions) + { + filter.FilterChanged += HandleFilterChanged; + } } /// @@ -109,6 +116,37 @@ IEnumerator> IEnumerable>.GetEnumerator() return options.GetEnumerator(); } + /// + /// Invoked when filter options are added or removed. + /// + /// The options collection. + /// The change event to the collection. + protected virtual void HandleOptionsChanged(object sender, NotifyCollectionChangedEventArgs args) + { + if (sender is not IEnumerable) + { + return; + } + + if (args.OldItems is IEnumerable> oldFilters) + { + foreach (var filter in oldFilters) + { + filter.FilterChanged -= HandleFilterChanged; + } + } + + var newItems = args.NewItems ?? sender; + if (newItems is IEnumerable> newFilters) + { + foreach (var filter in newFilters) + { + filter.FilterChanged -= HandleFilterChanged; + filter.FilterChanged += HandleFilterChanged; + } + } + } + /// /// Invoked when the filter options have changed. /// @@ -116,8 +154,27 @@ IEnumerator> IEnumerable>.GetEnumerator() /// Contains the proeprty that was changed. protected virtual void HandleOptionChanged(object sender, EventArgs args) { + if (args is PropertyChangedEventArgs eventArgs && eventArgs.PropertyName == nameof(IFilter.IsEnabled)) + { + // If the only think that changed was an `IsEnabled` property + // then there's nothing to do since `HandleFilterChanged` + // will take care of bubbling up the notification of the filter change. + return; + } + NotifyPropertyChanged(nameof(Options)); NotifyFilterChanged(); } + + /// + /// Invoked when one of the filters in the collection changes. + /// + /// The filter that changed. + /// The event. + protected virtual void HandleFilterChanged(object sender, EventArgs args) + { + // When a filter in the collection changes, then the collection changes. + NotifyFilterChanged(); + } } }