public static void TestObservableCollection() { var owner = new BindableBase(); var oc = new ObservableCollection <string>(); var obsc = MvvmRx.ObserveCollectionChanges(owner, oc); var values = MvvmRx.ObserveCollectionValues(owner, oc); obsc.Subscribe(val => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(val.args.AsString()); }); values.Subscribe(val => { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"[{string.Join<string>(", ", val)}]"); }); oc.Add("A"); oc.Add("B"); oc.Add("C"); oc.Add("D"); oc.Move(1, 3); oc[2] = "E"; oc.RemoveAt(2); oc.Clear(); }
public SelectManyDemoVm() { Items = new ObservableCollection <ItemVm>().AllDisposedBy(this); SelectedIds = new ObservableCollection <string> { "2", "5", "8", "1111", "11115", "22229", "StamItem" }; Initialize(); SelectionCommand = MvvmRx.CreateCommand <IEnumerable <String> >(this); RandomFiveCommand = MvvmRx.CreateCommand(this); RemoveSelected = MvvmRx.CreateCommand <IEnumerable>(this); ResetCommand = MvvmRx.CreateCommand(this); ClearCommand = MvvmRx.CreateCommand(this); SelectionCommand.Select(ienum => ienum.ToObservableCollection()).ApplyOnProperty(this, x => x.SelectedIds); RandomFiveCommand.Subscribe(_ => { var rnd = new Random(); var items = Enumerable.Range(0, 5).Select(__ => Items[rnd.Next(Items.Count)].Uid); SelectedIds.AddRange(items); }).DisposedBy(this); RemoveSelected.Subscribe(x => { foreach (var item in x.OfType <string>().ToArray()) { SelectedIds.Remove(item); } }).DisposedBy(this); ResetCommand.Subscribe(_ => { var rnd = new Random(); var ids = Items.Select(x => x.Uid).Where(x => rnd.Next(40) < 2).ToObservableCollection(); SelectedIds = ids; }).DisposedBy(this); ClearCommand.Subscribe(_ => { Items.Clear(); }).DisposedBy(this); }
public void ConnectToStore <T>(ReduxStore <T> store) where T : class, IImmutable, new() { var rootSelector = Selectors.CreateSelector((T state) => state); var onHistory = store.ObserveHistory() .WithLatestFrom(store.Select(rootSelector), (history, state) => (history, state)) .Select(pair => _selectHistoryRecords(pair.history, pair.state)); var onLatestEnsureHistory = EnsureManager .GetHistory() .Select(history => history.ToImmutableDictionary(session => session.Action)); onLatestEnsureHistory .Subscribe(val => _ensureSessions = val) .DisposedBy(this); MvvmRx.ApplyOnCollection(onHistory, this, Records, factory: Resolver.Resolve <HistoryRecordVm>, syncer: (model, vm) => vm.ReadModel(model, _ensureSessions), onRemove: vm => vm.Dispose()); var onCurrentHistoryRecord = Observable.CombineLatest( onHistory, MvvmRx.ObservePropertyValues(this, x => x.SelectedItem), (records, index) => (records, index)) .Select(pair => _pure_getSelectedHistoryRecord(pair.records, pair.index)) .Where(record => record != null); onCurrentHistoryRecord .Select(record => _pure_createJsonHierarchy(record.Action)) .ApplyOnProperty(this, x => x.Action); onCurrentHistoryRecord .Select(record => _pure_createJsonHierarchy(record.NextState)) .ApplyOnProperty(this, x => x.State); onCurrentHistoryRecord .Subscribe(record => Differ.ReadModel(record.PreviousState.ToJson(), record.NextState.ToJson())) .DisposedBy(this); var onEnsureSession = Observable.CombineLatest(onLatestEnsureHistory, onCurrentHistoryRecord, (history, record) => _pure_getEnsureSessionHistory(record, history)); onEnsureSession .Select(session => session?.Items ?.Select(item => Resolver.Resolve <EnsureItemVm>().ReadModel(item)) ?.ToObservableCollection() ?? new ObservableCollection <EnsureItemVm>() ) .ApplyOnProperty(this, x => x.EnsureItems); var onEnsureItem = Observable.CombineLatest( onEnsureSession, MvvmRx.ObservePropertyValues(this, x => x.SelectedEnsureItem), (session, index) => (session, index)) .Select(pair => _pure_getSelectedEnsureItem(pair.session, pair.index)); onEnsureItem.Select(item => _pure_createJsonHierarchy(item?.Context.Entity)) .ApplyOnProperty(this, x => x.EnsureItemState); onEnsureItem .Subscribe(item => EnsureDiffer.ReadModel(item?.Before.ToJson(), item?.After.ToJson())) .DisposedBy(this); }
public static void Run() { _container = new UnityContainer(); _container.RegisterType <IResolver, UnityResolver>(new ContainerControlledLifetimeManager()); _vm = _container.Resolve <ViewModel>(); _vm.Items.CollectionChanged += (s, e) => { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"Collection Changed: {e.AsString()}"); }; MvvmRx.ObserveMany(_vm, _vm.Items, item => item.DoMath, (item, number) => $"item: {item.Uid}, number: {number}") .Subscribe(val => { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(val); }).DisposedBy(_vm); Console.WriteLine(_model.ToJson()); _subj = new BehaviorSubject <ImmutableList <ItemModel> >(ImmutableList <ItemModel> .Empty); _vm.Initialize(_subj); _subj.ObserveDiff(m => m.Uid) .Subscribe(diff => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(diff.ToJson()); }).DisposedBy(_vm); _do(ImmutableList.Create( new ItemModel("1", "First", "Odd"), new ItemModel("2", "Second", "Even"), new ItemModel("3", "Third", "Odd"), new ItemModel("4", "Fourth", "Even"), new ItemModel("5", "Fifth", "Odd"))); var cmd3 = _vm.Items[2].DoMath; // 1, 2, 5, 4 _do(_model .RemoveAt(2) .Move(2, 3)); _vm.Items[1].DoMath.Execute(100); _vm.Items.Last().DoMath.Execute(200); // this should not do anything becuase the command is not in the loop anymore // since the owner of the command is disposed, this shoud throw an error try { cmd3.Execute(300); } catch (ObjectDisposedException ex) { Console.WriteLine("Thrown object disposed exception as expected"); } // 6, 4 _do(_model .SetItem(1, new ItemModel("6", "Sixth", "Even")) .Add(new ItemModel("7", "Seventh", "Odd")) .RemoveAll(m => m.Category == "Odd")); // 6*, 4 _do(_model .SetItem(0, _model[0].With(x => x.DisplayName, "New Sixth").With(x => x.Category, "Edited")) .RemoveAt(1) .Insert(0, new ItemModel("4", "New Fourth", "Edited"))); _vm.AddItem.Subscribe(x => { _do(_model .Add(new ItemModel((++_counter).ToString(), "Item " + _counter, "Category " + _counter))); }, () => Console.WriteLine("Command Completed") ); _vm.RemoveItem.Subscribe(id => { _do(_model.RemoveAll(x => x.Uid == id)); }); _vm.AddItem.Execute(null); _vm.AddItem.Execute(null); _vm.RemoveItem.Execute("4"); MvvmRx.ObservePropertyChanges(_vm, x => x.Caption) .Subscribe(x => Console.WriteLine($"Property Caption changed from {x.oldValue} to {x.newValue}"), () => Console.WriteLine("Property observer completed")); // property changes observable should fire two events _vm.Caption = "A"; _vm.Caption = "B"; _vm.Dispose(); }
public ViewModel() { Items = new ObservableCollection <ItemVm>().AllDisposedBy(this); AddItem = MvvmRx.CreateCommand(this); RemoveItem = MvvmRx.CreateCommand <string>(this); }
public ItemVm() { DoMath = MvvmRx.CreateCommand <int>(this); }