public static IDisposable connect_item_actions <T>(INotifyCollectionChanged notifier, IEnumerable <T> collection, Action <T> action)
 {
     foreach (T v in collection)
     {
         action(v);
     }
     return(notifier.Subscribe((s, e) => CollectionHelper.process_new_items(collection, action, e)));
 }
        public static IDisposable connect_set <T>(IList <T> source_list, INotifyCollectionChanged source_notifier, ISet <T> target)
        {
            return(source_notifier.Subscribe(callback));

            void callback(object sender, NotifyCollectionChangedEventArgs e)
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (T v in e.NewItems)
                    {
                        target.Add(v);
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (T v in e.OldItems)
                    {
                        target.Remove(v);
                    }
                    break;

                case NotifyCollectionChangedAction.Replace:
                    foreach (T v in e.OldItems)
                    {
                        target.Remove(v);
                    }
                    foreach (T v in e.NewItems)
                    {
                        target.Add(v);
                    }
                    break;

                case NotifyCollectionChangedAction.Reset:
                    target.Clear();
                    for (int i = 0; i < source_list.Count; i++)
                    {
                        target.Add(source_list[i]);
                    }
                    break;
                }
            }
        }
        public static IDisposable connect_sorted_distinct_mux <T, TResult>(IList <T> source_list, INotifyCollectionChanged source_notifier, IList <TResult> target, Func <T, IEnumerable <TResult> > selector, IComparer <TResult> comparer, IEqualityComparer <TResult> eq_comparer)
        {
            var ref_count = new RefCountMap <TResult>(eq_comparer);

            if (target.Count > 0)
            {
                target.Clear();
            }
            foreach (var v in source_list)
            {
                var r = selector(v);
                target.InsertSortedDistinct(r, comparer);
            }
            return(source_notifier.Subscribe(on_collection_changed));

            void on_collection_changed(object sender, NotifyCollectionChangedEventArgs e)
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    target.InsertSorted(e.NewItems.Cast <T>().SelectMany(selector).Where(ref_count.increment_adds_key), comparer);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    target.RemoveSorted(e.OldItems.Cast <T>().SelectMany(selector).Where(ref_count.decrement_removes_key), comparer);
                    break;

                case NotifyCollectionChangedAction.Replace:
                    target.RemoveSorted(e.OldItems.Cast <T>().SelectMany(selector).Where(ref_count.decrement_removes_key), comparer);
                    target.InsertSorted(e.NewItems.Cast <T>().SelectMany(selector).Where(ref_count.increment_adds_key), comparer);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    target.Clear();
                    ref_count.Clear();
                    target.InsertSorted(source_list.SelectMany(selector).Where(ref_count.increment_adds_key), comparer);
                    break;
                }
            }
        }
示例#4
0
 public static IDisposable Subscribe(this INotifyCollectionChanged collection, Action callback)
 {
     return(collection.Subscribe(_ => callback()));
 }