public static ItemViewModel Create(EvaluationItem model) { if (model == null) return new UndefinedViewModel(); if (string.IsNullOrEmpty(model.Id)) return new NewItemViewModel(); if (model.Status == "deleted") { return new DeletedViewModel { Id = model.Id, Name = model.Name, StatisticalWay = model.StatisticalWay }; } if (model.Links != null && model.Links.Any(o => o.Method == "PATCH")) { var vm = new ItemEditViewModel(model); return vm; } return new ItemViewModel { Id = model.Id, Name = model.Name, StatisticalWay = model.StatisticalWay, FormulaParams = model.FormulaParams.ConvertTo<IFormulaParams>() }; }
public ItemEditViewModel(EvaluationItem model) { Id = model.Id; _Name = model.Name; StatisticalWay = model.StatisticalWay; FormulaParams = model.FormulaParams.ConvertTo<IFormulaParams>(); Description = model.Description.ConvertTo<IEvaluationItemDescription>(); var obsvr = Observer.Create<FormulaInfo>(o => { if (_updateValues == null) return; _updateValues.Formula = o; }); Observable.FromEventPattern<PropertyChangedEventArgs>(Description, "PropertyChanged") .Select(args => args.Sender) .OfType<IEvaluationItemDescription>() .Subscribe(o => { if (_updateValues == null) return; _updateValues.Description = o; }); SetFormulaOptions = model.FormulaOptions.Select(o => { var vm = o.ConvertTo<IFormulaParams>(); if (vm != null) Observable.FromEventPattern<PropertyChangedEventArgs>(vm, "PropertyChanged") .Select(args => args.Sender) .OfType<IFormulaParams>().Select(p => new FormulaInfo().PopulateWith(p)) .Subscribe(obsvr); return vm ?? new UnknownFormulaParams(); }).ToList(); _SelectedFormula = SetFormulaOptions.FirstOrDefault(o => FormulaParams != null && FormulaParams.GetType() == o.GetType()); this.WhenAny(x => x.SelectedFormula, x => x.Value) .Select(o => o == null ? null : new FormulaInfo().PopulateWith(o)) .Subscribe(obsvr); this.WhenAny(x => x.Name, x => x.Value) .Subscribe(o => { if (_updateValues == null) return; _updateValues.Name = o; }); IsEditing = true; _updateValues = new UpateRequestBody(); }