/// <summary> /// Initializes a new instance of the <see cref="UnitOfMeasureScreenViewModel"/> class. /// This also sets up all commands and data objects in the view. /// </summary> public UnitOfMeasureScreenViewModel() { this.Cancel = new AsyncCommand( (obj) => { this.Selected = null; this.CurrentUnit = new UnitOfMeasureViewModel(this.Save, this.Cancel); }, this.CanCancel); this.Save = new AsyncCommand( async(obj) => { var model = new UnitOfMeasureModel { Id = this.currentUnit.Id, Name = this.currentUnit.FullName, Abbreviation = this.currentUnit.Abbreviation, IsDeleted = this.currentUnit.IsDeleted }; var existing = this.Units.FirstOrDefault(u => u.Id == this.currentUnit.Id); if (existing == null) { model = await base.Api.PostAsync <UnitOfMeasureModel>(Constants.Endpoints.Units, model); if (model == null) { //error saving, so show field error and return. return; } var newCopy = new UnitOfMeasureViewModel(model, Save, Cancel); this.Units.Add(newCopy); } else { model = await base.Api.PutAsync <UnitOfMeasureModel>(Constants.Endpoints.Units, model); if (model == null) { //error saving, so show field error and return. return; } existing.FullName = model.Name; existing.Abbreviation = model.Abbreviation; existing.IsDeleted = model.IsDeleted; existing.SetModificationType(ModificationType.Update); } this.CurrentUnit = new UnitOfMeasureViewModel(this.Save, this.Cancel); this.Selected = null; }, this.ValidateInput); this.Units = new ObservableCollection <UnitOfMeasureViewModel>(); this.CurrentUnit = new UnitOfMeasureViewModel(this.Save, this.Cancel); this.Crumbs.Add(new BreadcrumbItemModel("Admin", this.GoToAdmin)); this.Crumbs.Add(new BreadcrumbItemModel("Unit of Measure")); }
/// <summary> /// Initializes a new instance of the <see cref="UnitOfMeasureModel"/> class. /// This is a copy constructor so all values will get copied to the new copy. /// </summary> /// <param name="toCopy">The object to copy.</param> public UnitOfMeasureViewModel(UnitOfMeasureViewModel toCopy) { this.UnitTypes = new List <string> { "Number", "Text", "Toggle (On/Off, Yes/No etc.)" }; this.fullName = toCopy.FullName; this.unitType = toCopy.UnitType; this.abbreviation = toCopy.Abbreviation; this.Id = toCopy.Id; this.Save = toCopy.Save; this.Cancel = toCopy.Cancel; this.modificationType = toCopy.modificationType; }