Exemplo n.º 1
0
        /// <inheritdoc />
        protected override void OnInitialize()
        {
            base.OnInitialize();

            var partConnectors = new List <IPartConnectorViewModel>(Product.Parts.Count);

            foreach (var partConnector in Product.Parts)
            {
                IPartConnectorViewModel partConnectorVm;
                if (partConnector.IsCollection)
                {
                    partConnectorVm = new CollectionPartConnectorViewModel(partConnector)
                    {
                        DialogManager      = DialogManager,
                        PartDialogsFactory = PartDialogsFactory
                    };
                }
                else
                {
                    partConnectorVm = new SinglePartConnectorPartViewModel(partConnector)
                    {
                        DialogManager      = DialogManager,
                        PartDialogsFactory = PartDialogsFactory
                    };
                }

                partConnectors.Add(partConnectorVm);
            }

            PartConnectors        = partConnectors.ToArray();
            SelectedPartConnector = PartConnectors.FirstOrDefault();
        }
        public CollectionPartConnectorViewModel(PartConnectorViewModel partConnector)
        {
            PartConnector = partConnector;

            AddCmd    = new AsyncCommand(OnAdd, CanAdd, true);
            RemoveCmd = new RelayCommand(Remove, CanRemove);

            PartConnector.PartLinks.ForEach(pl => AddPartLink(partConnector, pl));
            SelectedPartLink = PartLinks.FirstOrDefault();
        }
        private SinglePartConnectorPartViewModel AddPartLink(PartConnectorViewModel partConnector, PartLinkViewModel partLink)
        {
            var link = new SinglePartConnectorPartViewModel(partConnector, partLink)
            {
                DialogManager      = DialogManager,
                PartDialogsFactory = PartDialogsFactory
            };

            PartLinks.Add(link);

            return(link);
        }
        private async Task OnAdd(object obj)
        {
            var dialog = PartDialogsFactory.CreateSelectPartDialog(PartConnector);
            await DialogManager.ShowDialogAsync(dialog);

            if (!dialog.Result)
            {
                PartDialogsFactory.Destroy(dialog);
                return;
            }

            var newLink = AddPartLink(PartConnector, dialog.PartLink);

            newLink.BeginEdit();

            _newLinks.Add(newLink);
            SelectedPartLink = newLink;

            PartDialogsFactory.Destroy(dialog);
        }
        private void Remove(object obj)
        {
            var idx = PartLinks.IndexOf(SelectedPartLink);

            // If is not new, add to list of removed part links
            if (!_newLinks.Contains(SelectedPartLink))
            {
                _removedLinks.Add(SelectedPartLink);
            }
            else
            {
                _newLinks.Remove(SelectedPartLink);
            }

            PartLinks.Remove(SelectedPartLink);

            if (idx >= PartLinks.Count)
            {
                idx = PartLinks.Count - 1;
            }

            SelectedPartLink = idx >= 0 ? PartLinks[idx] : null;
        }