public static ICompositeList <T> Where <T>
     (this ICompositeList <T> @this, IObservable <Func <T, bool> > predicateObservable)
 {
     return(@this
            .Items
            .CombineLatest(predicateObservable, (items, predicate) => items.Where(predicate))
            .ToCompositeList());
 }
        /// <summary>
        /// A version of 'Where' where the predicate is allowed to return an
        /// IObservable of bool.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="@this"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static ICompositeList <T> Where <T>
            (this ICompositeList <T> @this, Func <T, IObservable <bool> > predicate) =>

        @this
        .SelectMany
            (item =>
            predicate(item)
            .Select(r => r ? new [] { item } : new T [] {})
            .ToCompositeList()
            );
 /// <summary>
 /// Generates an observable for items removed from the list. Note
 /// that duplicates are ignored.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source"></param>
 /// <returns></returns>
 public static IObservable <ImmutableHashSet <T> > RemovedFromSetObservable <T>
     (this ICompositeList <T> source)
 {
     return(source
            .Items
            .StartWith(ImmutableList <T> .Empty)
            .Buffer(2, 1).Where(b => b.Count == 2)
            .Select(b => b[0].Except(b[1]).ToImmutableHashSet())
            .Where(c => c.Count > 0));
 }
        public static ICompositeList <TResult> Select <TSource, TResult>
            (this ICompositeList <TSource> m
            , Func <TSource, TResult> f
            )
        {
            var r = m
                    .Items
                    .Select(p => p.Select(f).ToImmutableList())
                    .Select(p => new CompositeList <TResult>(Observable.Return(p, Scheduler.Immediate)));

            return(r.ToCompositeList());
        }
 public static IObservable <List <DiffElement <T> > > ChangesObservable <T>(this ICompositeList <T> source, IEqualityComparer <T> comparer = null)
 {
     return(source
            .Items
            .StartWith(ImmutableList <T> .Empty)
            .Buffer(2, 1).Where(b => b.Count == 2)
            .Select(b =>
     {
         var sections = Diff.CalculateSections(b[0], b[1], comparer);
         var alignment = Diff.AlignElements
                             (b[0], b[1], sections, new BasicReplaceInsertDeleteDiffElementAligner <T>());
         return alignment.ToList();
     }));
 }
Ejemplo n.º 6
0
        internal ReadOnlyObservableCollection(ICompositeList <T> list, System.Collections.ObjectModel.ObservableCollection <T> collection, IEqualityComparer <T> eq) : base(collection)
        {
            _List       = list;
            _Collection = collection;

            _Disposable = list.ChangesObservable(eq)
                          .Subscribe(change =>
            {
                int i = 0;
                foreach (var diff in change)
                {
                    switch (diff.Operation)
                    {
                    case DiffOperation.Match:
                        break;

                    case DiffOperation.Insert:
                        _Collection.Insert(i, diff.ElementFromCollection2.Value);
                        break;

                    case DiffOperation.Delete:
                        _Collection.RemoveAt(i);
                        i--;
                        break;

                    case DiffOperation.Replace:
                        _Collection[i] = diff.ElementFromCollection2.Value;
                        break;

                    case DiffOperation.Modify:
                        _Collection[i] = diff.ElementFromCollection2.Value;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    i++;
                }
            });
        }
 public static ICompositeList <TResult> SelectMany <TSource, TResult>
     (this ICompositeList <TSource> m
     , Func <TSource, IEnumerable <TResult> > f
     ) => m.Bind(v => new CompositeList <TResult>(f(v)));
 public static CompositeListSubscription <T> Subscribe <T>
     (this ICompositeList <T> @this) => new CompositeListSubscription <T>(@this);
 public static ICompositeList <TResult> SelectMany <TSource, TResult>
     (this ICompositeList <TSource> m
     , Func <TSource, ICompositeList <TResult> > f
     ) => m.Bind(f);
 public static IObservable <float> Min <T>
 (
     this ICompositeList <T> source,
     Func <T, float> aggregator) => source.Items.Select(items => items.Min(aggregator));
 public static IObservable <T> Min <T> (this ICompositeList <T> source) => source.Items.Select(items => items.Min());
 public static IObservable <bool> All <T>
     (this ICompositeList <T> source,
     Func <T, bool> pred) => source.Items.Select(items => items.All(pred));
 public static IObservable <int> Sum <T>
 (
     this ICompositeList <T> source,
     Func <T, int> aggregator) => source.Items.Select(items => items.Sum(aggregator));
Ejemplo n.º 14
0
        /// <summary>
        /// Use this to supply a ReadOnly ObservableCollection for view lists etc.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="eq"></param>
        /// <returns></returns>
        public static ReadOnlyObservableCollection <T> CreateObservableCollection <T>(this ICompositeList <T> list, IEqualityComparer <T> eq)
        {
            var collection = new System.Collections.ObjectModel.ObservableCollection <T>();

            return(new ReadOnlyObservableCollection <T>(list, collection, eq));
        }
 public static IObservable <U> Aggregate <T, U>
 (
     this ICompositeList <T> source,
     Func <U, T, U> aggregator, U init) => source.Items.Select(items => items.Aggregate(init, aggregator));
 public static ICompositeList <TResult> SelectMany <TSource, TICompositeList, TResult>
     (this ICompositeList <TSource> m
     , Func <TSource, ICompositeList <TICompositeList> > f
     , Func <TSource, TICompositeList, TResult> g
     ) => m.Bind(x => f(x).Bind(y => new CompositeList <TResult>(g(x, y))));
 public CompositeListSubscription(ICompositeList <T> list)
 {
     _Subscription = list.Items
                     .Where(v => v != null)
                     .Subscribe(v => Items = v);
 }
 public static IDisposable Bind <T>
     (this ICompositeList <T> @this,
     CompositeSourceList <T> target) =>
 @this.Items.Subscribe(items => target.Source = items);
 public static ICompositeList <T> Concat <T>
     (this ICompositeList <T> @this,
     ICompositeList <T> other) => @this.Items.CombineLatest(other.Items, (a, b) => a.Concat(b)).ToCompositeList();
 public static ICompositeList <T> Where <T>
     (this ICompositeList <T> @this, Func <T, bool> predicate) =>
 @this.SelectMany(v => predicate(v) ? new[] { v }: new T[] {});
Ejemplo n.º 21
0
        protected IDisposable CreateComboBox <T, TModel>(
            IPropertyManagerPageGroup @group,
            string caption,
            string tip,
            ICompositeList <T> items,
            IEqualityComparer <T> itemEqualityComparer,
            Func <T, string> itemToStringFn,
            TModel model,
            Expression <Func <TModel, T> > selectedItemExpression,
            Action <IPropertyManagerPageCombobox> config = null)
        {
            var id       = NextId();
            var comboBox = @group.CreateComboBox(id, caption, tip);

            config?.Invoke(comboBox);

            // Sync source collection with SW ComboBox collection
            Action <short, T> insert = (index, element) =>
            {
                var insertIndex = comboBox.InsertItem(index, itemToStringFn(element));
                Debug.Assert(insertIndex != -1, "Item couldn't be inserted");
            };

            Action <short> delete = index =>
            {
                var deleteIndex = comboBox.DeleteItem(index);
                Debug.Assert(deleteIndex != -1, "Item couldn't be deleted");
            };

            var swComboBoxUpdated = new Subject <Unit>();
            var d0 = AfterActivationObs
                     .Select(_ => items.ChangesObservable(itemEqualityComparer))
                     .Switch()
                     .Subscribe(changes =>
            {
                short index = 0;
                foreach (var change in changes)
                {
                    switch (change.Operation)
                    {
                    case DiffOperation.Match:
                        break;

                    case DiffOperation.Insert:
                        insert(index++, change.ElementFromCollection2.Value);
                        break;

                    case DiffOperation.Delete:
                        delete(index);
                        break;

                    case DiffOperation.Replace:
                        delete(index);
                        insert(index++, change.ElementFromCollection2.Value);
                        break;

                    case DiffOperation.Modify:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                swComboBoxUpdated.OnNext(Unit.Default);
            });

            // Sync source to SW selection
            var d1 = swComboBoxUpdated
                     .Select(_ => model.WhenAnyValue(selectedItemExpression))
                     .Switch()
                     .Select(selectedItem => items
                             .Items
                             .Select(list => (short)list.IndexOf(selectedItem))
                             )
                     .Switch()
                     .AssignTo(comboBox, box => box.CurrentSelection);

            // Sync SW to source selection
            var selectedItemProxy = selectedItemExpression.GetProxy(model);
            var d2 = ComboBoxSelectionObservable(id)
                     .Select(index => items
                             .Items
                             .Select(list => list[index])
                             )
                     .Switch()
                     .AssignTo(selectedItemProxy, p => p.Value);

            return(ControlHolder.Create(@group, comboBox, d0, d1, d2, swComboBoxUpdated));
        }
 public static IObservable <T> Aggregate <T>
 (
     this ICompositeList <T> source,
     Func <T, T, T> aggregator) => source.Items.Select(items => items.Aggregate(aggregator));