Skip to content

Commit dc8843c

Browse files
committed
Account avatar
1 parent e951be4 commit dc8843c

11 files changed

Lines changed: 683 additions & 117 deletions

File tree

Packages/com.walletconnect.web3modal/Resources/Fonts & Materials/Inter/Inter-Medium SDF.asset

Lines changed: 229 additions & 40 deletions
Large diffs are not rendered by default.

Packages/com.walletconnect.web3modal/Resources/Fonts & Materials/Inter/Inter-SemiBold SDF.asset

Lines changed: 324 additions & 54 deletions
Large diffs are not rendered by default.

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Components/Avatar.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Views/AccountView/AccountView.uss

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@
44
align-items: center;
55
justify-content: center;
66
margin-bottom: 20px;
7-
padding-top: 40px;
7+
margin-top: -20px;
8+
}
9+
10+
#account-view__profile-avatar {
11+
width: 75px;
12+
height: 75px;
13+
border-radius: 100%;
14+
border-width: 8px;
15+
border-color: var(--wui-gray-glass-005);
16+
overflow: hidden;
17+
align-items: center;
18+
}
19+
20+
#account-view__profile-avatar-image {
21+
width: 110%;
22+
height: 110%;
823
}
924

1025
#account-view__profile-address {
11-
margin-top: 20px;
26+
margin-top: 10px;
1227
padding: 0;
1328
-unity-text-align: middle-center;
1429
color: var(--wui-color-fg-100);

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Views/AccountView/AccountView.uxml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
22
<Style src="AccountView.uss"/>
33
<ui:VisualElement name="account-view__profile">
4+
<ui:VisualElement name="account-view__profile-avatar">
5+
<ui:Image name="account-view__profile-avatar-image"/>
6+
</ui:VisualElement>
47
<ui:Label name="account-view__profile-address"/>
58
<ui:VisualElement name="account-view__profile-balance-container">
69
<ui:Label name="account-view__profile-balance-value"/>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public async Task InitializeAsync(ConnectorController connectorController, Netwo
9292
private async void ConnectorAccountConnectedHandler(object sender, Connector.AccountConnectedEventArgs e)
9393
{
9494
var account = await e.GetAccount();
95-
9695
if (account.AccountId == AccountId)
9796
return;
9897

@@ -108,7 +107,6 @@ await Task.WhenAll(
108107

109108
private async void ConnectorAccountChangedHandler(object sender, Connector.AccountChangedEventArgs e)
110109
{
111-
Debug.Log("ConnectorAccountChangedHandler");
112110
if (e.Account.Address != Address)
113111
{
114112
Address = e.Account.Address;
@@ -127,7 +125,8 @@ public async Task UpdateProfile()
127125
ProfileName = string.IsNullOrWhiteSpace(identity.Name)
128126
? Address.Truncate()
129127
: identity.Name;
130-
ProfileAvatar = identity.Avatar;
128+
129+
ProfileAvatar = identity.Avatar ?? string.Empty;
131130
}
132131

133132
public async Task UpdateBalance()

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4-
using UnityEngine;
54
using WalletConnect.Web3Modal.Http;
65
using WalletConnect.Web3Modal.Model.BlockchainApi;
76
using WalletConnectUnity.Core;
@@ -22,14 +21,12 @@ public class BlockchainApiController
2221

2322
public async Task<GetIdentityResponse> GetIdentityAsync(string address)
2423
{
25-
Debug.Log("GetIdentityAsync");
2624
var projectId = ProjectConfiguration.Load().Id;
2725
return await _httpClient.GetAsync<GetIdentityResponse>($"identity/{address}?projectId={projectId}");
2826
}
2927

3028
public async Task<GetBalanceResponse> GetBalanceAsync(string address)
3129
{
32-
Debug.Log("GetBalanceAsync");
3330
var projectId = ProjectConfiguration.Load().Id;
3431
return await _httpClient.GetAsync<GetBalanceResponse>($"account/{address}/balance?projectId={projectId}&currency=usd", headers: _getBalanceHeaders);
3532
}

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.ComponentModel;
33
using System.Threading.Tasks;
4+
using Unity.VectorGraphics;
45
using UnityEngine;
56
using UnityEngine.UIElements;
67
using WalletConnect.UI;
@@ -21,6 +22,7 @@ public override bool HeaderBorder
2122

2223
private ListItem _networkButton;
2324
private RemoteSprite<Image> _networkIcon;
25+
private RemoteSprite<Image> _avatar;
2426

2527
public AccountPresenter(RouterController router, VisualElement parent) : base(router)
2628
{
@@ -68,6 +70,9 @@ private void AccountPropertyChangedHandler(object sender, PropertyChangedEventAr
6870
case nameof(AccountController.ProfileName):
6971
UpdateProfileName();
7072
break;
73+
case nameof(AccountController.ProfileAvatar):
74+
UpdateProfileAvatar();
75+
break;
7176
case nameof(AccountController.Balance):
7277
View.SetBalance(TrimToThreeDecimalPlaces(Web3Modal.AccountController.Balance));
7378
break;
@@ -87,15 +92,24 @@ private void UpdateProfileName()
8792
View.SetProfileName(profileName);
8893
}
8994

90-
// private void ChainChangedHandler(object sender, NetworkController.ChainChangedEventArgs e)
91-
// {
92-
// UpdateNetworkButton(e.Chain);
93-
// }
94-
95-
// private async void AccountChangedHandler(object sender, Connector.AccountChangedEventArgs e)
96-
// {
97-
// await UpdateIdentity(e.Account.Address);
98-
// }
95+
private void UpdateProfileAvatar()
96+
{
97+
var avatarUrl = Web3Modal.AccountController.ProfileAvatar;
98+
99+
if (string.IsNullOrEmpty(avatarUrl))
100+
{
101+
var address = Web3Modal.AccountController.Address;
102+
var texture = UiUtils.GenerateAvatarTexture(address);
103+
View.ProfileAvatarImage.image = texture;
104+
}
105+
else
106+
{
107+
var remoteSprite = RemoteSpriteFactory.GetRemoteSprite<Image>(avatarUrl);
108+
_avatar?.UnsubscribeImage(View.ProfileAvatarImage);
109+
_avatar = remoteSprite;
110+
_avatar.SubscribeImage(View.ProfileAvatarImage);
111+
}
112+
}
99113

100114
protected override void OnVisibleCore()
101115
{
@@ -132,12 +146,6 @@ private void UpdateNetworkButton(Chain chain)
132146
_networkButton.ApplyIconStyle(ListItem.IconStyle.Default);
133147
}
134148

135-
private async Task UpdateIdentity(string address)
136-
{
137-
var identity = await Web3Modal.BlockchainApiController.GetIdentityAsync(address);
138-
139-
}
140-
141149
private async void OnDisconnect()
142150
{
143151
try
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections.Generic;
2+
using Unity.VectorGraphics;
3+
using UnityEngine;
4+
5+
namespace WalletConnect.Web3Modal.Utils
6+
{
7+
public class UiUtils
8+
{
9+
private static GradientStop[] GenerateGradientStops(Color baseColor)
10+
{
11+
var stops = new GradientStop[5];
12+
for (var i = 0; i < 5; i++)
13+
{
14+
var tint = 0.25f * i;
15+
var tintedColor = Color.Lerp(Color.white, baseColor, tint);
16+
stops[i] = new GradientStop()
17+
{
18+
Color = tintedColor,
19+
StopPercentage = i * 0.25f
20+
};
21+
}
22+
23+
return stops;
24+
}
25+
26+
public static Texture2D GenerateAvatarTexture(string address)
27+
{
28+
var baseColorHex = address.Substring(2, 6);
29+
if (!ColorUtility.TryParseHtmlString($"#{baseColorHex}", out var baseColor))
30+
return null;
31+
32+
var circleShape = new Shape
33+
{
34+
Fill = new GradientFill
35+
{
36+
Type = GradientFillType.Radial,
37+
Stops = GenerateGradientStops(baseColor),
38+
RadialFocus = new Vector2(0.3f, -0.2f)
39+
}
40+
};
41+
VectorUtils.MakeCircleShape(circleShape, Vector2.zero, 4.0f);
42+
43+
var scene = new Scene()
44+
{
45+
Root = new SceneNode()
46+
{
47+
Shapes = new List<Shape>
48+
{
49+
circleShape
50+
}
51+
}
52+
};
53+
54+
var tessOptions = new VectorUtils.TessellationOptions()
55+
{
56+
StepDistance = 100.0f,
57+
MaxCordDeviation = 0.5f,
58+
MaxTanAngleDeviation = 0.1f,
59+
SamplingStepSize = 0.01f
60+
};
61+
62+
var geoms = VectorUtils.TessellateScene(scene, tessOptions);
63+
var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 16, true);
64+
65+
var mat = new Material(Shader.Find("Unlit/VectorGradient"));
66+
var texture = VectorUtils.RenderSpriteToTexture2D(sprite, 128, 128, mat);
67+
68+
return texture;
69+
}
70+
}
71+
}

Packages/com.walletconnect.web3modal/Runtime/Utils/UiUtils.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)