Esempio n. 1
0
        public virtual void ReplacePrimaryColor(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var swatch = new SwatchesProvider().Swatches.FirstOrDefault(
                s => string.Compare(s.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0);

            if (swatch == null)
            {
                throw new ArgumentException($"No such swatch '{name}'", nameof(name));
            }

            ReplacePrimaryColor(swatch);
        }
        public SearchOptionsViewModel(ISearchMetadataCollection metadataCollection, 
            ISchedulerProvider schedulerProvider,
            SearchHints searchHints)
        {
            SearchHints = searchHints;
            //TODO: options for colour

            var swatches = new SwatchesProvider().Swatches;

            bool binding = false;

            var orderChanged = Observable.FromEventPattern<OrderChangedEventArgs>(
                                            h => PositionMonitor.OrderChanged += h,
                                            h => PositionMonitor.OrderChanged -= h)
                                    .Select(evt => evt.EventArgs)
                                    .Where(args=>args.PreviousOrder!=null && args.NewOrder.Length == args.PreviousOrder.Length)
                                    .Select(positionChangedArgs =>
                                    {
                                            //reprioritise filters and highlights
                                            return positionChangedArgs.NewOrder
                                            .OfType<SearchOptionsProxy>()
                                            .Select((item, index) => new {Meta=(SearchMetadata)item, index})
                                            //.Where(x => x.index != x.Meta.Position)
                                            .Select(x => new SearchMetadata(x.Meta, x.index))
                                            .ToArray();
                                    })
                                    .Subscribe(positionChangedArgs =>
                                    {
                                       
                                        positionChangedArgs.ForEach(metadataCollection.AddorUpdate);
                                    });

            ReadOnlyObservableCollection<SearchOptionsProxy> data;

            var userOptions = metadataCollection.Metadata.Connect()
                .WhereReasonsAre(ChangeReason.Add, ChangeReason.Remove) //ignore updates because we update from here
                .Transform(meta => new SearchOptionsProxy(meta, swatches, m => metadataCollection.Remove(m.SearchText)))
                .SubscribeMany(so =>
                {
                    //when a value changes, write the original value back to the cache
                    return so.WhenAnyPropertyChanged()

                        .Subscribe(_ =>metadataCollection.AddorUpdate(new SearchMetadata(metadataCollection.Metadata.Count,
                                    so.Text, so.Filter, so.Highlight, so.UseRegex, so.IgnoreCase)));
                })
                .Sort(SortExpressionComparer<SearchOptionsProxy>.Ascending(proxy => proxy.Position))

                .ObserveOn(schedulerProvider.MainThread)
                .Bind(out data)
                .Subscribe();
            
            Data = data;

            //command to add the current search to the tail collection
            var searchInvoker = SearchHints.SearchRequested.Subscribe(request =>
            {
                schedulerProvider.Background.Schedule(() =>
                {
                    metadataCollection.AddorUpdate(new SearchMetadata(metadataCollection.NextIndex(), request.Text, false, true, request.UseRegEx, true));
                });
            });
        
            
            _cleanUp = new CompositeDisposable(searchInvoker, 
                userOptions, 
                searchInvoker,
                orderChanged);
        }