Skip to content

Commit

Permalink
Добавление и удаление элементов из MtObservableCollection не вызывает…
Browse files Browse the repository at this point in the history
… перегрузку всей коллекции.
  • Loading branch information
Фёдор Шведов committed Mar 21, 2016
1 parent 5c3c124 commit 5220bbc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 64 deletions.
22 changes: 11 additions & 11 deletions src/MyToolkit/Collections/MtObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace MyToolkit.Collections
/// <typeparam name="T"></typeparam>
public class MtObservableCollection<T> : ObservableCollection<T>
{
private List<T> _oldCollection = null;
private List<T> _oldCollection;
private event EventHandler<MtNotifyCollectionChangedEventArgs<T>> _extendedCollectionChanged;

/// <summary>Initializes a new instance of the <see cref="MtObservableCollection{T}"/> class.</summary>
Expand All @@ -45,31 +45,31 @@ public MtObservableCollection(IEnumerable<T> collection) : base(collection) { }
/// <summary>Adds multiple items to the collection. </summary>
/// <param name="collection">The items to add. </param>
/// <exception cref="ArgumentNullException">The value of 'collection' cannot be null. </exception>
public void AddRange(IEnumerable<T> collection)
public void AddRange(IList<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
throw new ArgumentNullException(nameof(collection));

foreach (var item in collection)
Items.Add(item);

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (System.Collections.IList)collection));
}

/// <summary>Removes multiple items from the collection. </summary>
/// <param name="collection">The items to remove. </param>
/// <exception cref="ArgumentNullException">The value of 'collection' cannot be null. </exception>
public void RemoveRange(IEnumerable<T> collection)
public void RemoveRange(IList<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
throw new ArgumentNullException(nameof(collection));

foreach (var item in collection.ToList())
Items.Remove(item);

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (System.Collections.IList)collection));
}

/// <summary>Resets the whole collection with a given list. </summary>
Expand All @@ -78,13 +78,13 @@ public void RemoveRange(IEnumerable<T> collection)
public void Initialize(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
throw new ArgumentNullException(nameof(collection));

Items.Clear();
foreach (var i in collection)
Items.Add(i);

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

Expand Down
6 changes: 2 additions & 4 deletions src/MyToolkit/Collections/ObservableCollectionObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public void Initialize(ObservableCollection<T> collection)
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
var copy = ItemChanged;
if (copy != null)
copy(sender, args);
copy?.Invoke(sender, args);
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
Expand All @@ -92,8 +91,7 @@ private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs
}

var copy = CollectionChanged;
if (copy != null)
copy(sender, new MtNotifyCollectionChangedEventArgs<T>(addedItems, removedItems, null));
copy?.Invoke(sender, new MtNotifyCollectionChangedEventArgs<T>(addedItems, removedItems, null));
}
}
}
44 changes: 15 additions & 29 deletions src/MyToolkit/Collections/ObservableCollectionViewBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public abstract class ObservableCollectionViewBase<TItem> : IList<TItem>, IDispo
private readonly Dictionary<INotifyPropertyChanged, PropertyChangedEventHandler> _events =
new Dictionary<INotifyPropertyChanged, PropertyChangedEventHandler>();

private bool _isTracking = false;
private bool _isTracking;
private bool _trackItemChanges;
private bool _trackCollectionChanges = false;

private readonly object _syncRoot = new object();
private bool _trackCollectionChanges;

/// <summary>Initializes a new instance of the <see cref="ObservableCollectionViewBase{TItem}"/> class. </summary>
protected ObservableCollectionViewBase()
Expand Down Expand Up @@ -125,13 +123,14 @@ public bool TrackItemChanges
/// <summary>Adds a multiple elements to the underlying collection. </summary>
/// <param name="items">The items to add. </param>
[Obsolete("Use methods on Items property instead. 9/20/2014")]
public void AddRange(IEnumerable<TItem> items)
public void AddRange(IList<TItem> items)
{
var old = TrackCollectionChanges;
TrackCollectionChanges = false;

if (Items is MtObservableCollection<TItem>)
((MtObservableCollection<TItem>)Items).AddRange(items);
var collection = Items as MtObservableCollection<TItem>;
if (collection != null)
collection.AddRange(items);
else
{
foreach (var i in items)
Expand Down Expand Up @@ -241,9 +240,10 @@ private void DeregisterEvent(INotifyPropertyChanged item)

private void TrackCollection()
{
if (Items is ObservableCollection<TItem>)
var items = Items as ObservableCollection<TItem>;
if (items != null)
{
var collection = (ObservableCollection<TItem>)Items;
var collection = items;
_itemsChangedHandler = WeakEvent.RegisterEvent<ObservableCollectionViewBase<TItem>, NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
this,
h => collection.CollectionChanged += h,
Expand Down Expand Up @@ -283,15 +283,13 @@ private void UntrackAllItems()
private void OnInternalCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var copy = CollectionChanged;
if (copy != null)
copy(this, e);
copy?.Invoke(this, e);
}

private void OnInternalPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var copy = PropertyChanged;
if (copy != null)
copy(this, e);
copy?.Invoke(this, e);
}

public int Count
Expand Down Expand Up @@ -347,10 +345,7 @@ public bool Contains(TItem item)
return _internalCollection.Contains(item);
}

public bool IsReadOnly
{
get { return true; }
}
public bool IsReadOnly => true;

public bool Contains(object value)
{
Expand All @@ -367,20 +362,11 @@ public int IndexOf(object value)
return _internalCollection.IndexOf((TItem)value);
}

public bool IsFixedSize
{
get { return false; }
}
public bool IsFixedSize => false;

public bool IsSynchronized
{
get { return true; }
}
public bool IsSynchronized => true;

public object SyncRoot
{
get { return _syncRoot; }
}
public object SyncRoot { get; } = new object();

public void CopyTo(Array array, int index)
{
Expand Down
31 changes: 11 additions & 20 deletions src/MyToolkit/Utilities/EntityContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ namespace MyToolkit.Utilities
/// <typeparam name="TIdentity">The type of the identity.</typeparam>
public class EntityContainer<TEntity, TIdentity> where TEntity : class, IEntity<TIdentity>
{
private readonly MtObservableCollection<TEntity> _collection = new MtObservableCollection<TEntity>();

/// <summary>Gets the entity collection.</summary>
public MtObservableCollection<TEntity> Collection
{
get { return _collection; }
}
public MtObservableCollection<TEntity> Collection { get; } = new MtObservableCollection<TEntity>();

/// <summary>Gets an entity by ID.</summary>
/// <returns>The entity.</returns>
public TEntity Get(TIdentity id)
{
return _collection.SingleOrDefault(n => Equals(n.Id, id));
return Collection.SingleOrDefault(n => Equals(n.Id, id));
}

/// <summary>Initializes the container with some entities.</summary>
Expand All @@ -43,16 +38,12 @@ public void Clear()

/// <summary>Adds multiple entities.</summary>
/// <param name="items">The entities.</param>
public void AddRange(IEnumerable<TEntity> items)
public void AddRange(IList<TEntity> items)
{
foreach (var item in items)
{
var c = Get(item.Id);
if (c != null)
_collection.Remove(c);
}
foreach (var c in items.Select(item => Get(item.Id)).Where(c => c != null))
Collection.Remove(c);

_collection.AddRange(items);
Collection.AddRange(items);
}

/// <summary>Inserts an entity.</summary>
Expand All @@ -62,8 +53,8 @@ public void Insert(int position, TEntity item)
{
var entity = Get(item.Id);
if (entity != null)
_collection.Remove(entity);
_collection.Insert(position, item);
Collection.Remove(entity);
Collection.Insert(position, item);
}

/// <summary>Adds an entity at the end of the collection.</summary>
Expand All @@ -72,8 +63,8 @@ public void Add(TEntity item)
{
var c = Get(item.Id);
if (c != null)
_collection.Remove(c);
_collection.Add(item);
Collection.Remove(c);
Collection.Add(item);
}

/// <summary>Removes an entity from the collection.</summary>
Expand All @@ -86,7 +77,7 @@ public void Remove(TEntity item)
if (c is IDisposable)
((IDisposable)c).Dispose();

_collection.Remove(c);
Collection.Remove(c);
}
else if (item is IDisposable)
((IDisposable)item).Dispose();
Expand Down

0 comments on commit 5220bbc

Please sign in to comment.