Exemplo n.º 1
0
        public void UpdateContacts(IEnumerable <IContact> addedContacts, IEnumerable <IContact> changedContacts)
        {
            foreach (var contact in addedContacts)
            {
                bool add   = false;
                var  user  = contact as IUser;
                var  group = contact as IGroup;

                if (user != null && (user.Relation == UserRelation.Friend || user.Relation == UserRelation.PendingFriend))
                {
                    add = true;
                }
                else if (group != null && group.Joined)
                {
                    add = true;
                }
                if (add)
                {
                    contactVMs.Add(ContactViewModel.Create(client, contact));
                }
            }

            foreach (var contact in changedContacts)
            {
                bool remove = false;
                var  user   = contact as IUser;
                var  group  = contact as IGroup;

                if (user != null)
                {
                    switch (user.Relation)
                    {
                    case UserRelation.None:
                        remove = true;
                        break;

                    // Don't include ourselves in any lists
                    case UserRelation.Me:
                        continue;

                    // Add any users who just became our friends
                    case UserRelation.Friend:
                    case UserRelation.PendingFriend:
                        if (contactVMs.SingleOrDefault(_ => _.Contact == contact) == null)
                        {
                            contactVMs.Add(ContactViewModel.Create(client, contact));
                        }
                        break;
                    }
                }

                if (user != null && user.Relation == UserRelation.None)
                {
                    remove = true;
                }
                else if (group != null && !group.Joined)
                {
                    remove = true;
                }
                if (remove)
                {
                    var vm = contactVMs.SingleOrDefault(_ => _.Contact == contact);
                    if (vm != null)
                    {
                        contactVMs.Remove(vm);
                    }
                }
            }

            contactsView.Refresh();

            NotifyPropertyChanged("Contacts");
            NotifyPropertyChanged("ContactsView");
        }