Esempio n. 1
0
        /// <summary>
        /// ステージされたデータをすべて破棄し、Itemsを再構築します。
        /// 初期要素数はinitialSizeになります。
        /// </summary>
        /// <returns></returns>
        public async Task ResetCollection()
        {
            if (_prevCancelToken != null)
            {
                _prevCancelToken.Cancel();
                _prevCancelToken.Dispose();
            }
            try
            {
                _prevCancelToken = new CancellationTokenSource();
                await UpdateProxyAsync(_prevCancelToken.Token);
            }
            catch (TaskCanceledException)
            {
                return;
            }

            _collectionChangedTrigger.OnNext(CollectionChanged <T> .Reset);
            int i = 0;

            foreach (var item in _proxy.Take(_initialSize).ToArray())
            {
                _collectionChangedTrigger.OnNext(CollectionChanged <T> .Add(i++, item));
            }
            CollectionReset?.Invoke(this, EventArgs.Empty);
            _prevCancelToken.Dispose();
            _prevCancelToken = null;
        }
Esempio n. 2
0
        public MainWindowViewModel()
        {
            this.SimpleAddCommand = new ReactiveCommand();
            this.SimpleAddCommand.Subscribe(_ =>
            {
                this.source.OnNext(random.Next());
            });
            this.SimpleResetCommand = new ReactiveCommand();

            // IObservaboe<T>からコレクションへ変換。オプションとしてコレクションをリセットするIO<Unit>を渡せる
            this.SimpleCollection = this.source.ToReadOnlyReactiveCollection(this.SimpleResetCommand.ToUnit());

            this.CollectionChangedCollection = this.collectionChangedSource.ToReadOnlyReactiveCollection();
            this.CollectionChangedAddCommand = new ReactiveCommand();
            this.CollectionChangedAddCommand.Subscribe(_ =>
            {
                this.collectionChangedSource.OnNext(
                    CollectionChanged <int> .Add(0, random.Next()));
            });

            this.CollectionChangedClearCommand = new ReactiveCommand();
            this.CollectionChangedClearCommand.Subscribe(_ =>
            {
                this.collectionChangedSource.OnNext(
                    CollectionChanged <int> .Reset);
            });

            this.CollectionChangedRemoveCommand = new ReactiveCommand();
            this.CollectionChangedRemoveCommand.Subscribe(_ =>
            {
                this.collectionChangedSource.OnNext(
                    CollectionChanged <int> .Remove(0));
            });

            // ObservableCollection<T>からReadOnlyObservableCollection<U>への変換
            this.ObservableCollectionToReadOnlyReactiveCollection = this.sourceCollection
                                                                    .ToReadOnlyReactiveCollection(x => x + " value.");

            this.SourceCollectionAddCommand = new ReactiveCommand();
            this.SourceCollectionAddCommand.Subscribe(_ =>
            {
                this.sourceCollection.Add(random.Next().ToString());
            });
            this.SourceCollectionRemoveCommand = new ReactiveCommand();
            this.SourceCollectionRemoveCommand.Subscribe(_ =>
            {
                this.sourceCollection.RemoveAt(this.sourceCollection.Count - 1);
            });
            this.SourceCollectionResetCommand = new ReactiveCommand();
            this.SourceCollectionResetCommand.Subscribe(_ =>
            {
                this.sourceCollection.Clear();
            });
        }
Esempio n. 3
0
 public HidariueService()
 {
     this.AddCommand   = new ReactiveCommand <Hidariue>();
     this.ResetCommand = new ReactiveCommand();
     //this.AddCommand = new ReactiveCommand<Hidariue>();
     this.DeleteCommand = new ReactiveCommand <Hidariue>();
     this.Hidariues     = Observable.Merge(
         this.AddCommand
         .Select(h => CollectionChanged <Hidariue> .Add(0, h)),
         this.ResetCommand
         .Select(_ => CollectionChanged <Hidariue> .Reset),
         //this.UpdateCommand
         //    .Select(h => CollectionChanged<Hidariue>.Replace(Hidariues.IndexOf(h), h)),
         this.DeleteCommand
         .Select(h => CollectionChanged <Hidariue> .Remove(Hidariues.IndexOf(h), h))
         ).ToReadOnlyReactiveCollection <Hidariue>();
 }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="scheduler"></param>
        /// <param name="disposeElement"></param>
        /// <returns></returns>
        public static ReadOnlyReactiveCollection <T> ToReadOnlyReactiveCollection <T>(this IObservable <T[]> self, IScheduler scheduler = null, bool disposeElement = true)
        {
            return(self
                   .Select(o =>
            {
                var reset = new[]
                {
                    CollectionChanged <T> .Reset,
                };
                var add = o?.Select((oo, ii) => CollectionChanged <T> .Add(ii, oo)) ?? Enumerable.Empty <CollectionChanged <T> >();

                return reset.Concat(add);
            })
                   .Select(o => o.ToObservable())
                   .Switch()
                   .ToReadOnlyReactiveCollection(scheduler, disposeElement));
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="self"></param>
        /// <param name="converter"></param>
        /// <param name="scheduler"></param>
        /// <param name="disposeElement"></param>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public static ReadOnlyReactiveCollection <U> ToReadOnlyReactiveCollection <T, U>(this IObservable <T[]> self, Func <T[], U[]> converter, IScheduler scheduler = null, bool disposeElement = true)
        {
            var collectionChanged = self
                                    .Select(o =>
            {
                var reset = new[]
                {
                    CollectionChanged <U> .Reset,
                };
                var add = converter(o)?.Select((oo, ii) => CollectionChanged <U> .Add(ii, oo)) ?? Enumerable.Empty <CollectionChanged <U> >();

                return(reset.Concat(add));
            })
                                    .Select(o => o.ToObservable())
                                    .Switch();

            return(Enumerable.Empty <U>()
                   .ToReadOnlyReactiveCollection(collectionChanged, scheduler, disposeElement));
        }
Esempio n. 6
0
        /// <summary>
        /// Proxyからn分のデータをItemsにステージします。
        /// </summary>
        /// <param name="n"></param>
        /// <returns>追加があればtrue</returns>
        public bool Stage(int n)
        {
            var currentIndex = Items.Count;
            var fixedProxy   = _proxy.ToArray();
            var result       = false;

            for (int i = 0; i < n; ++i)
            {
                var index = currentIndex + i;
                if (fixedProxy.Length <= index)
                {
                    break;
                }
                result = true;
                _collectionChangedTrigger.OnNext(
                    CollectionChanged <T> .Add(index, fixedProxy[index]));
            }

            return(result);
        }
Esempio n. 7
0
        public void CollectionChangedTest()
        {
            var s = new Subject <CollectionChanged <int> >();

            var target = s.ToReadOnlyReactiveCollection();

            target.Count.Is(0);
            s.OnNext(CollectionChanged <int> .Add(0, 10));
            s.OnNext(CollectionChanged <int> .Add(1, 2));

            target.Is(10, 2);

            s.OnNext(CollectionChanged <int> .Remove(0));
            target.Is(2);
            s.OnNext(CollectionChanged <int> .Add(1, 3));
            target.Is(2, 3);
            s.OnNext(CollectionChanged <int> .Replace(1, 100));
            target.Is(2, 100);
            s.OnNext(CollectionChanged <int> .Reset);
            target.Count.Is(0);
        }
    public void ToCollectionChangedTest()
    {
        var source = new ObservableCollection <string>();
        var target = source.ToReadOnlyReactiveCollection();
        var values = new List <CollectionChanged <string> >();

        target.ToCollectionChanged().Subscribe(x => values.Add(x));

        source.Add("abc");
        source.Add("def");
        source.Remove("abc");
        source.Clear();
        values.Is(
            new[]
        {
            CollectionChanged <string> .Add(0, "abc"),
            CollectionChanged <string> .Add(1, "def"),
            CollectionChanged <string> .Remove(0, "abc"),
            CollectionChanged <string> .Reset,
        },
            (x, y) => x.Action == y.Action && x.Value == y.Value);
    }
        public static IObservable <CollectionChanged <T> > ObserveHistory <T>(this IObservable <T> observable, int maxHistoryCount)
        {
            int count = 0;

            return(observable.SelectMany(value =>
            {
                if (count < maxHistoryCount)
                {
                    return new[]
                    {
                        CollectionChanged <T> .Add(count++, value)
                    };
                }
                else
                {
                    return new[]
                    {
                        CollectionChanged <T> .Remove(0, default(T)),
                        CollectionChanged <T> .Add(maxHistoryCount - 1, value),
                    };
                }
            }));
        }