Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[Bug] GravatarImageSource throws exception when offline (#1816)
Browse files Browse the repository at this point in the history
* [Bug] GravatarImageSource throws exception when offline #1770

Fix Nullable

* ConfigureAwait, revert sln
  • Loading branch information
VladislavAntonyuk authored Feb 21, 2022
1 parent b767630 commit 2803cb4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion samples/XCT.Sample.Android/SplashActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.App;
#nullable enable
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ static void UpdateStatusBarAppearance()
}
}

static void UpdateStatusBarAppearance(UIWindow window)
static void UpdateStatusBarAppearance(UIWindow? window)
{
var vc = window.RootViewController ?? throw new NullReferenceException(nameof(window.RootViewController));
var vc = window?.RootViewController ?? throw new NullReferenceException(nameof(window.RootViewController));
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
Expand All @@ -12,8 +13,8 @@ namespace Xamarin.CommunityToolkit.UI.Views
public partial class GravatarImageSourceHandler
{
const string requestUriFormat = "https://www.gravatar.com/avatar/{0}?s={1}&d={2}";
static readonly Lazy<HttpClient> lazyHttp = new Lazy<HttpClient>(() => new HttpClient());
static readonly SemaphoreSlim semaphore = new SemaphoreSlim(1);
static readonly Lazy<HttpClient> lazyHttp = new(() => new HttpClient());
static readonly SemaphoreSlim semaphore = new(1);

public static async Task<FileInfo?> LoadInternal(ImageSource imageSource, float scale, string cacheDirectory)
{
Expand All @@ -26,7 +27,7 @@ public partial class GravatarImageSourceHandler
{
_ = gis.Email ?? throw new InvalidOperationException($"{nameof(gis.Email)} is not initialized");
var imageBytes = await GetGravatarAsync(gis.Email, gis.Size, scale, gis.Default);
await SaveImage(cacheFileInfo, imageBytes ?? Array.Empty<byte>());
await SaveImage(cacheFileInfo, imageBytes);
}

return cacheFileInfo;
Expand Down Expand Up @@ -67,7 +68,7 @@ static async Task<bool> UseCacheFile(bool cachingEnabled, TimeSpan cacheValidity
{
await semaphore.WaitAsync();

if (!file.Directory.Exists)
if (file.Directory is { Exists: false })
file.Directory.Create();
}
finally
Expand All @@ -87,12 +88,20 @@ static string CacheFileName(GravatarImageSource gis, float scale)
static async Task<byte[]> GetGravatarAsync(string email, int size, float scale, DefaultGravatar defaultGravatar)
{
var requestUri = GetGravatarUri(email, size, scale, defaultGravatar);
using var response = await lazyHttp.Value.GetAsync(requestUri);
try
{
using var response = await lazyHttp.Value.GetAsync(requestUri);

if (!response.IsSuccessStatusCode)
return Array.Empty<byte>();
if (!response.IsSuccessStatusCode)
return Array.Empty<byte>();

return await response.Content.ReadAsByteArrayAsync();
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
catch (Exception e)
{
Trace.WriteLine(e.Message);
return Array.Empty<byte>();
}
}

static string GetGravatarUri(string email, int size, float scale, DefaultGravatar defaultGravatar)
Expand All @@ -113,10 +122,9 @@ static string GetMd5Hash(string str)

var sBuilder = new StringBuilder();

if (hash != null)
foreach (var hashByte in hash)
{
for (var i = 0; i < hash.Length; i++)
sBuilder.Append(hash[i].ToString("x2"));
sBuilder.Append(hashByte.ToString("x2"));
}

return sBuilder.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ void UpdateVolume()
if (avPlayerViewController.Player != null)
avPlayerViewController.Player.Volume = (float)Element.Volume;
}

void UpdateSpeed()
{
if (avPlayerViewController.Player != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using Xamarin.CommunityToolkit.UI.Views.Helpers;
using Xamarin.CommunityToolkit.UI.Views.Helpers;
using Xamarin.CommunityToolkit.UI.Views.Options;
using Xamarin.CommunityToolkit.Views.Snackbar.Helpers;
using Xamarin.Forms;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit 2803cb4

Please sign in to comment.