public void ItemRemoved() { var list = new RxList<string>("1", "2"); string item = null; list.ItemRemoved.Subscribe(x => item = x); list.Remove("1"); Assert.AreEqual("1", item); list.Remove("2"); Assert.AreEqual("2", item); list = new RxList<string>("1", "2"); list.ItemRemoved.Subscribe(x => item = x); list.Remove("2"); Assert.AreEqual("2", item); list.Remove("1"); Assert.AreEqual("1", item); }
public static void SetUpSync <TViewModel, TModel>( this RxList <TViewModel> list, DbRepository repository, RxList <TModel> modelList, Func <TViewModel, TModel> newModelObject ) where TViewModel : IIdObject, IRxObject where TModel : IdObject { Action <TViewModel, TModel> bind = (viewModel, model) => { viewModel.Changed.Where(y => y.Property.Name != nameof(IdObject.Id)).Subscribe(y => { model.GetType().GetProperty(y.Property.Name)?.SetValue(model, y.NewValue); }); }; var modelsById = modelList.ToDictionary(x => x.Id); foreach (var item in list) { bind(item, modelsById[item.Id]); } list.ItemAdded.Subscribe(async viewModel => { viewModel.ObservePropertyChange(y => y.Id).SubscribeOnce(y => viewModel.Id = y); var newModel = newModelObject(viewModel); bind(viewModel, newModel); modelList.Add(newModel); await repository.WaitForIdle(); }); list.ItemRemoved.Subscribe(viewModel => { var model = modelList.Single(x => x.Id == viewModel.Id); modelList.Remove(model); }); }
public void DeriveRemovesOnRemoved() { var list = new RxList<string>("1", "2", "3"); int removed = -1; list.Derive(x => int.Parse(x), x => removed = x); list.Remove("2"); Assert.AreEqual(2, removed); }
public void Removed() { var list = new RxList<string>("1", "2"); var item = new RxListItem<string>(); list.Removed.Subscribe(x => item = x); list.Remove("1"); Assert.AreEqual(0, item.Index); Assert.AreEqual("1", item.Value); list.Remove("2"); Assert.AreEqual(0, item.Index); Assert.AreEqual("2", item.Value); list = new RxList<string>("1", "2"); list.Removed.Subscribe(x => item = x); list.Remove("2"); Assert.AreEqual(1, item.Index); Assert.AreEqual("2", item.Value); list.Remove("1"); Assert.AreEqual(0, item.Index); Assert.AreEqual("1", item.Value); }
public void ObserveElement() { var one = new TestModel { StringProperty = "1" }; var two = new TestModel { StringProperty = "2" }; var list = new RxList<TestModel>(one, two); string s = null; list.ObserveElementProperty(x => x.StringProperty).Subscribe(x => s = x.Value); list[0].StringProperty = "foo"; Assert.AreEqual("foo", s); list.Remove(one); one.StringProperty = "bar"; Assert.AreEqual("foo", s); list[0] = new TestModel { StringProperty = "3" }; Assert.AreEqual("3", s); }