Exemple #1
0
        public ContactsViewModel(IEventAggregator eventAggragator, IContactService contactService, IDialogService dialogService)
            : base(eventAggragator, contactService, dialogService)
        {
            ActivateContactCommand = new DelegateCommand <string>(ActivateContact);

            Contacts = contactService.GetContacts();

            EventAggregator.GetEvent <ContactUpdatedEvent>().Subscribe((contact) =>
            {
                if (ActiveContact != null)
                {
                    ActiveContact.CopyProperties(contact);
                }
            });

            EventAggregator.GetEvent <ContactDeletedEvent>().Subscribe((contact) =>
            {
                if (ActiveContact != null)
                {
                    Contacts.Remove(Contacts.First(c => c.Id == contact.Id));
                }
            });

            var viewItemsCountChangedEvent = EventAggregator.GetEvent <ViewItemsCountChangedEvent>();

            Contacts.CollectionChanged += (s, a) => viewItemsCountChangedEvent.Publish(Contacts.Count);
        }
        public bool CanCloseDialog()
        {
            InteractionResult msgCloseResult = InteractionResult.None;

            if (!_isDirty && !ActiveContact.HasErrors())
            {
                return(true);
            }

            if (!_isDirty && _isNewContact && !_closeRequested)
            {
                return(true);
            }

            if (_closeRequested)
            {
                msgCloseResult = _saveChanges ? InteractionResult.Yes : InteractionResult.No;
            }
            else
            {
                msgCloseResult = _messageBoxService.Show("IG Outlook", ResourceStrings.SaveChangesMessage_Text, MessageBoxButtons.YesNoCancel);
            }

            if (msgCloseResult == InteractionResult.Cancel)
            {
                _saveChanges = false;
                return(false);
            }
            else
            {
                _saveChanges = msgCloseResult == InteractionResult.Yes;

                if (_saveChanges)
                {
                    InteractionResult msgInvalidDataResult = InteractionResult.None;

                    if (ActiveContact.HasErrors())
                    {
                        msgInvalidDataResult = _messageBoxService.Show("IG Outlook", ResourceStrings.InvalidContactDataMessage_Text, MessageBoxButtons.YesNo);
                    }

                    if (msgInvalidDataResult == InteractionResult.No)
                    {
                        return(true);
                    }
                    else
                    {
                        if (_isNewContact)
                        {
                            ContactService.AddContact(ActiveContact);
                            return(true);
                        }
                    }

                    ContactService.UpdateContact(ActiveContact);
                    EventAggregator.GetEvent <ContactUpdatedEvent>().Publish(ActiveContact);
                }

                return(true);
            }
        }