public static void Reset <T>(this IChangeable <T> r, IEnumerable <T> newItems) => r.Change(ItemChangedArgs.Reset(newItems, null));
public static void Update <T>(this IChangeable <T> r, T item) => r.Change(ItemChangedArgs.Update(item));
public static void Remove <T>(this IChangeable <T> r, int id) => r.Change(ItemChangedArgs.Remove <T>(id));
public static void Remove <T>(this IChangeable <T> r, T item) => r.Change(ItemChangedArgs.Remove(item));
public static void Add <T>(this IChangeable <T> r, T item) => r.Change(ItemChangedArgs.Add(item));
public void Change(ItemChangedArgs <T> args) { switch (args.Action) { case ItemChangedAction.Add: _items.Add(args.Item.Id, args.Item); break; case ItemChangedAction.Remove: if (args.Id.HasValue) { var id = args.Id.Value; T removed; if (_items.TryGetValue(id, out removed)) { args = ItemChangedArgs.Remove(removed); } _items.Remove(id); } else { _items.Remove(args.Item.Id); } break; case ItemChangedAction.Update: { T removed; if (_items.TryGetValue(args.Item.Id, out removed)) { args = ItemChangedArgs.Update(args.Item, removed); } _items[args.Item.Id] = args.Item; } break; case ItemChangedAction.Reset: { var newItems = args.NewItems; var oldItems = _items.Values; var removed = Except(oldItems, newItems); var added = Except(newItems, oldItems); _items.Clear(); if (newItems != null) { foreach (var item in newItems) { _items[item.Id] = item; } } args = ItemChangedArgs.Reset(newItems, oldItems); } break; default: throw new ArgumentException("Invalid arg.Action"); } _Changed.OnNext(this, args); }