Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
MakeshVineeth committed Nov 4, 2021
1 parent a02c617 commit 8127bf8
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions WikiPreview.Fluent.Plugin/ResultGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
Expand Down Expand Up @@ -54,7 +53,7 @@ public static async ValueTask<WikiPreviewSearchResult> GenerateSearchResult(Page

double score = displayedName.SearchTokens(searchedText);
string wikiUrl = displayedName.Replace(' ', '_');
BitmapImageResult bitmapImageResult;
BitmapImageResult bitmapImageResult = null;

if (loadImage && value.Thumbnail != null)
{
Expand All @@ -63,29 +62,19 @@ public static async ValueTask<WikiPreviewSearchResult> GenerateSearchResult(Page
using var imageClient = new HttpClient();
imageClient.DefaultRequestHeaders.UserAgent.TryParseAdd(UserAgentString);

using HttpResponseMessage response =
await imageClient.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead);

if (response.IsSuccessStatusCode)
await imageClient.GetStreamAsync(imgUrl).ContinueWith(task =>
{
await using Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

#pragma warning disable CA1416
if (!task.IsCompletedSuccessfully) return;
var bitmap =
new Bitmap(stream); // Wiki Images are not working with AvaloniaBitmap as of now.
#pragma warning restore CA1416
bitmapImageResult = new BitmapImageResult(bitmap);
}
else
{
bitmapImageResult = WikipediaLogo;
}
}
else
{
bitmapImageResult = WikipediaLogo;
new Bitmap(task.Result); // Wiki Images are not working with AvaloniaBitmap as of now.

if (!bitmap.Size.IsEmpty)
bitmapImageResult = new BitmapImageResult(bitmap);
});
}

bitmapImageResult ??= WikipediaLogo;

return new WikiPreviewSearchResult(resultName)
{
Url = wikiUrl,
Expand Down

0 comments on commit 8127bf8

Please sign in to comment.