Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UnitOfMeasureModel"/> class.
        /// This then caches the save and cancel commands so that it can
        /// notify any views based on them.
        /// </summary>
        /// <param name="model">The model data to copy.</param>
        /// <param name="save">The save command that is based on this unit.</param>
        /// <param name="cancel">The cancel command that is based on this unit.</param>
        public ItemViewModel(ItemModel model, AsyncCommand save, AsyncCommand cancel, ObservableCollection <UnitOfMeasureModel> units)
            : this(save, cancel, units)
        {
            this.Model = model;

            this.Id             = model.Id;
            this.name           = model.Name;
            this.lowerBound     = model.Specification.LowerBound;
            this.upperBound     = model.Specification.UpperBound;
            this.unit           = units.FirstOrDefault(u => u.Id == model.Specification.UnitOfMeasure.Id);
            this.isDeleted      = model.IsDeleted;
            this.meter          = model.Meter;
            this.comparisonType = ComparisonTypeViewModel.Locate(model.Specification.ComparisonType);
            this.SetModificationType(Shared.ModificationType.Update);
        }
        /// <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 ItemScreenViewModel(RegionViewModel region, StationViewModel station)
        {
            this.Units = new ObservableCollection <UnitOfMeasureModel>();
            this.Items = new ObservableCollection <ItemViewModel>();

            this.Cancel = new AsyncCommand(
                (obj) =>
            {
                this.Selected    = null;
                this.CurrentItem = new ItemViewModel(this.Save, this.Cancel, this.Units);
            }, this.CanCancel);

            this.Save = new AsyncCommand(
                async(obj) =>
            {
                var model = new ItemModel
                {
                    Id            = this.currentItem.Id,
                    Name          = this.currentItem.Name,
                    IsDeleted     = this.currentItem.IsDeleted,
                    Meter         = this.currentItem.Meter,
                    StationId     = this.BelongsTo.Id,
                    Specification = new SpecificationModel
                    {
                        ComparisonType = this.currentItem.ComparisonType.Name,
                        LowerBound     = this.currentItem.LowerBound,
                        UpperBound     = this.currentItem.UpperBound,
                        UnitOfMeasure  = this.currentItem.Unit
                    }
                };

                var existing = this.Items.FirstOrDefault(u => u.Id == this.currentItem.Id);
                if (existing == null)
                {
                    model = await base.Api.PostAsync <ItemModel>(
                        $"{Constants.Endpoints.Items}", model);
                    if (model == null)
                    {
                        return;
                    }

                    var newCopy = new ItemViewModel(model, Save, Cancel, Units);
                    this.Items.Add(newCopy);
                }
                else
                {
                    model = await base.Api.PutAsync <ItemModel>(
                        $"{Constants.Endpoints.Items}", model);

                    if (model == null)
                    {
                        return;
                    }

                    existing.Name           = model.Name;
                    existing.ComparisonType = ComparisonTypeViewModel.Locate(model.Specification.ComparisonType);
                    existing.IsDeleted      = model.IsDeleted;
                    existing.UpperBound     = model.Specification.UpperBound;
                    existing.LowerBound     = model.Specification.LowerBound;
                    existing.Unit           = model.Specification.UnitOfMeasure;
                    existing.Meter          = model.Meter;
                    existing.SetModificationType(ModificationType.Update);
                }

                this.CurrentItem = new ItemViewModel(this.Save, this.Cancel, this.Units);
                this.Selected    = null;
            }, this.ValidateInput);

            this.Crumbs.Add(new BreadcrumbItemModel("Admin", this.GoToAdmin));

            this.CurrentItem = new ItemViewModel(this.Save, this.Cancel, this.Units);

            this.BelongsTo = station.Model;

            this.SetupRegionBreadcrumb(region);
            this.SetupStationBreadcrumb(station);
        }