Пример #1
0
        public PagedDataViewer(ITradeService tradeService, ISchedulerProvider schedulerProvider)
        {
            //build observable predicate from search text
            var filter = this.WhenValueChanged(t => t.SearchText)
                         .Throttle(TimeSpan.FromMilliseconds(250))
                         .Select(BuildFilter);

            //build observable sort comparer
            var sort = SortParameters.WhenValueChanged(t => t.SelectedItem)
                       .Select(prop => prop.Comparer)
                       .ObserveOn(schedulerProvider.Background);

            //build observable comparer
            var currentPageChanged = PageParameters.WhenValueChanged(p => p.CurrentPage);
            var pageSizeChanged    = PageParameters.WhenValueChanged(p => p.PageSize);
            var pager = currentPageChanged.CombineLatest(pageSizeChanged, (page, size) => new PageRequest(page, size))
                        .StartWith(new PageRequest(1, 25))
                        .DistinctUntilChanged()
                        .Sample(TimeSpan.FromMilliseconds(100));

            // filter, sort, page and bind to observable collection
            _cleanUp = tradeService.All.Connect()
                       .Filter(filter) // apply user filter
                       .Transform(trade => new TradeProxy(trade), new ParallelisationOptions(ParallelType.Ordered, 5))
                       .Sort(sort, SortOptimisations.ComparesImmutableValuesOnly)
                       .Page(pager)
                       .ObserveOn(schedulerProvider.MainThread)
                       .Do(changes => PageParameters.Update(changes.Response))
                       .Bind(out _data) // update observable collection bindings
                       .DisposeMany()   // dispose when no longer required
                       .Subscribe();
        }
Пример #2
0
        public PagedDataViewer(ITradeService tradeService, ISchedulerProvider schedulerProvider)
        {
            //watch for filter changes and change filter
            var filterController = new FilterController <Trade>(trade => true);
            var filterApplier    = this.WhenValueChanged(t => t.SearchText)
                                   .Throttle(TimeSpan.FromMilliseconds(250))
                                   .Select(BuildFilter)
                                   .Subscribe(filterController.Change);

            //watch for changes to sort and apply when necessary
            var sortContoller = new SortController <TradeProxy>(SortExpressionComparer <TradeProxy> .Ascending(proxy => proxy.Id));
            var sortChange    = SortParameters.WhenValueChanged(t => t.SelectedItem).Select(prop => prop.Comparer)
                                .ObserveOn(schedulerProvider.TaskPool)
                                .Subscribe(sortContoller.Change);

            //watch for page changes and change filter
            var pageController     = new PageController();
            var currentPageChanged = PageParameters.WhenValueChanged(p => p.CurrentPage);
            var pageSizeChanged    = PageParameters.WhenValueChanged(p => p.PageSize);
            var pageChanger        = currentPageChanged.CombineLatest(pageSizeChanged, (page, size) => new PageRequest(page, size))
                                     .DistinctUntilChanged()
                                     .Sample(TimeSpan.FromMilliseconds(100))
                                     .Subscribe(pageController.Change);

            // filter, sort, page and bind to loaded data
            var loader = tradeService.All.Connect()
                         .Filter(filterController) // apply user filter
                         .Transform(trade => new TradeProxy(trade), new ParallelisationOptions(ParallelType.Ordered, 5))
                         .Sort(sortContoller, SortOptimisations.ComparesImmutableValuesOnly)
                         .Page(pageController)
                         .ObserveOn(schedulerProvider.MainThread)
                         .Do(changes => _pageParameters.Update(changes.Response))
                         .Bind(out _data) // update observable collection bindings
                         .DisposeMany()   //since TradeProxy is disposable dispose when no longer required
                         .Subscribe();

            _cleanUp = new CompositeDisposable(loader, filterController, filterApplier, sortChange, sortContoller, pageChanger, pageController);
        }