| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CollectionTrackingBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.Specialized; |
| | | 10 | | using System.Reflection; |
| | | 11 | | using System.Threading; |
| | | 12 | | using MyNet.Reflection; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Observable.Behaviors; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Tracks changes on collections of an ObservableObject. This behavior allows you to track changes on collections that |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class CollectionTrackingBehavior : SuspendableBehavior<ObservableObject> |
| | | 20 | | { |
| | 72 | 21 | | private readonly HashSet<string> _trackedProperties = new(StringComparer.Ordinal); |
| | 72 | 22 | | private readonly HashSet<INotifyCollectionChanged> _trackedCollections = []; |
| | 72 | 23 | | private readonly Lock _gate = new(); |
| | | 24 | | private int _initialTrackScheduled; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="CollectionTrackingBehavior"/> class, which tracks changes on collec |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="owner">The ObservableObject whose collections will be tracked.</param> |
| | | 30 | | /// <exception cref="ArgumentNullException">Thrown if the owner parameter is null.</exception> |
| | | 31 | | public CollectionTrackingBehavior(ObservableObject owner) |
| | 72 | 32 | | : base(owner) |
| | | 33 | | { |
| | 72 | 34 | | TrackExistingCollections(); |
| | 72 | 35 | | ScheduleDeferredInitialTrack(); |
| | 72 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Occurs when a tracked collection changes. |
| | | 40 | | /// </summary> |
| | | 41 | | public event EventHandler<NotifyCollectionChangedEventArgs>? CollectionChanged; |
| | | 42 | | |
| | | 43 | | #region Tracking |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Registers a collection manually. |
| | | 47 | | /// </summary> |
| | | 48 | | public void Track(INotifyCollectionChanged collection) |
| | | 49 | | { |
| | 9 | 50 | | if (IsDisposed) |
| | 0 | 51 | | return; |
| | | 52 | | |
| | 9 | 53 | | if (IsSuspended) |
| | 0 | 54 | | return; |
| | | 55 | | |
| | | 56 | | lock (_gate) |
| | | 57 | | { |
| | 9 | 58 | | if (!_trackedCollections.Add(collection)) |
| | 0 | 59 | | return; |
| | | 60 | | |
| | 9 | 61 | | collection.CollectionChanged += OnCollectionChanged; |
| | 9 | 62 | | } |
| | 9 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Unregisters a collection manually. |
| | | 67 | | /// </summary> |
| | | 68 | | public void Untrack(INotifyCollectionChanged collection) |
| | 3 | 69 | | { |
| | | 70 | | lock (_gate) |
| | | 71 | | { |
| | 3 | 72 | | if (!_trackedCollections.Remove(collection)) |
| | 0 | 73 | | return; |
| | | 74 | | |
| | 3 | 75 | | collection.CollectionChanged -= OnCollectionChanged; |
| | 3 | 76 | | } |
| | 3 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Tracks observable collections exposed by readable owner properties that are already initialized. |
| | | 81 | | /// </summary> |
| | | 82 | | internal void TrackExistingCollections() |
| | | 83 | | { |
| | 150 | 84 | | if (IsDisposed) |
| | 0 | 85 | | return; |
| | | 86 | | |
| | 4134 | 87 | | foreach (var property in Owner.GetType().GetPublicProperties()) |
| | 1917 | 88 | | TryTrackProperty(property); |
| | 150 | 89 | | } |
| | | 90 | | |
| | | 91 | | private void TryTrackProperty(PropertyInfo property) |
| | | 92 | | { |
| | 1917 | 93 | | if (!typeof(INotifyCollectionChanged).IsAssignableFrom(property.PropertyType)) |
| | 1878 | 94 | | return; |
| | | 95 | | |
| | 39 | 96 | | if (_trackedProperties.Contains(property.Name)) |
| | 6 | 97 | | return; |
| | | 98 | | |
| | 33 | 99 | | if (!PropertyValueAccess.TryGetValue(Owner, property, out var value)) |
| | 30 | 100 | | return; |
| | | 101 | | |
| | 3 | 102 | | if (value is not INotifyCollectionChanged collection) |
| | 0 | 103 | | return; |
| | | 104 | | |
| | 3 | 105 | | _trackedProperties.Add(property.Name); |
| | 3 | 106 | | Track(collection); |
| | 3 | 107 | | } |
| | | 108 | | |
| | | 109 | | private void ScheduleDeferredInitialTrack() |
| | | 110 | | { |
| | 72 | 111 | | if (Interlocked.Exchange(ref _initialTrackScheduled, 1) != 0) |
| | 0 | 112 | | return; |
| | | 113 | | |
| | 72 | 114 | | var context = SynchronizationContext.Current; |
| | 72 | 115 | | if (context is not null) |
| | | 116 | | { |
| | 72 | 117 | | context.Post(static state => ((CollectionTrackingBehavior)state!).DeferredInitialTrack(), this); |
| | 72 | 118 | | return; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | ThreadPool.QueueUserWorkItem(static state => ((CollectionTrackingBehavior)state!).DeferredInitialTrack(), this); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | private void DeferredInitialTrack() |
| | | 125 | | { |
| | 72 | 126 | | if (IsDisposed) |
| | 0 | 127 | | return; |
| | | 128 | | |
| | 72 | 129 | | TrackExistingCollections(); |
| | 72 | 130 | | } |
| | | 131 | | |
| | | 132 | | #endregion |
| | | 133 | | |
| | | 134 | | #region Event handling |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Handles the CollectionChanged event for tracked collections. This method is called when a collection that implem |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="sender">The collection that changed.</param> |
| | | 140 | | /// <param name="e">The event arguments describing the change.</param> |
| | | 141 | | private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| | | 142 | | { |
| | 6 | 143 | | if (IsDisposed || IsSuspended) |
| | 0 | 144 | | return; |
| | | 145 | | |
| | 6 | 146 | | TrackExistingCollections(); |
| | 6 | 147 | | CollectionChanged?.Invoke(sender, e); |
| | 6 | 148 | | } |
| | | 149 | | |
| | | 150 | | #endregion |
| | | 151 | | |
| | | 152 | | #region IDisposable |
| | | 153 | | |
| | | 154 | | /// <inheritdoc/> |
| | | 155 | | protected override void DisposeManagedResources() |
| | 3 | 156 | | { |
| | | 157 | | lock (_gate) |
| | | 158 | | { |
| | 6 | 159 | | foreach (var collection in _trackedCollections) |
| | 0 | 160 | | collection.CollectionChanged -= OnCollectionChanged; |
| | | 161 | | |
| | 3 | 162 | | _trackedCollections.Clear(); |
| | 3 | 163 | | _trackedProperties.Clear(); |
| | 3 | 164 | | } |
| | | 165 | | |
| | 3 | 166 | | base.DisposeManagedResources(); |
| | 3 | 167 | | } |
| | | 168 | | |
| | | 169 | | #endregion |
| | | 170 | | } |
| | | 171 | | |