using System.Collections; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; namespace FruitBank.Common { public interface IMgFastObservableCollection { public void AddRange(IEnumerable other); public void Replace(IEnumerable other); public void RemoveRange(IEnumerable other); public void Synchronize(NotifyCollectionChangedEventArgs args); } public interface IMgFastObservableCollection : IMgFastObservableCollection { public void Replace(IEnumerable other); public void Sort(IComparer comparer); public void SortAndReplace(IEnumerable other, IComparer comparer); } public class MgObservableCollection : ObservableCollection, IMgFastObservableCollection { private bool _suppressChangedEvent; public void Replace(IEnumerable other) { _suppressChangedEvent = true; Clear(); AddRange(other); } public void Replace(IEnumerable other) { _suppressChangedEvent = true; Clear(); foreach (T item in other) Add(item); _suppressChangedEvent = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); } public void AddRange(IEnumerable other) { _suppressChangedEvent = true; foreach (var item in other) { if (item is T tItem) Add(tItem); } _suppressChangedEvent = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); } public void RemoveRange(IEnumerable other) { _suppressChangedEvent = true; foreach (var item in other) { if (item is T tItem) Remove(tItem); } _suppressChangedEvent = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); } public void SortAndReplace(IEnumerable other, IComparer comparer) { List values = new(other); values.Sort(comparer); Replace(values); } public void Sort(IComparer comparer) { List values = new(this); values.Sort(comparer); Replace(values); } public void Synchronize(NotifyCollectionChangedEventArgs args) { switch (args.Action) { case NotifyCollectionChangedAction.Add when args.NewItems != null: AddRange(args.NewItems); break; case NotifyCollectionChangedAction.Remove when args.OldItems != null: RemoveRange(args.OldItems); break; case NotifyCollectionChangedAction.Reset: Clear(); break; case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Move: break; default: throw new ArgumentOutOfRangeException(); } } protected override void OnPropertyChanged(PropertyChangedEventArgs e) { if (_suppressChangedEvent) return; base.OnPropertyChanged(e); } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (_suppressChangedEvent) return; base.OnCollectionChanged(e); } //protected override void ClearItems() //{ // base.ClearItems(); //} //protected override void InsertItem(int index, T item) //{ // base.InsertItem(index, item); //} //protected override void MoveItem(int oldIndex, int newIndex) //{ // base.MoveItem(oldIndex, newIndex); //} //public override event NotifyCollectionChangedEventHandler? CollectionChanged //{ // add => base.CollectionChanged += value; // remove => base.CollectionChanged -= value; //} } }