[Bug] GravatarImageSource throws exception when offline (#1816)
* [Bug] GravatarImageSource throws exception when offline #1770 Fix Nullable * ConfigureAwait, revert sln
This commit is contained in:
Родитель
b767630fa9
Коммит
2803cb433a
|
@ -1,4 +1,5 @@
|
|||
using Android.App;
|
||||
#nullable enable
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.Content.PM;
|
||||
using Android.OS;
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -99,9 +99,9 @@ namespace Xamarin.CommunityToolkit.iOS.Effects
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -26,7 +27,7 @@ namespace Xamarin.CommunityToolkit.UI.Views
|
|||
{
|
||||
_ = 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;
|
||||
|
@ -67,7 +68,7 @@ namespace Xamarin.CommunityToolkit.UI.Views
|
|||
{
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
if (!file.Directory.Exists)
|
||||
if (file.Directory is { Exists: false })
|
||||
file.Directory.Create();
|
||||
}
|
||||
finally
|
||||
|
@ -87,12 +88,20 @@ namespace Xamarin.CommunityToolkit.UI.Views
|
|||
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)
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return Array.Empty<byte>();
|
||||
|
||||
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Trace.WriteLine(e.Message);
|
||||
return Array.Empty<byte>();
|
||||
|
||||
return await response.Content.ReadAsByteArrayAsync();
|
||||
}
|
||||
}
|
||||
|
||||
static string GetGravatarUri(string email, int size, float scale, DefaultGravatar defaultGravatar)
|
||||
|
@ -113,10 +122,9 @@ namespace Xamarin.CommunityToolkit.UI.Views
|
|||
|
||||
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();
|
||||
|
|
|
@ -312,6 +312,7 @@ namespace Xamarin.CommunityToolkit.UI.Views
|
|||
if (avPlayerViewController.Player != null)
|
||||
avPlayerViewController.Player.Volume = (float)Element.Volume;
|
||||
}
|
||||
|
||||
void UpdateSpeed()
|
||||
{
|
||||
if (avPlayerViewController.Player != null)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче