Exemple #1
0
 public static void Reset <T>(this IChangeable <T> r, IEnumerable <T> newItems) => r.Change(ItemChangedArgs.Reset(newItems, null));
Exemple #2
0
        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);
        }