|
1 | 1 | using System; |
2 | 2 | using System.Collections; |
3 | 3 | using System.Collections.Generic; |
| 4 | +using System.Collections.Specialized; |
| 5 | +using System.ComponentModel; |
4 | 6 | using System.Linq; |
5 | 7 |
|
6 | 8 | namespace Float.Core.Collections |
@@ -28,6 +30,11 @@ public FilterCollection(IEnumerable<IFilter<T>> filterOptions) |
28 | 30 | options = new ObservableElementCollection<IFilter<T>>(filterOptions); |
29 | 31 | options.ChildPropertyChanged += HandleOptionChanged; |
30 | 32 | options.CollectionChanged += HandleOptionChanged; |
| 33 | + |
| 34 | + foreach (var filter in filterOptions) |
| 35 | + { |
| 36 | + filter.FilterChanged += HandleFilterChanged; |
| 37 | + } |
31 | 38 | } |
32 | 39 |
|
33 | 40 | /// <summary> |
@@ -109,15 +116,65 @@ IEnumerator<IFilter<T>> IEnumerable<IFilter<T>>.GetEnumerator() |
109 | 116 | return options.GetEnumerator(); |
110 | 117 | } |
111 | 118 |
|
| 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 | + |
112 | 150 | /// <summary> |
113 | 151 | /// Invoked when the filter options have changed. |
114 | 152 | /// </summary> |
115 | 153 | /// <param name="sender">The initiator of the change.</param> |
116 | 154 | /// <param name="args">Contains the proeprty that was changed.</param> |
117 | 155 | protected virtual void HandleOptionChanged(object sender, EventArgs args) |
118 | 156 | { |
| 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 | + |
119 | 165 | NotifyPropertyChanged(nameof(Options)); |
120 | 166 | NotifyFilterChanged(); |
121 | 167 | } |
| 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 | + } |
122 | 179 | } |
123 | 180 | } |
0 commit comments