Skip to content

Commit

Permalink
NR Use new CollectionItemChanged event to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Sep 24, 2023
1 parent 1ef3beb commit edfff41
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PackageVersion Include="Microsoft.Graph" Version="4.54.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageVersion Include="System.ServiceModel.Syndication" Version="7.0.0" />
<PackageVersion Include="System.Waf.Core" Version="7.0.1-alpha.0.30" />
<PackageVersion Include="System.Waf.UnitTesting.Core" Version="7.0.1-alpha.0.30" />
<PackageVersion Include="System.Waf.Core" Version="7.0.1-alpha.0.37" />
<PackageVersion Include="System.Waf.UnitTesting.Core" Version="7.0.1-alpha.0.37" />
</ItemGroup>
</Project>
18 changes: 3 additions & 15 deletions src/NewsReader/NewsReader.Domain/Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,16 @@ public void SetLoadError(Exception loadError, string loadErrorMessage)
private void Initialize()
{
items.CollectionChanged += ItemsCollectionChanged;
items.CollectionChanging += ItemsCollectionChanging;
foreach (var x in items) x.PropertyChanged += FeedItemPropertyChanged;
items.CollectionItemChanged += ItemsCollectionItemChanged;
UpdateUnreadItemsCount();
Validate();
}

private void DataManagerPropertyChanged(object? sender, PropertyChangedEventArgs e) => TrimItemsList();

private void ItemsCollectionChanging(object? sender, NotifyCollectionChangedEventArgs e)
{
var oldItems = e.Action == NotifyCollectionChangedAction.Reset ? items : e.OldItems?.Cast<FeedItem>() ?? Array.Empty<FeedItem>();
foreach (var x in oldItems) x.PropertyChanged -= FeedItemPropertyChanged;
}

private void ItemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
var newItems = e.Action == NotifyCollectionChangedAction.Reset ? items : e.NewItems?.Cast<FeedItem>() ?? Array.Empty<FeedItem>();
foreach (var x in newItems) x.PropertyChanged += FeedItemPropertyChanged;
UpdateUnreadItemsCount();
}
private void ItemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => UpdateUnreadItemsCount();

private void FeedItemPropertyChanged(object? sender, PropertyChangedEventArgs e)
private void ItemsCollectionItemChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(FeedItem.MarkAsRead)) UpdateUnreadItemsCount();
}
Expand Down

0 comments on commit edfff41

Please sign in to comment.