Skip to content

Commit 19f4c49

Browse files
committed
Improve avatar image loading
1 parent 1dfb65b commit 19f4c49

3 files changed

Lines changed: 76 additions & 26 deletions

File tree

Packages/com.walletconnect.web3modal/Runtime/Controllers/AccountController.cs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.CompilerServices;
66
using System.Threading.Tasks;
77
using UnityEngine;
8+
using WalletConnect.Web3Modal.Http;
89
using WalletConnect.Web3Modal.Utils;
910

1011
namespace WalletConnect.Web3Modal
@@ -41,8 +42,8 @@ public string ProfileName
4142
get => _profileName;
4243
set => SetField(ref _profileName, value);
4344
}
44-
45-
public string ProfileAvatar
45+
46+
public AccountAvatar ProfileAvatar
4647
{
4748
get => _profileAvatar;
4849
set => SetField(ref _profileAvatar, value);
@@ -63,13 +64,15 @@ public string BalanceSymbol
6364
private ConnectorController _connectorController;
6465
private NetworkController _networkController;
6566
private BlockchainApiController _blockchainApiController;
67+
68+
private UnityHttpClient _httpClient = new();
6669

6770
private string _address;
6871
private string _accountId;
6972
private string _chainId;
7073

7174
private string _profileName;
72-
private string _profileAvatar;
75+
private AccountAvatar _profileAvatar;
7376

7477
private string _balance;
7578
private string _balanceSymbol;
@@ -100,23 +103,23 @@ private async void ConnectorAccountConnectedHandler(object sender, Connector.Acc
100103
ChainId = account.ChainId;
101104

102105
await Task.WhenAll(
103-
UpdateProfile(),
104-
UpdateBalance()
106+
UpdateBalance(),
107+
UpdateProfile()
105108
);
106109
}
107110

108111
private async void ConnectorAccountChangedHandler(object sender, Connector.AccountChangedEventArgs e)
109112
{
110-
if (e.Account.Address != Address)
111-
{
112-
Address = e.Account.Address;
113-
await UpdateProfile();
114-
}
115-
113+
var oldAddress = Address;
114+
115+
Address = e.Account.Address;
116116
AccountId = e.Account.AccountId;
117117
ChainId = e.Account.ChainId;
118-
119-
await UpdateBalance();
118+
119+
await Task.WhenAll(
120+
UpdateBalance(),
121+
e.Account.Address != oldAddress ? UpdateProfile() : Task.CompletedTask
122+
);
120123
}
121124

122125
public async Task UpdateProfile()
@@ -126,7 +129,24 @@ public async Task UpdateProfile()
126129
? Address.Truncate()
127130
: identity.Name;
128131

129-
ProfileAvatar = identity.Avatar ?? string.Empty;
132+
if (!string.IsNullOrWhiteSpace(identity.Avatar))
133+
{
134+
try
135+
{
136+
var headers = await _httpClient.HeadAsync(identity.Avatar);
137+
var avatarFormat = headers["Content-Type"].Split('/').Last();
138+
ProfileAvatar = new AccountAvatar(identity.Avatar, avatarFormat);
139+
}
140+
catch (Exception e)
141+
{
142+
ProfileAvatar = default;
143+
}
144+
}
145+
else
146+
147+
{
148+
ProfileAvatar = default;
149+
}
130150
}
131151

132152
public async Task UpdateBalance()
@@ -159,5 +179,23 @@ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string prope
159179
OnPropertyChanged(propertyName);
160180
return true;
161181
}
182+
183+
}
184+
185+
public readonly struct AccountAvatar
186+
{
187+
public readonly string AvatarUrl;
188+
public readonly string AvatarFormat;
189+
190+
public AccountAvatar(string avatarUrl, string avatarFormat)
191+
{
192+
AvatarUrl = avatarUrl;
193+
AvatarFormat = avatarFormat;
194+
}
195+
196+
public bool IsEmpty
197+
{
198+
get => string.IsNullOrWhiteSpace(AvatarUrl);
199+
}
162200
}
163201
}

Packages/com.walletconnect.web3modal/Runtime/Http/UnityHttpClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public class UnityHttpClient : HttpClientDecorator
9292
private readonly HttpClientDecorator[] _decorators;
9393
private readonly Func<HttpRequestContext, CancellationToken, Task<HttpResponseContext>> _next;
9494

95+
public UnityHttpClient(params HttpClientDecorator[] decorators) : this(null, TimeSpan.FromSeconds(5), decorators)
96+
{
97+
}
98+
9599
public UnityHttpClient(Uri basePath, TimeSpan timeout, params HttpClientDecorator[] decorators)
96100
{
97101
_basePath = basePath;
@@ -131,6 +135,15 @@ public async Task<T> GetAsync<T>(string path, IDictionary<string, string> parame
131135
return response.GetResponseAs<T>();
132136
}
133137

138+
public async Task<IDictionary<string, string>> HeadAsync(string path, IDictionary<string, string> parameters = null, IDictionary<string, string> headers = null)
139+
{
140+
path = path.AppendQueryString(parameters);
141+
142+
var request = new HttpRequestContext(path, "HEAD", null, null, headers, _decorators);
143+
var response = await InvokeRecursive(request, CancellationToken.None);
144+
return response.ResponseHeaders;
145+
}
146+
134147
protected override Task<HttpResponseContext> SendAsyncCore(HttpRequestContext requestContext, CancellationToken cancellationToken, Func<HttpRequestContext, CancellationToken, Task<HttpResponseContext>> next)
135148
{
136149
var url = _basePath != null
@@ -175,7 +188,7 @@ protected override Task<HttpResponseContext> SendAsyncCore(HttpRequestContext re
175188
{
176189
if (uwr.result != UnityWebRequest.Result.Success)
177190
{
178-
tcs.SetException(new Exception($"Failed to send web request: {uwr.error}")); // TODO: use custom ex type
191+
tcs.SetException(new Exception($"Failed to send web request: {uwr.error}. Url: {url.ToString()}")); // TODO: use custom ex type
179192
uwr.Dispose();
180193
return;
181194
}

Packages/com.walletconnect.web3modal/Runtime/Presenters/AccountPresenter.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ private void CreateButtons(VisualElement view)
6464

6565
private void AccountPropertyChangedHandler(object sender, PropertyChangedEventArgs e)
6666
{
67-
Debug.Log($"AccountPropertyChangedHandler: {e.PropertyName}");
6867
switch (e.PropertyName)
6968
{
7069
case nameof(AccountController.ProfileName):
7170
UpdateProfileName();
7271
break;
72+
case nameof(AccountController.Address):
7373
case nameof(AccountController.ProfileAvatar):
7474
UpdateProfileAvatar();
7575
break;
@@ -94,22 +94,21 @@ private void UpdateProfileName()
9494

9595
private void UpdateProfileAvatar()
9696
{
97-
var avatarUrl = Web3Modal.AccountController.ProfileAvatar;
97+
var avatar = Web3Modal.AccountController.ProfileAvatar;
9898

99-
if (!string.IsNullOrEmpty(avatarUrl) && (avatarUrl.EndsWith(".jpg") || avatarUrl.EndsWith(".png") || avatarUrl.EndsWith(".jpeg")))
99+
if (avatar.IsEmpty || avatar.AvatarFormat != "png" && avatar.AvatarFormat != "jpg" && avatar.AvatarFormat != "jpeg")
100100
{
101-
var remoteSprite = RemoteSpriteFactory.GetRemoteSprite<Image>(avatarUrl);
102-
_avatar?.UnsubscribeImage(View.ProfileAvatarImage);
103-
_avatar = remoteSprite;
104-
_avatar.SubscribeImage(View.ProfileAvatarImage);
105-
}
106-
else
107-
{
108-
109101
var address = Web3Modal.AccountController.Address;
110102
var texture = UiUtils.GenerateAvatarTexture(address);
111103
View.ProfileAvatarImage.image = texture;
112104
}
105+
else
106+
{
107+
var remoteSprite = RemoteSpriteFactory.GetRemoteSprite<Image>(avatar.AvatarUrl);
108+
_avatar?.UnsubscribeImage(View.ProfileAvatarImage);
109+
_avatar = remoteSprite;
110+
_avatar.SubscribeImage(View.ProfileAvatarImage);
111+
}
113112
}
114113

115114
protected override void OnVisibleCore()

0 commit comments

Comments
 (0)