From c843284b5f4b52473fa9fee169d489c7a8b6b87a Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Tue, 19 Nov 2024 19:43:02 -0500 Subject: [PATCH] add System.Linq.Async --- HackerNews.csproj | 3 ++- NewsViewModel.cs | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/HackerNews.csproj b/HackerNews.csproj index 978b2a8..e567e04 100644 --- a/HackerNews.csproj +++ b/HackerNews.csproj @@ -7,7 +7,7 @@ Library HackerNews true - 9.0.0 + 9.0.10 true enable @@ -44,6 +44,7 @@ + diff --git a/NewsViewModel.cs b/NewsViewModel.cs index 4bb45ac..b0f4c6d 100644 --- a/NewsViewModel.cs +++ b/NewsViewModel.cs @@ -17,24 +17,24 @@ public async Task RefreshAsync() // Ensure this method is called on the UI thread await MainThread.InvokeOnMainThreadAsync(async () => { - try - { - TopStoryCollection.Clear(); + try + { + TopStoryCollection.Clear(); - var sortedStories = (await GetTopStoriesAsync().ToListAsync()) - .OrderByDescending(story => story.Score) - .ToList(); + var sortedStories = (await GetTopStoriesAsync().ToListAsync()) + .OrderByDescending(story => story.Score) + .ToList(); - foreach (var story in sortedStories) - { - TopStoryCollection.Add(story); // Add sorted stories to the collection - } - } - catch (Exception ex) - { + foreach (var story in sortedStories) + { + TopStoryCollection.Add(story); // Add sorted stories to the collection + } + } + catch (Exception ex) + { _logger.LogError(ex, "Error while refreshing top stories."); - } - }); + } + }); } static void InsertIntoSortedCollection(ObservableCollection collection, Comparison comparison, T modelToInsert) @@ -74,7 +74,7 @@ private async IAsyncEnumerable GetTopStoriesAsync() _logger.LogError(ex, "Error while fetching top stories."); yield break; // Stop if there’s an issue fetching the top story IDs } - + foreach (var id in topStoryIds) { StoryModel story;