public RepositoryViewModel(IDispatcher dispatcher, IFactory <Profile> profileFactory) { UndoCommand = new ReactiveCommand(); AddCommand = new ReactiveCommand <bool>(); NameChangedCommand = new ReactiveCommand <string>(); SelectedContentChangedCommand = new ReactiveCommand <object>(); this.dispatcher = dispatcher; //Items = new ReactiveCollection<SelectDeleteItem>(Enumerable.Range(0, 10).Select(a => new SelectDeleteItem()).ToObservable()); ; //var undos = Items.ToCollectionChanged().Select(a => a.Value.Undo.Where(a => a).Select(b => new KeyValuePair<string, SelectDeleteItem>("Undo", a.Value))).SelectMany(a => a); var deletes = Items.ToCollectionChanged().Select(a => a.Value.Delete.Where(c => c).Select(b => new KeyValuePair <string, SelectDeleteItem>("Delete", a.Value))).SelectMany(a => a); var selects = Items.ToCollectionChanged().Select(a => a.Value.Select /*.Where(a => a)*/.Select(b => new KeyValuePair <bool, SelectDeleteItem>(b, a.Value))).SelectMany(a => a); var selects2 = Items.ToCollectionChanged().Where(a => Items.Count == 1).Select(a => Items.Single()).Select(b => new KeyValuePair <bool, SelectDeleteItem>(true, b)); var selects3 = Items.ToCollectionChanged().Where(a => Items.Count == 0 && a.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) .Select(a => a.Value).Select(b => new KeyValuePair <bool, SelectDeleteItem>(true, b)); //selects2.DelaySubscription(TimeSpan.FromSeconds(1)).Subscribe(a => //{ //}); //selects3.Subscribe(a => //{ //}); this.Deletes = deletes.Select(a => a.Value).ToReactiveCollection(); deletes.Select(a => a.Value).Subscribe(a => Items.Remove(a)); (UndoCommand as ReactiveCommand).Subscribe(a => { if (Deletes.Any()) { var last = Deletes.Last(); Deletes.Remove(last); last.Delete.Value = false; last.Select.Value = Items.Count() == 0; Items.Add(last); } }); //(AddCommand as ReactiveCommand<bool>).Subscribe(a => //{ // bool parameter = (bool)a; // if (!Equals(parameter, true)) return; // //if (!string.IsNullOrWhiteSpace(FruitTextBox.Text)) // // FruitListBox.Items.Add(FruitTextBox.Text.Trim()); //}); (AddCommand as ReactiveCommand <bool>) .Where(b => b) .WithLatestFrom((NameChangedCommand as ReactiveCommand <string>), (bl, text) => text) .Subscribe(text => { Items.Add(new SelectDeleteItem(profileFactory.Build(text))); }); SelectedItem = selects.Merge(selects2).Where(a => a.Key).Select(a => a.Value).ToReactiveProperty(); //mode: ReactivePropertyMode.DistinctUntilChanged selects.Subscribe(a => { if (a.Key) { foreach (var x in Items.Where(b => b != a.Value)) { x.Select.Value = false; } } else if (a.Key == false && SelectedItem.Value == a.Value && Items.Count != 1) { this.SelectedItem.Value = null; } }); SelectedItem.Subscribe(a => { //this.dispatcher.InvokeAsync(() => this.SelectedItem.Value = a); }); // For some reason doesn't pick up initial selecteditem SelectedItem.Where(a => a != null).Take(1).Subscribe(a => { this.dispatcher.InvokeAsync(() => this.SelectedItem.Value = a); }); (SelectedContentChangedCommand as ReactiveCommand <object>).Subscribe(a => { (SelectedItem.Value.Value as Profile).Steps = (a as Dictionary <object, int>).Select(ba => ba.Key).Cast <Step>().ToArray(); }); }
//public void LoadFromXml(XmlElement xml) //{ // Inserts.Clear(); // Updates.Clear(); // Deletes.Clear(); // foreach(XmlElement xitem in xml.SelectNodes("Insert")) // { // var item = new ChangeSetInsertItem(); // item.LoadFromXml(xitem); // Inserts.Add(item); // } //} public bool HasChanges() { return(Updates.Any() || Inserts.Any() || Deletes.Any()); }