< Summary

Class:ProfileHUDView
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs
Covered lines:80
Uncovered lines:27
Coverable lines:107
Total lines:333
Line coverage:74.7% (80 of 107)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
SetProfile(...)0%220100%
ToggleMenu()0%330100%
HideMenu()0%330100%
SetVisibility(...)0%5.25080%
HandleProfileSnapshot(...)0%3.143075%
HandleClaimedProfileName(...)0%110100%
HandleUnverifiedProfileName(...)0%3.033085.71%
SetConnectedWalletSectionActive(...)0%110100%
SetActiveUnverifiedNameGOs(...)0%220100%
HandleProfileAddress(...)0%110100%
SetProfileImage(...)0%2100%
OnDestroy()0%220100%
CopyAddress()0%12300%
ShowCopyToast()0%20400%
OnEnable()0%110100%
OnDisable()0%110100%
SetBackpackButtonVisibility(...)0%550100%
ActivateProfileNameEditionMode(...)0%4.024088.89%
UpdateCharLimit(...)0%110100%
SetProfileName(...)0%110100%
SetNameRegex(...)0%2100%
IsValidAvatarName(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Text.RegularExpressions;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8internal class ProfileHUDView : MonoBehaviour
 9{
 10    private const int ADDRESS_CHUNK_LENGTH = 6;
 11    private const int NAME_POSTFIX_LENGTH = 4;
 12    private const float COPY_TOAST_VISIBLE_TIME = 3;
 13
 14    [SerializeField]
 15    internal ShowHideAnimator mainShowHideAnimator;
 16
 17    [SerializeField]
 18    internal ShowHideAnimator menuShowHideAnimator;
 19
 20    [SerializeField]
 21    internal GameObject loadingSpinner;
 22
 23    [SerializeField]
 24    internal ShowHideAnimator copyToast;
 25
 26    [SerializeField]
 27    internal GameObject copyTooltip;
 28
 29    [SerializeField]
 30    internal InputAction_Trigger closeAction;
 31
 32    [Header("Hide GOs on claimed name")]
 33    [SerializeField]
 34    internal GameObject[] hideOnNameClaimed;
 35
 36    [Header("Connected wallet sections")]
 37    [SerializeField]
 38    internal GameObject connectedWalletSection;
 39
 40    [SerializeField]
 41    internal GameObject nonConnectedWalletSection;
 42
 43    [Header("Thumbnail")]
 44    [SerializeField]
 45    internal RawImage imageAvatarThumbnail;
 46
 47    [SerializeField]
 48    internal Button buttonToggleMenu;
 49
 50    [Header("Texts")]
 51    [SerializeField]
 52    internal TextMeshProUGUI textName;
 53
 54    [SerializeField]
 55    internal TextMeshProUGUI textPostfix;
 56
 57    [SerializeField]
 58    internal TextMeshProUGUI textAddress;
 59
 60    [Header("Buttons")]
 61    [SerializeField]
 62    internal Button buttonClaimName;
 63
 64    [SerializeField]
 65    internal Button buttonBackpack;
 66
 67    [SerializeField]
 68    internal Button buttonCopyAddress;
 69
 70    [SerializeField]
 71    internal Button buttonLogOut;
 72
 73    [SerializeField]
 74    internal Button buttonSignUp;
 75
 76    [SerializeField]
 77    internal Button_OnPointerDown buttonTermsOfServiceForConnectedWallets;
 78
 79    [SerializeField]
 80    internal Button_OnPointerDown buttonPrivacyPolicyForConnectedWallets;
 81
 82    [SerializeField]
 83    internal Button_OnPointerDown buttonTermsOfServiceForNonConnectedWallets;
 84
 85    [SerializeField]
 86    internal Button_OnPointerDown buttonPrivacyPolicyForNonConnectedWallets;
 87
 88    [Header("Name Edition")]
 89    [SerializeField]
 90    internal GameObject editNameTooltipGO;
 91
 92    [SerializeField]
 93    internal Button_OnPointerDown buttonEditName;
 94
 95    [SerializeField]
 96    internal Button_OnPointerDown buttonEditNamePrefix;
 97
 98    [SerializeField]
 99    internal TMP_InputField inputName;
 100
 101    [SerializeField]
 102    internal TextMeshProUGUI textCharLimit;
 103
 104    [SerializeField]
 105    internal ManaCounterView manaCounterView;
 106
 107    [SerializeField]
 108    internal ManaCounterView polygonManaCounterView;
 109
 110    [Header("Tutorial Config")]
 111    [SerializeField]
 112    internal RectTransform backpackTooltipReference;
 113
 114    private InputAction_Trigger.Triggered closeActionDelegate;
 115
 116    private Coroutine copyToastRoutine = null;
 117    private UserProfile profile = null;
 118    private Regex nameRegex = null;
 119
 120    internal event Action OnOpen;
 121    internal event Action OnClose;
 122
 123    private void Awake()
 124    {
 13125        closeActionDelegate = (x) => HideMenu();
 126
 13127        buttonToggleMenu.onClick.AddListener(ToggleMenu);
 13128        buttonCopyAddress.onClick.AddListener(CopyAddress);
 13129        buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true);
 13130        buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true);
 13131        inputName.onValueChanged.AddListener(UpdateCharLimit);
 13132        inputName.onDeselect.AddListener((x) => ActivateProfileNameEditionMode(false));
 13133        copyToast.gameObject.SetActive(false);
 13134    }
 135
 136    internal void SetProfile(UserProfile userProfile)
 137    {
 2138        profile = userProfile;
 2139        if (userProfile.hasClaimedName)
 140        {
 1141            HandleClaimedProfileName(userProfile);
 1142        }
 143        else
 144        {
 1145            HandleUnverifiedProfileName(userProfile);
 146        }
 147
 2148        SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3);
 2149        HandleProfileAddress(userProfile);
 2150        HandleProfileSnapshot(userProfile);
 2151    }
 152
 153    internal void ToggleMenu()
 154    {
 2155        if (menuShowHideAnimator.isVisible)
 156        {
 1157            HideMenu();
 1158        }
 159        else
 160        {
 1161            menuShowHideAnimator.Show();
 1162            CommonScriptableObjects.isProfileHUDOpen.Set(true);
 1163            OnOpen?.Invoke();
 164        }
 1165    }
 166
 167    internal void HideMenu()
 168    {
 1169        if (menuShowHideAnimator.isVisible)
 170        {
 1171            menuShowHideAnimator.Hide();
 1172            CommonScriptableObjects.isProfileHUDOpen.Set(false);
 1173            OnClose?.Invoke();
 174        }
 1175    }
 176
 177    internal void SetVisibility(bool visible)
 178    {
 3179        if (visible && !mainShowHideAnimator.isVisible)
 0180            mainShowHideAnimator.Show();
 3181        else if (!visible && mainShowHideAnimator.isVisible)
 1182            mainShowHideAnimator.Hide();
 3183    }
 184
 185    private void HandleProfileSnapshot(UserProfile userProfile)
 186    {
 2187        if (profile)
 188        {
 2189            profile.OnFaceSnapshotReadyEvent -= SetProfileImage;
 190        }
 191
 2192        if (userProfile.faceSnapshot != null)
 193        {
 0194            SetProfileImage(userProfile.faceSnapshot);
 0195        }
 196        else
 197        {
 2198            loadingSpinner.SetActive(true);
 2199            userProfile.OnFaceSnapshotReadyEvent += SetProfileImage;
 200        }
 2201    }
 202
 203    private void HandleClaimedProfileName(UserProfile userProfile)
 204    {
 1205        textName.text = userProfile.userName;
 1206        SetActiveUnverifiedNameGOs(false);
 1207    }
 208
 209    private void HandleUnverifiedProfileName(UserProfile userProfile)
 210    {
 1211        if (!String.IsNullOrEmpty(userProfile.userName) &&
 212            userProfile.userName.Length > NAME_POSTFIX_LENGTH)
 213        {
 1214            textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1);
 1215        }
 216        else
 217        {
 0218            textName.text = userProfile.userName;
 219        }
 220
 1221        textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}";
 1222        SetActiveUnverifiedNameGOs(true);
 1223    }
 224
 225    private void SetConnectedWalletSectionActive(bool active)
 226    {
 2227        connectedWalletSection.SetActive(active);
 2228        nonConnectedWalletSection.SetActive(!active);
 2229    }
 230
 231    private void SetActiveUnverifiedNameGOs(bool active)
 232    {
 16233        for (int i = 0; i < hideOnNameClaimed.Length; i++)
 234        {
 6235            hideOnNameClaimed[i].SetActive(active);
 236        }
 2237    }
 238
 239    private void HandleProfileAddress(UserProfile userProfile)
 240    {
 2241        string address = userProfile.userId;
 2242        string start = address.Substring(0, ADDRESS_CHUNK_LENGTH);
 2243        string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH);
 2244        textAddress.text = $"{start}...{end}";
 2245    }
 246
 247    private void SetProfileImage(Texture2D texture)
 248    {
 0249        profile.OnFaceSnapshotReadyEvent -= SetProfileImage;
 0250        imageAvatarThumbnail.texture = texture;
 0251        loadingSpinner.SetActive(false);
 0252    }
 253
 254    private void OnDestroy()
 255    {
 13256        if (profile)
 257        {
 1258            profile.OnFaceSnapshotReadyEvent -= SetProfileImage;
 259        }
 13260    }
 261
 262    private void CopyAddress()
 263    {
 0264        if (!profile)
 265        {
 0266            return;
 267        }
 268
 0269        DCL.Environment.i.platform.clipboard.WriteText(profile.userId);
 270
 0271        copyTooltip.gameObject.SetActive(false);
 0272        if (copyToastRoutine != null)
 273        {
 0274            StopCoroutine(copyToastRoutine);
 275        }
 276
 0277        copyToastRoutine = StartCoroutine(ShowCopyToast());
 0278    }
 279
 280    private IEnumerator ShowCopyToast()
 281    {
 0282        if (!copyToast.gameObject.activeSelf)
 283        {
 0284            copyToast.gameObject.SetActive(true);
 285        }
 286
 0287        copyToast.Show();
 0288        yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME);
 0289        copyToast.Hide();
 0290    }
 291
 26292    private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; }
 293
 26294    private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; }
 295
 296    internal void SetBackpackButtonVisibility(bool visible)
 297    {
 17298        if (visible && !buttonBackpack.gameObject.activeSelf)
 2299            buttonBackpack.gameObject.SetActive(true);
 15300        else if (!visible && buttonBackpack.gameObject.activeSelf)
 14301            buttonBackpack.gameObject.SetActive(false);
 15302    }
 303
 304    internal void ActivateProfileNameEditionMode(bool activate)
 305    {
 2306        if (profile != null && profile.hasClaimedName)
 0307            return;
 308
 2309        editNameTooltipGO.SetActive(!activate);
 2310        textName.gameObject.SetActive(!activate);
 2311        inputName.gameObject.SetActive(activate);
 312
 2313        if (activate)
 314        {
 1315            inputName.text = textName.text;
 1316            inputName.Select();
 317        }
 2318    }
 319
 6320    private void UpdateCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLimit}"
 321
 2322    internal void SetProfileName(string newName) { textName.text = newName; }
 323
 0324    internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); }
 325
 326    internal bool IsValidAvatarName(string name)
 327    {
 0328        if (nameRegex == null)
 0329            return true;
 330
 0331        return nameRegex.IsMatch(name);
 332    }
 333}