Example #1
0
        protected override void OnLambdaValueChanged(LambdaValueChangedEventArgs <TSource, IEnumerable <TResult> > args)
        {
            // unsubscribe from old value
            UnsubscribeItem(args.OldValue);
            items.Remove(args.OldValue);

            // subscribe to new value
            items.Add(args.NewValue);
            SubscribeItem(args.NewValue);

            var oldValues = args.OldValue.Except(args.NewValue).ToList();
            var newValues = args.NewValue.Except(args.OldValue).ToList();

            if (oldValues.Count == 0 && newValues.Count >= 1)
            {
                NotifyCollectionChangedUtil.RaiseAddEvent <TResult>(OnCollectionChanged, newValues);
            }
            else if (oldValues.Count >= 1 && newValues.Count == 0)
            {
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TResult>(OnCollectionChanged, oldValues);
            }
            else if (oldValues.Count >= 1 && newValues.Count >= 1)
            {
                NotifyCollectionChangedUtil.RaiseReplaceEvent <TResult>(OnCollectionChanged, oldValues, newValues);
            }
        }
Example #2
0
        protected override void OnPredicateCollectionChanged(NotifyCollectionChangedEventArgs args)
        {
            switch (args.Action)
            {
#if !SILVERLIGHT
            case NotifyCollectionChangedAction.Move:
#endif
            case NotifyCollectionChangedAction.Replace:
            case NotifyCollectionChangedAction.Reset:
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                break;

            case NotifyCollectionChangedAction.Add:
                var newItems = args.NewItems.Cast <LambdaOperation <bool> >().Where(i => i.Value).Select(i => Predicates[i]).ToList();
                if (newItems.Count > 0)
                {
                    NotifyCollectionChangedUtil.RaiseAddEvent <TElement>(OnCollectionChanged, newItems);
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                var oldItems = args.OldItems.Cast <LambdaOperation <bool> >().Where(i => i.Value).Select(i => Predicates[i]).ToList();
                if (oldItems.Count > 0)
                {
                    NotifyCollectionChangedUtil.RaiseRemoveEvent <TElement>(OnCollectionChanged, oldItems);
                }
                break;
            }
        }
Example #3
0
        protected override void OnSourceCollectionItemsRemoved(IEnumerable <TElement> oldItems, int startingIndex)
        {
            var oldTrack = new List <TElement>();

            foreach (var item in oldItems)
            {
                if (counts.GetOrDefault(item) == 0)
                {
                    // item did not exist, ignore
                    continue;
                }
                else if (counts.GetOrDefault(item) == 1)
                {
                    // item has been removed
                    counts.Remove(item);
                    oldTrack.Add(item);
                }
                else
                {
                    // decrease count
                    counts[item]--;
                }
            }

            if (oldTrack.Count > 0)
            {
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TElement>(OnCollectionChanged, oldTrack);
            }
        }
Example #4
0
        protected override void OnSourceCollectionReset()
        {
            var oldItems = counts.Keys.ToList();
            var newItems = Source;

            var newTrack = new List <TElement>();
            var oldTrack = new List <TElement>();

            foreach (var item in newItems)
            {
                if (counts.GetOrDefault(item) == 0)
                {
                    // item did not exist before, we've just added it
                    counts[item] = 1;
                    newTrack.Add(item);
                }
                else
                {
                    // item already existed, increase it's count
                    counts[item]++;
                }
            }

            foreach (var item in oldItems)
            {
                if (counts.GetOrDefault(item) == 0)
                {
                    // value never existed in the first place
                    continue;
                }
                else if (counts.GetOrDefault(item) == 1)
                {
                    // item had one count, removed for good
                    counts.Remove(item);
                    oldTrack.Add(item);
                }
                else
                {
                    // decrease count
                    counts[item]--;
                }
            }

            if (newTrack.Count > 0 && oldTrack.Count > 0)
            {
                // both new and old items exist
                NotifyCollectionChangedUtil.RaiseReplaceEvent <TElement>(OnCollectionChanged, oldTrack, newTrack);
            }
            else if (newTrack.Count > 0 && oldTrack.Count == 0)
            {
                // only new items were added
                NotifyCollectionChangedUtil.RaiseAddEvent <TElement>(OnCollectionChanged, newTrack);
            }
            else if (newTrack.Count == 0 && oldTrack.Count > 0)
            {
                // only items were removed
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TElement>(OnCollectionChanged, oldTrack);
            }
        }
Example #5
0
        protected override void OnLambdaCollectionItemsRemoved(IEnumerable <LambdaOperation <IEnumerable <TResult> > > oldItems, int startingIndex)
        {
            // unsubscribe from oldl items
            foreach (var oldItem in oldItems.Select(i => i.Value))
            {
                UnsubscribeItem(oldItem);
                items.Remove(oldItem);
            }

            NotifyCollectionChangedUtil.RaiseRemoveEvent <TResult>(OnCollectionChanged, oldItems.SelectMany(i => i.Value));
        }
Example #6
0
        protected override void OnLambdaCollectionItemsAdded(IEnumerable <LambdaOperation <IEnumerable <TResult> > > newItems, int startingIndex)
        {
            // subscribe to new items
            foreach (var newItem in newItems.Select(i => i.Value))
            {
                items.Add(newItem);
                SubscribeItem(newItem);
            }

            NotifyCollectionChangedUtil.RaiseAddEvent <TResult>(OnCollectionChanged, newItems.SelectMany(i => i.Value));
        }
Example #7
0
        protected override void OnLambdaValueChanged(LambdaValueChangedEventArgs <TSource, TResult> args)
        {
            var oldValue = (TResult)args.OldValue;
            var newValue = (TResult)args.NewValue;

            // single value has been replaced
            if (!object.Equals(oldValue, newValue))
            {
                NotifyCollectionChangedUtil.RaiseReplaceEvent <TResult>(RaiseCollectionChanged, oldValue, newValue);
            }
        }
Example #8
0
        protected override void OnSource2CollectionItemsRemoved(IEnumerable <TSource> oldItems, int startingIndex)
        {
            foreach (var oldItem in oldItems)
            {
                source2Lookup.Remove(oldItem);
            }

            var matched = oldItems.Where(i => sourceLookup.Contains(i)).ToList();

            if (matched.Any())
            {
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TSource>(OnCollectionChanged, matched);
            }
        }
Example #9
0
        protected override void OnSourceCollectionItemsAdded(IEnumerable <TSource> newItems, int startingIndex)
        {
            foreach (var newItem in newItems)
            {
                sourceLookup.Add(newItem);
            }

            var matched = newItems.Where(i => source2Lookup.Contains(i)).ToList();

            if (matched.Any())
            {
                NotifyCollectionChangedUtil.RaiseAddEvent <TSource>(OnCollectionChanged, matched);
            }
        }
Example #10
0
 protected override void OnPredicateValueChanged(LambdaValueChangedEventArgs <TElement, bool> args)
 {
     if (!args.OldValue && args.NewValue)
     {
         // was false, now true: item added
         NotifyCollectionChangedUtil.RaiseAddEvent <TElement>(OnCollectionChanged, args.Item);
     }
     else if (args.OldValue && !args.NewValue)
     {
         // was true, now false: item removed
         NotifyCollectionChangedUtil.RaiseRemoveEvent <TElement>(OnCollectionChanged, args.Item);
     }
     ;
 }
Example #11
0
        /// <summary>
        /// Remoes the specified item with the given key.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="key"></param>
        void RemoveItemWithKey(TElement item, TKey key)
        {
            // group associated with key
            var group = GetOrCreateGroup(key);

            // remove item from group
            group.Remove(item);

            // if group is empty, remove group
            if (group.Count == 0)
            {
                groups.Remove(key);
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TElement>(RaiseCollectionChanged, group);
            }
        }
Example #12
0
        /// <summary>
        /// Gets or creates a grouping for the specified key value.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Grouping <TKey, TElement> GetOrCreateGroup(TKey key)
        {
            // does group already exist?
            var group = groups.GetOrDefault(key);

            if (group != null)
            {
                return(group);
            }

            // create new group
            group = groups[key] = new Grouping <TKey, TElement>(key);
            NotifyCollectionChangedUtil.RaiseAddEvent <TElement>(RaiseCollectionChanged, group);
            return(group);
        }
Example #13
0
        void item_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
        {
            switch (args.Action)
            {
#if !SILVERLIGHT && !PCL
            case NotifyCollectionChangedAction.Move:
#endif
            case NotifyCollectionChangedAction.Replace:
            case NotifyCollectionChangedAction.Reset:
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                break;

            case NotifyCollectionChangedAction.Add:
                NotifyCollectionChangedUtil.RaiseAddEvent <TResult>(OnCollectionChanged, args.NewItems.Cast <TResult>());
                break;

            case NotifyCollectionChangedAction.Remove:
                NotifyCollectionChangedUtil.RaiseRemoveEvent <TResult>(OnCollectionChanged, args.OldItems.Cast <TResult>());
                break;
            }
        }
Example #14
0
        protected override void OnSourceCollectionItemsAdded(IEnumerable <TElement> newItems, int startingIndex)
        {
            var newTrack = new List <TElement>();

            foreach (var item in newItems)
            {
                if (counts.GetOrDefault(item) == 0)
                {
                    // item did not exist, added
                    counts.GetOrCreate(item, i => 1);
                    newTrack.Add(item);
                }
                else
                {
                    // item already existed, increase count
                    counts[item]++;
                }
            }

            if (newTrack.Count > 0)
            {
                NotifyCollectionChangedUtil.RaiseAddEvent <TElement>(OnCollectionChanged, newTrack);
            }
        }