protected override void OnInitialize()
        {
            base.OnInitialize();

            Bus.Listen <string>("db")
            .Where(m => m == "Reload")
            .Subscribe(_ => CatalogOffers?.Clear(), CloseCancellation.Token);

            Offers.Select(v => {
                v = v ?? new List <Offer>();
                if (IsFilterByCatalogName)
                {
                    return(v.GroupBy(g => g.GroupName)
                           .Select(g => new object[] { new GroupHeader(g.Key) }.Concat(g))
                           .SelectMany(o => o)
                           .ToList());
                }
                else
                {
                    return(v.Cast <object>().ToList());
                }
            })
            .Subscribe(DisplayItems);
            CurrentDisplayItem.OfType <Offer>().Subscribe(CurrentOffer);
            CurrentOffer.Subscribe(CurrentDisplayItem);
            //.Skip(1) - пропускаем начальные значения
            CurrentRegion.Cast <Object>().Skip(1)
            .Merge(CurrentFilter.Skip(1))
            .Merge(CurrentProducer.Skip(1))
            .Merge(HideJunk.Select(v => (object)v).Skip(1))
            .Subscribe(_ => Filter());
            DbReloadToken.SelectMany(_ => Env.RxQuery(s => {
                if (IsFilterByCatalogName)
                {
                    var catalogs = s.Query <Catalog>()
                                   .Fetch(c => c.Name)
                                   .Where(c => c.Name == filterCatalogName).ToArray();
                    var ids = catalogs.Select(c => c.Id).ToArray();

                    var result = s.Query <Offer>()
                                 .Where(o => ids.Contains(o.CatalogId))
                                 .Fetch(o => o.Price)
                                 .ToList();
                    result.Each(o => o.GroupName = catalogs.Where(c => c.Id == o.CatalogId)
                                                   .Select(c => c.FullName)
                                                   .FirstOrDefault());
                    return(result);
                }
                else
                {
                    var catalogId = filterCatalog.Id;
                    return(s.Query <Offer>().Where(o => o.CatalogId == catalogId)
                           .Fetch(o => o.Price)
                           .ToList());
                }
            })).CatchSubscribe(x => {
                CatalogOffers = x;
                UpdateFilters();
                Filter(false);
                UpdateOffers(Offers.Value);
                if (x.Count == 0)
                {
                    Manager.Warning("Нет предложений");
                    TryClose();
                }
            }, CloseCancellation);

            UpdateMaxProducers();
        }
Exemple #2
0
        protected override void OnInitialize()
        {
            base.OnInitialize();

            var obProducers = DbReloadToken
                              .SelectMany(_ => RxQuery(s => new[] { EmptyProducer }
                                                       .Concat(s.Query <Producer>()
                                                               .OrderBy(p => p.Name)
                                                               .ToList())
                                                       .ToList()));

            obProducers.Subscribe(Producers);
            obProducers.Subscribe(_ =>
            {
                var currentFilterProducerId = CurrentFilterProducer.HasValue ? CurrentFilterProducer.Value.Id : 0;
                if (CanSaveFilterProducer.Value && currentFilterProducerId > 0 &&
                    Producers.Value.Any(d => d.Id == currentFilterProducerId))
                {
                    CurrentProducer.Value = CurrentFilterProducer.Value;
                }
            });

            SearchBehavior.ActiveSearchTerm.Cast <object>()
            .Merge(Prices.Select(p => p.Changed()).Merge().Throttle(Consts.FilterUpdateTimeout, UiScheduler))
            .Merge(OnlyBase.Changed())
            .Merge(CurrentProducer.Changed())
            .Merge(HideJunk.Changed())
            .Merge(DbReloadToken)
            .Select(v => {
                ProducerFilterStateSet();

                var term = SearchBehavior.ActiveSearchTerm.Value;
                if (String.IsNullOrEmpty(term))
                {
                    return(Observable.Return(new List <Offer>()));
                }
                IsLoading.Value = true;
                return(RxQuery(s => {
                    var query = s.Query <Offer>();
                    query = Util.ContainsAny(query, o => o.ProductSynonym,
                                             term.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                    query = Util.Filter(query, o => o.Price.Id, Prices);

                    var producer = CurrentProducer.Value;
                    if (producer != null && producer.Id > 0)
                    {
                        var id = producer.Id;
                        query = query.Where(o => o.ProducerId == id);
                    }

                    if (OnlyBase)
                    {
                        query = query.Where(o => o.Price.BasePrice);
                    }
                    if (HideJunk)
                    {
                        query = query.Where(o => !o.Junk);
                    }

                    var result = SortOffers(query.Fetch(o => o.Price).ToList());
                    return result;
                }));
            })
            .Switch()
            //будь бдителен CalculateRetailCost и LoadOrderItems может вызвать обращение к базе если данные еще не загружены
            //тк синхронизация не производится загрузка должна выполняться в основной нитке
            .Do(v => {
                LoadOrderItems(v);
                Calculate(v);
                IsLoading.Value = false;
            })
            .Subscribe(Offers, CloseCancellation.Token);

            //используется в случае если нужно найти предложения по позиции отказа
            if (!String.IsNullOrEmpty(initTerm))
            {
                DbReloadToken
                .Do(_ => IsLoading.Value = true)
                .SelectMany(_ => RxQuery(s => QueryByFullText(s, initTerm)))
                .Do(v => {
                    LoadOrderItems(v);
                    Calculate(v);
                    IsLoading.Value = false;
                })
                .Subscribe(Offers, CloseCancellation.Token);
            }
        }