Beispiel #1
0
        public override INotificationResult Notify(IList <INotificationResult> sources)
        {
            var notification = CollectionChangedNotificationResult <INotifyGrouping <TKey, TItem> > .Create(this);

            var added   = notification.AddedItems;
            var removed = notification.RemovedItems;

            foreach (var change in sources)
            {
                if (change.Source == source)
                {
                    var sourceChange = (ICollectionChangedNotificationResult <TItem>)change;
                    if (sourceChange.IsReset)
                    {
                        OnDetach();
                        OnAttach();
                        OnCleared();
                        notification.TurnIntoReset();
                        return(notification);
                    }
                    else
                    {
                        NotifySource(sourceChange, added, removed);
                    }
                }
                else
                {
                    var keyChange = (IValueChangedNotificationResult <TKey>)change;
                    var tagged    = (TaggedObservableValue <TKey, TItem>)keyChange.Source;

                    ObservableGroup <TKey, TItem> group;
                    if (groups.TryGetValue(keyChange.OldValue, out group))
                    {
                        group.Items.Remove(tagged.Tag);
                        if (group.Count == 0)
                        {
                            groups.Remove(keyChange.OldValue);
                            removed.Add(group);
                        }
                    }

                    if (!groups.TryGetValue(keyChange.NewValue, out group))
                    {
                        group = new ObservableGroup <TKey, TItem>(keyChange.NewValue);
                        groups.Add(keyChange.NewValue, group);
                        added.Add(group);
                    }
                    group.Items.Add(tagged.Tag);
                }
            }

            if (added.Count == 0 && removed.Count == 0)
            {
                return(UnchangedNotificationResult.Instance);
            }

            OnRemoveItems(removed.Cast <ObservableGroup <TKey, TItem> >());
            OnAddItems(added.Cast <ObservableGroup <TKey, TItem> >());
            return(notification);
        }
Beispiel #2
0
        private void AttachItem(TItem item, INotifyValue <TKey> key)
        {
            var           entry = new Entry(key, item, this);
            Stack <Entry> entryStack;

            if (!keys.TryGetValue(item, out entryStack))
            {
                entryStack = new Stack <Entry>();
                keys.Add(item, entryStack);
            }
            entryStack.Push(entry);
            var add = false;
            ObservableGroup <TKey, TItem> group;

            if (!groups.TryGetValue(key.Value, out group))
            {
                group = new ObservableGroup <TKey, TItem>(key.Value);
                groups.Add(key.Value, group);
                add = true;
            }
            group.ItemsInternal.Add(item);
            key.ValueChanged += entry.KeyChanged;
            if (add)
            {
                OnAddItem(group);
            }
        }
Beispiel #3
0
 public override bool Contains(ObservableGroup <TKey, TItem> item)
 {
     if (item != null)
     {
         return(groups.ContainsValue(item));
     }
     else
     {
         return(false);
     }
 }
Beispiel #4
0
        private bool AttachItem(TItem item)
        {
            var key = keySelector.InvokeTagged(item, item);

            key.Successors.Set(this);
            keys[item] = key;

            var add = false;
            ObservableGroup <TKey, TItem> group;

            if (!groups.TryGetValue(key.Value, out group))
            {
                group = new ObservableGroup <TKey, TItem>(key.Value);
                groups.Add(key.Value, group);
                add = true;
            }
            group.Items.Add(item);

            return(add);
        }
Beispiel #5
0
            public void KeyChanged(object sender, ValueChangedEventArgs e)
            {
                ObservableGroup <TKey, TItem> group;
                var oldKey = (TKey)e.OldValue;

                if (Parent.groups.TryGetValue(oldKey, out group))
                {
                    group.ItemsInternal.Remove(Item);
                    if (group.Count == 0)
                    {
                        Parent.groups.Remove(oldKey);
                        Parent.OnRemoveItem(group);
                    }
                }
                var newKey = (TKey)e.NewValue;

                if (!Parent.groups.TryGetValue(newKey, out group))
                {
                    group = new ObservableGroup <TKey, TItem>(newKey);
                    Parent.groups.Add(newKey, group);
                    Parent.OnAddItem(group);
                }
                group.ItemsInternal.Add(Item);
            }