Skip to content

Commit

Permalink
NR simplify code by using new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 22, 2023
1 parent 18a0976 commit db4e058
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 74 deletions.
16 changes: 8 additions & 8 deletions src/NewsReader/NewsReader.Domain.Test/FeedItemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ public void ApplyValuesFromTest()
{
AssertHelper.ExpectedException<ArgumentNullException>(() => new FeedItem(null!, DateTimeOffset.Now, "test", "test"));

var itemA1 = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc");
Assert.AreEqual(new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), itemA1.Date);
var itemA1 = new FeedItem(new("http://www.test.com/rss/feed"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name", "desc");
Assert.AreEqual(new DateTimeOffset(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), itemA1.Date);
Assert.AreEqual("name", itemA1.Name);
Assert.AreEqual("desc", itemA1.Description);
Assert.IsFalse(itemA1.MarkAsRead);

var itemA2 = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2022, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2", "desc2");
var itemA2 = new FeedItem(new("http://www.test.com/rss/feed"), new(2022, 5, 5, 12, 0, 0, new(1, 0, 0)), "name2", "desc2");
itemA2.MarkAsRead = true;
itemA1.ApplyValuesFrom(itemA2);
Assert.AreEqual(new DateTimeOffset(2022, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), itemA1.Date);
Assert.AreEqual(new DateTimeOffset(2022, 5, 5, 12, 0, 0, new(1, 0, 0)), itemA1.Date);
Assert.AreEqual("name2", itemA1.Name);
Assert.AreEqual("desc2", itemA1.Description);
Assert.IsTrue(itemA1.MarkAsRead);

var itemB1 = new FeedItem(new Uri("http://www.test.com/rss/feed2"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc");
var itemB1 = new FeedItem(new("http://www.test.com/rss/feed2"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name", "desc");
AssertHelper.ExpectedException<InvalidOperationException>(() => itemA1.ApplyValuesFrom(itemB1));
}

[TestMethod]
public void CloneTest()
{
var item = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc");
var item = new FeedItem(new("http://www.test.com/rss/feed"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name", "desc");
item.MarkAsRead = true;
var clone = item.Clone();

Assert.AreNotSame(item, clone);
Assert.AreEqual(new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), clone.Date);
Assert.AreEqual(new DateTimeOffset(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), clone.Date);
Assert.AreEqual("name", clone.Name);
Assert.AreEqual("desc", clone.Description);
Assert.IsTrue(clone.MarkAsRead);
Expand All @@ -48,7 +48,7 @@ public void CloneTest()
[TestMethod]
public void SupportNull()
{
var item = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc");
var item = new FeedItem(new("http://www.test.com/rss/feed"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name", "desc");
item.Name = null;
item.Description = null;
Assert.IsNull(item.Name);
Expand Down
26 changes: 13 additions & 13 deletions src/NewsReader/NewsReader.Domain.Test/FeedManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void SetDataManager()
var feedManagerA = new FeedManager();
Assert.AreSame(feedManagerA, feedManagerA.Feeds.Single().DataManager);

var feedA1 = new Feed(new Uri("http://www.test.com/rss/feed"));
var feedA1 = new Feed(new("http://www.test.com/rss/feed"));
Assert.IsNull(feedA1.DataManager);

feedManagerA.Feeds.Add(feedA1);
Expand Down Expand Up @@ -44,11 +44,11 @@ public void MergeTest()
{
var feedManagerA = new FeedManager();
feedManagerA.Feeds.Remove(feedManagerA.Feeds.Single());
var feedA1 = new Feed(new Uri("http://www.test.com/rss/feedA1"));
var feedA2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
var feedA1 = new Feed(new("http://www.test.com/rss/feedA1"));
var feedA2 = new Feed(new("http://www.test.com/rss/feedA2"));
feedManagerA.Feeds.Add(feedA1);
feedManagerA.Feeds.Add(feedA2);
var item = new FeedItem(new Uri("http://www.test.com/rss/feed"), new DateTimeOffset(2120, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name", "desc");
var item = new FeedItem(new("http://www.test.com/rss/feed"), new(2120, 5, 5, 12, 0, 0, new(1, 0, 0)), "name", "desc");
feedA2.UpdateItems(new[] { item });
Assert.IsFalse(feedManagerA.Feeds.Last().Items.Single().MarkAsRead);

Expand All @@ -58,8 +58,8 @@ public void MergeTest()
MaxItemsLimit = 43
};
feedManagerB.Feeds.Remove(feedManagerB.Feeds.Single());
var feedB1 = new Feed(new Uri("http://www.test.com/rss/feedB1"));
var feedB2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
var feedB1 = new Feed(new("http://www.test.com/rss/feedB1"));
var feedB2 = new Feed(new("http://www.test.com/rss/feedA2"));
feedManagerB.Feeds.Add(feedB1);
feedManagerB.Feeds.Add(feedB2);
feedB2.UpdateItems(new[] { item }, cloneItemsBeforeInsert: true);
Expand All @@ -76,7 +76,7 @@ public void MergeTest()
// Just remove one element => this way the Merge does not create new instances
var feedManagerC = new FeedManager();
feedManagerC.Feeds.Remove(feedManagerC.Feeds.Single());
var feedC2 = new Feed(new Uri("http://www.test.com/rss/feedA2"));
var feedC2 = new Feed(new("http://www.test.com/rss/feedA2"));
feedManagerC.Feeds.Add(feedC2);
feedC2.UpdateItems(new[] { item }, cloneItemsBeforeInsert: true);
feedC2.Items.Single().MarkAsRead = false;
Expand All @@ -92,12 +92,12 @@ public void MergeTest()
[TestMethod]
public void FeedEqualityComparerTest()
{
Assert.IsTrue(FeedEqualityComparer.Default.Equals(new Feed(new Uri("http://microsoft.com")), new Feed(new Uri("http://microsoft.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new Uri("http://microsoft.com")), new Feed(new Uri("http://google.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new Uri("http://microsoft.com")), new Feed(null!)));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(null!), new Feed(new Uri("http://microsoft.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new Uri("http://microsoft.com")), null!));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(null!, new Feed(new Uri("http://microsoft.com"))));
Assert.IsTrue(FeedEqualityComparer.Default.Equals(new Feed(new("http://microsoft.com")), new Feed(new("http://microsoft.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new("http://microsoft.com")), new Feed(new("http://google.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new("http://microsoft.com")), new Feed(null!)));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(null!), new Feed(new("http://microsoft.com"))));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(new Feed(new("http://microsoft.com")), null!));
Assert.IsFalse(FeedEqualityComparer.Default.Equals(null!, new Feed(new("http://microsoft.com"))));
Assert.IsTrue(FeedEqualityComparer.Default.Equals(new Feed(null!), new Feed(null!)));
Assert.IsTrue(FeedEqualityComparer.Default.Equals(null!, null!));

Expand Down
48 changes: 23 additions & 25 deletions src/NewsReader/NewsReader.Domain.Test/FeedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class FeedTest : DomainTest

private static void IsLoadingCoreTest(bool useSerializer)
{
var feed1 = new Feed(new Uri("http://www.test.com/rss/feed"));
var feed2 = new Feed(new Uri("http://www.test.com/rss/feed"));
var feed1 = new Feed(new("http://www.test.com/rss/feed"));
var feed2 = new Feed(new("http://www.test.com/rss/feed"));
feed1 = !useSerializer ? feed1 : SerializerHelper.Clone(feed1);
feed2 = !useSerializer ? feed2 : SerializerHelper.Clone(feed2);

Expand All @@ -39,7 +39,7 @@ private static void IsLoadingCoreTest(bool useSerializer)
Assert.IsFalse(feed2.IsLoading);
feed2.StartLoading();
Assert.IsTrue(feed2.IsLoading);
feed2.UpdateItems(new FeedItem[0]);
feed2.UpdateItems([]);
Assert.IsFalse(feed2.IsLoading);
}

Expand All @@ -57,20 +57,20 @@ private static void IsLoadingCoreTest(bool useSerializer)

private static void UpdateItemsCoreTest(bool cloneItemsBeforeInsert, bool useSerializer)
{
var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
feed.UpdateItems(new[] {
new FeedItem(new Uri("http://www.test.com/rss/feed/1"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name1", "desc"),
new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 7, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2", "desc"),
});
var feed = new Feed(new("http://www.test.com/rss/feed"));
feed.UpdateItems([
new FeedItem(new("http://www.test.com/rss/feed/1"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name1", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/2"), new(2020, 5, 7, 12, 0, 0, new(1, 0, 0)), "name2", "desc"),
]);
feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

Assert.AreEqual(2, feed.Items.Count);
Assert.IsTrue(new[] { "name2", "name1" }.SequenceEqual(feed.Items.Select(x => x.Name)));

var newItems = new[]
{
new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 7, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2b", "desc"),
new FeedItem(new Uri("http://www.test.com/rss/feed/3"), new DateTimeOffset(2020, 5, 6, 12, 0, 0, new TimeSpan(1, 0, 0)), "name3", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/2"), new(2020, 5, 7, 12, 0, 0, new(1, 0, 0)), "name2b", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/3"), new(2020, 5, 6, 12, 0, 0, new(1, 0, 0)), "name3", "desc"),
};
feed.UpdateItems(newItems, cloneItemsBeforeInsert: cloneItemsBeforeInsert);

Expand All @@ -94,13 +94,13 @@ private static void UpdateItemsCoreTest(bool cloneItemsBeforeInsert, bool useSer

private static void UnreadItemsCountCoreTest(bool useSerializer)
{
var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
var feed = new Feed(new("http://www.test.com/rss/feed"));
var feedManager = new FeedManager() { MaxItemsLimit = null, ItemLifetime = null };
feedManager.Feeds.Add(feed);
feed.UpdateItems(new[] {
new FeedItem(new Uri("http://www.test.com/rss/feed/1"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name1", "desc"),
new FeedItem(new Uri("http://www.test.com/rss/feed/2"), new DateTimeOffset(2020, 5, 5, 12, 0, 0, new TimeSpan(1, 0, 0)), "name2", "desc"),
});
feed.UpdateItems([
new FeedItem(new("http://www.test.com/rss/feed/1"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name1", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/2"), new(2020, 5, 5, 12, 0, 0, new(1, 0, 0)), "name2", "desc"),
]);
feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

Assert.AreEqual(2, feed.UnreadItemsCount);
Expand All @@ -111,9 +111,7 @@ private static void UnreadItemsCountCoreTest(bool useSerializer)

AssertHelper.PropertyChangedEvent(feed, x => x.UnreadItemsCount, () =>
{
feed.UpdateItems(new[] {
new FeedItem(new Uri("http://www.test.com/rss/feed/3"), new DateTimeOffset(2020, 5, 5, 12, 0, 3, new TimeSpan(1, 0, 0)), "name3", "desc"),
});
feed.UpdateItems([new FeedItem(new("http://www.test.com/rss/feed/3"), new(2020, 5, 5, 12, 0, 3, new(1, 0, 0)), "name3", "desc")]);
});

Assert.AreEqual(2, feed.UnreadItemsCount);
Expand All @@ -131,7 +129,7 @@ private static void UnreadItemsCountCoreTest(bool useSerializer)

private static void TrimItemsListWithMaxItemsLimitTest(bool useSerializer)
{
var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
var feed = new Feed(new("http://www.test.com/rss/feed"));
feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

var feedManager = new FeedManager();
Expand Down Expand Up @@ -165,7 +163,7 @@ private static void TrimItemsListWithMaxItemsLimitTest(bool useSerializer)

private static void TrimItemsListWithItemLifetimeTest(bool useSerializer)
{
var feed = new Feed(new Uri("http://www.test.com/rss/feed"));
var feed = new Feed(new("http://www.test.com/rss/feed"));
feed = !useSerializer ? feed : SerializerHelper.Clone(feed);

var feedManager = new FeedManager();
Expand Down Expand Up @@ -193,10 +191,10 @@ private static void TrimItemsListWithItemLifetimeTest(bool useSerializer)

private static void UpdateFeedItems(Feed feed)
{
feed.UpdateItems(new[] {
new FeedItem(new Uri("http://www.test.com/rss/feed/1"), DateTimeOffset.Now - TimeSpan.FromDays(10), "name1", "desc"),
new FeedItem(new Uri("http://www.test.com/rss/feed/2"), DateTimeOffset.Now - TimeSpan.FromDays(5), "name2", "desc"),
new FeedItem(new Uri("http://www.test.com/rss/feed/3"), DateTimeOffset.Now - TimeSpan.FromDays(1), "name3", "desc"),
});
feed.UpdateItems([
new FeedItem(new("http://www.test.com/rss/feed/1"), DateTimeOffset.Now - TimeSpan.FromDays(10), "name1", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/2"), DateTimeOffset.Now - TimeSpan.FromDays(5), "name2", "desc"),
new FeedItem(new("http://www.test.com/rss/feed/3"), DateTimeOffset.Now - TimeSpan.FromDays(1), "name3", "desc"),
]);
}
}
2 changes: 1 addition & 1 deletion src/NewsReader/NewsReader.Domain/Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Feed(Uri uri)
{
// Note: Serializer does not call the constructor.
this.uri = uri;
items = new();
items = [];
Initialize();
}

Expand Down
2 changes: 1 addition & 1 deletion src/NewsReader/NewsReader.Domain/FeedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FeedManager : Model, IDataManager
public FeedManager()
{
// Note: Serializer does not call the constructor.
feeds = new() { new Feed(new Uri("https://devblogs.microsoft.com/dotnet/category/maui/feed/")) };
feeds = [new Feed(new Uri("https://devblogs.microsoft.com/dotnet/category/maui/feed/"))];
ItemLifetime = TimeSpan.FromDays(365);
MaxItemsLimit = 250;
Initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,8 @@ private IReadOnlyList<ObservableGroupingView<TKey, TElement>> CreateGroupingList
private void OriginalCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => UpdateInnerList();


private sealed class InnerListKeyComparer : IEqualityComparer<ObservableGroupingView<TKey, TElement>>
private sealed class InnerListKeyComparer(IEqualityComparer<TKey> keyComparer) : IEqualityComparer<ObservableGroupingView<TKey, TElement>>
{
private readonly IEqualityComparer<TKey> keyComparer;

public InnerListKeyComparer(IEqualityComparer<TKey> keyComparer)
{
this.keyComparer = keyComparer;
}

public bool Equals(ObservableGroupingView<TKey, TElement>? x, ObservableGroupingView<TKey, TElement>? y)
{
if (x != null && y != null) return keyComparer.Equals(x.Key, y.Key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Waf.NewsReader.Domain.Foundation;

public class ObservableGroupingView<TKey, TElement> : ObservableListViewBase<TElement>, IGrouping<TKey, TElement>
public class ObservableGroupingView<TKey, TElement>(TKey key, IEnumerable<TElement> originalList) : ObservableListViewBase<TElement>(originalList), IGrouping<TKey, TElement>
{
public ObservableGroupingView(TKey key, IEnumerable<TElement> originalList) : base(originalList)
{
Key = key;
}

public TKey Key { get; }
public TKey Key { get; } = key;

internal new List<TElement> InnerList => base.InnerList;

Expand Down
Binary file modified src/NewsReader/NewsReader.Domain/GlobalSuppressions.cs
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ namespace Waf.NewsReader.Presentation.Services;

public class AppInfoService : IAppInfoService
{
public AppInfoService()
{
AppName = AppInfo.Name;
VersionString = AppInfo.VersionString;
}
public string AppName { get; } = AppInfo.Name;

public string AppName { get; }

public string VersionString { get; }
public string VersionString { get; } = AppInfo.VersionString;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class FeedItemView : IFeedItemView
public FeedItemView()
{
InitializeComponent();
cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource = new();
}

protected override void OnBindingContextChanged()
Expand All @@ -24,7 +24,7 @@ protected override void OnBindingContextChanged()
protected override void OnAppearing()
{
base.OnAppearing();
cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource = new();
webView.Source = viewModel.FeedItem?.Uri;
}

Expand All @@ -47,7 +47,7 @@ protected override bool OnBackButtonPressed()

private async void WebViewNavigating(object sender, WebNavigatingEventArgs e)
{
webViewNavigated = new TaskCompletionSource<object?>();
webViewNavigated = new();
activityIndicator.IsVisible = true;
activityIndicator.IsRunning = true;
var delayTask = Task.Delay(TimeSpan.FromSeconds(5), cancellationTokenSource.Token);
Expand Down

0 comments on commit db4e058

Please sign in to comment.