Skip to content

Commit

Permalink
add System.Linq.Async
Browse files Browse the repository at this point in the history
  • Loading branch information
iNoles committed Nov 20, 2024
1 parent f0d80fd commit c843284
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion HackerNews.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<OutputType>Library</OutputType>
<RootNamespace>HackerNews</RootNamespace>
<UseMaui>true</UseMaui>
<MauiVersion>9.0.0</MauiVersion>
<MauiVersion>9.0.10</MauiVersion>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Display name -->
Expand Down Expand Up @@ -44,6 +44,7 @@
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\Images\" />
Expand Down
32 changes: 16 additions & 16 deletions NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(ObservableCollection<T> collection, Comparison<T> comparison, T modelToInsert)
Expand Down Expand Up @@ -74,7 +74,7 @@ private async IAsyncEnumerable<StoryModel> 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;
Expand Down

0 comments on commit c843284

Please sign in to comment.