void _OnRawCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: // If an item got added, was it something we care to see? Assert.AreEqual(1, e.NewItems.Count); var newPost = (ActivityPost)e.NewItems[0]; if (_IsInteresting(newPost.Actor)) { _filteredCollection.Add(newPost); _interestMap[newPost.Actor] = true; } break; case NotifyCollectionChangedAction.Move: _filteredCollection.RefreshSort(); break; case NotifyCollectionChangedAction.Remove: // If an item got removed, was it one we were really showing? Assert.AreEqual(1, e.OldItems.Count); var oldPost = (ActivityPost)e.OldItems[0]; if (_IsInteresting(oldPost.Actor)) { if (_filteredCollection.Remove(oldPost)) { if (!_filteredCollection.Any(post => post.Actor.UserId == oldPost.Actor.UserId)) { oldPost.Actor.PropertyChanged -= _OnContactPropertyChanged; _interestMap.Remove(oldPost.Actor); } } } break; case NotifyCollectionChangedAction.Replace: // Unsupported Assert.Fail(); break; case NotifyCollectionChangedAction.Reset: foreach (var contact in _interestMap.Keys) { contact.PropertyChanged -= _OnContactPropertyChanged; } _interestMap.Clear(); _filteredCollection.Clear(); break; } }
internal FacebookPhotoAlbumCollection(FBMergeableCollection <FacebookPhotoAlbum> sourceCollection, FacebookService service, FacebookContact owner) : base(sourceCollection, service) { VerifyAccess(); if (owner != null) { _rawCollection = sourceCollection; _owner = owner; _filteredCollection = new FBMergeableCollection <FacebookPhotoAlbum>(from album in sourceCollection where album.OwnerId == owner.UserId select album, false); base.ReplaceSourceCollection(_filteredCollection); sourceCollection.CollectionChanged += _OwnerFilterOnRawCollectionChanged; } // A null owner implicitly means that the album collection is filterable else { _interestMap = new Dictionary <FacebookContact, bool>(); _rawCollection = sourceCollection; _filteredCollection = new FBMergeableCollection <FacebookPhotoAlbum>(false); base.ReplaceSourceCollection(_filteredCollection); foreach (var album in sourceCollection) { album.PropertyChanged += _OnAlbumOwnerUpdated; if (album.Owner != null) { lock (_localLock) { if (!_interestMap.ContainsKey(album.Owner)) { _interestMap.Add(album.Owner, _IsOwnerInteresting(album)); album.Owner.PropertyChanged += _OnContactPropertyChanged; } } album.PropertyChanged -= _OnAlbumOwnerUpdated; if (_IsOwnerInteresting(album)) { _filteredCollection.Add(album); } } } sourceCollection.CollectionChanged += _InterestFilterOnRawCollectionChanged; } }
private void _OnAlbumOwnerUpdated(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Owner") { var album = (FacebookPhotoAlbum)sender; album.PropertyChanged -= _OnAlbumOwnerUpdated; lock (_localLock) { if (!_interestMap.ContainsKey(album.Owner)) { _interestMap.Add(album.Owner, _IsOwnerInteresting(album)); album.Owner.PropertyChanged += _OnContactPropertyChanged; } } if (_IsOwnerInteresting(album)) { _filteredCollection.Add(album); } } }