public ApiModel(MainWindowModel mainWindow, ApiCollectionModel parent, Api api) : base(mainWindow, parent, api) { Inputs = new RxList <ApiInputModel>(); Outputs = new RxList <ApiOutputModel>(); Headers = new RxList <ApiHeaderModel>(); SubscribeForInputs(this.ObservePropertyChange(x => x.Model.Url), () => Model.Url, ApiInputType.Url); SubscribeForInputs(this.ObservePropertyChange(x => x.Model.Body).Where(x => ContentTypes.IsText(ContentType)), () => Model.Body, ApiInputType.Body); SubscribeForInputs(this.ObservePropertyChange(x => x.Headers).SelectMany(x => x.ObserveElementProperty(y => y.Name)).Merge(this.ObservePropertyChange(x => x.Headers).SelectMany(x => x.ObserveElementProperty(y => y.Value))), () => string.Join("\n", Headers.Select(x => x.Name + "=" + x.Value)), ApiInputType.Header); Model = api; if (api.Inputs != null) { Inputs.AddRange(api.Inputs.Select(x => new ApiInputModel { Id = x.Id, Name = x.Name, DefaultValue = x.DefaultValue, InputType = x.InputType })); } if (api.Outputs != null) { Outputs.AddRange(api.Outputs.Select(x => new ApiOutputModel { Id = x.Id, Name = x.Name, Expression = x.Expression, Type = x.Type })); } if (api.Headers != null) { Headers.AddRange(api.Headers.Select(x => new ApiHeaderModel { Id = x.Id, Name = x.Name, Value = x.Value })); } Send = RxCommand.CreateAsync(OnSend); Reset = RxCommand.Create(OnReset); Inputs.SetUpSync( MainWindow.Repository, Model.Inputs, x => new ApiInput { Name = x.Name, DefaultValue = "", InputType = x.InputType }); Outputs.SetUpSync( MainWindow.Repository, Model.Outputs, x => new ApiOutput { Name = x.Name, Expression = "" }); Headers.SetUpSync( MainWindow.Repository, Model.Headers, _ => new ApiHeader { Name = "", Value = "" }); }
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); }