public void ItemAdded() { var list = new RxList<string>(); string item = null; list.ItemAdded.Subscribe(x => item = x); list.Add("1"); Assert.AreEqual("1", item); Assert.AreEqual("1", list[0]); list.Add("2"); Assert.AreEqual("2", item); Assert.AreEqual("2", list[1]); }
public void Added() { var list = new RxList<string>(); var item = new RxListItem<string>(); list.Added.Subscribe(x => item = x); list.Add("1"); Assert.AreEqual(0, item.Index); Assert.AreEqual("1", item.Value); Assert.AreEqual("1", list[0]); list.Add("2"); Assert.AreEqual(1, item.Index); Assert.AreEqual("2", item.Value); Assert.AreEqual("2", list[1]); }
public void DeriveWithSelectorAdd() { var list = new RxList<string>("1"); var derived = list.Derive(x => int.Parse(x)); list.Add("2"); Assert.AreEqual(1, derived[0]); Assert.AreEqual(2, derived[1]); }
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 MainWindowModel(Func <SelectFileType, string, string, string> selectFile) { this.selectFile = selectFile; var canAddChild = this.ObserveProperty(x => x.SelectedItem).Select(x => x is ApiCollectionModel); AddApi = RxFunction.CreateAsync(async() => await OnAddApi(null)); AddApiCollection = RxFunction.CreateAsync(async() => await OnAddApiCollection(null)); AddChildApi = RxFunction.CreateAsync(async() => await OnAddApi((ApiCollectionModel)SelectedItem), canAddChild); AddChildApiCollection = RxFunction.CreateAsync(async() => await OnAddApiCollection((ApiCollectionModel)SelectedItem), canAddChild); Export = RxCommand.Create(OnExport); Import = RxCommand.CreateAsync(OnImport); Title = "Restless"; Items = new RxList <ApiItemModel>(); Methods = httpMethods.ToList(); OutputTypes = outputTypes.ToList(); SplitterPosition = Settings.Default.MainWindowSplitterPosition; ApiSplitterPosition = Settings.Default.ApiPanelSplitterPosition; this.ObservePropertyChange(x => x.SplitterPosition).Throttle(TimeSpan.FromSeconds(1)).Subscribe(x => { Settings.Default.MainWindowSplitterPosition = x; Settings.Default.Save(); }); this.ObservePropertyChange(x => x.ApiSplitterPosition).Throttle(TimeSpan.FromSeconds(1)).Subscribe(x => { Settings.Default.ApiPanelSplitterPosition = x; Settings.Default.Save(); }); ApiItemModel selectedItem = null; this.ObservePropertyChange(x => x.SelectedItem).Subscribe(x => { if (selectedItem != null) { selectedItem.IsSelected = false; } selectedItem = x; if (selectedItem != null) { selectedItem.IsSelected = true; } }); Repository = new DbRepository(new RestlessDb()); Task.Run(async() => { await Repository.Initialize(); await Repository.Load(); foreach (var apiItem in Repository.Items) { if (apiItem is Api) { Items.Add(new ApiModel(this, null, (Api)apiItem)); } else { Items.Add(new ApiCollectionModel(this, null, (ApiCollection)apiItem)); } } }); DeleteSelectedItem = RxCommand.CreateAsync(OnDeleteSelectedItem); }
public MainWindowModel(Func<SelectFileType, string, string, string> selectFile) { this.selectFile = selectFile; var canAddChild = this.ObserveProperty(x => x.SelectedItem).Select(x => x is ApiCollectionModel); AddApi = RxFunction.CreateAsync(async () => await OnAddApi(null)); AddApiCollection = RxFunction.CreateAsync(async () => await OnAddApiCollection(null)); AddChildApi = RxFunction.CreateAsync(async () => await OnAddApi((ApiCollectionModel)SelectedItem), canAddChild); AddChildApiCollection = RxFunction.CreateAsync(async () => await OnAddApiCollection((ApiCollectionModel)SelectedItem), canAddChild); Export = RxCommand.Create(OnExport); Import = RxCommand.CreateAsync(OnImport); Title = "Restless"; Items = new RxList<ApiItemModel>(); Methods = httpMethods.ToList(); OutputTypes = outputTypes.ToList(); SplitterPosition = Settings.Default.MainWindowSplitterPosition; ApiSplitterPosition = Settings.Default.ApiPanelSplitterPosition; this.ObservePropertyChange(x => x.SplitterPosition).Throttle(TimeSpan.FromSeconds(1)).Subscribe(x => { Settings.Default.MainWindowSplitterPosition = x; Settings.Default.Save(); }); this.ObservePropertyChange(x => x.ApiSplitterPosition).Throttle(TimeSpan.FromSeconds(1)).Subscribe(x => { Settings.Default.ApiPanelSplitterPosition = x; Settings.Default.Save(); }); ApiItemModel selectedItem = null; this.ObservePropertyChange(x => x.SelectedItem).Subscribe(x => { if (selectedItem != null) selectedItem.IsSelected = false; selectedItem = x; if (selectedItem != null) selectedItem.IsSelected = true; }); Repository = new DbRepository(new RestlessDb()); Task.Run(async () => { await Repository.Initialize(); await Repository.Load(); foreach (var apiItem in Repository.Items) { if (apiItem is Api) Items.Add(new ApiModel(this, null, (Api)apiItem)); else Items.Add(new ApiCollectionModel(this, null, (ApiCollection)apiItem)); } }); DeleteSelectedItem = RxCommand.CreateAsync(OnDeleteSelectedItem); }