< Summary

Class:ProfileHUDView
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs
Covered lines:77
Uncovered lines:24
Coverable lines:101
Total lines:318
Line coverage:76.2% (77 of 101)
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%110100%
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        loadingSpinner.SetActive(true);
 2188        userProfile.snapshotObserver.AddListener(SetProfileImage);
 2189    }
 190
 191    private void HandleClaimedProfileName(UserProfile userProfile)
 192    {
 1193        textName.text = userProfile.userName;
 1194        SetActiveUnverifiedNameGOs(false);
 1195    }
 196
 197    private void HandleUnverifiedProfileName(UserProfile userProfile)
 198    {
 1199        if (!String.IsNullOrEmpty(userProfile.userName) &&
 200            userProfile.userName.Length > NAME_POSTFIX_LENGTH)
 201        {
 1202            textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1);
 1203        }
 204        else
 205        {
 0206            textName.text = userProfile.userName;
 207        }
 208
 1209        textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}";
 1210        SetActiveUnverifiedNameGOs(true);
 1211    }
 212
 213    private void SetConnectedWalletSectionActive(bool active)
 214    {
 2215        connectedWalletSection.SetActive(active);
 2216        nonConnectedWalletSection.SetActive(!active);
 2217    }
 218
 219    private void SetActiveUnverifiedNameGOs(bool active)
 220    {
 16221        for (int i = 0; i < hideOnNameClaimed.Length; i++)
 222        {
 6223            hideOnNameClaimed[i].SetActive(active);
 224        }
 2225    }
 226
 227    private void HandleProfileAddress(UserProfile userProfile)
 228    {
 2229        string address = userProfile.userId;
 2230        string start = address.Substring(0, ADDRESS_CHUNK_LENGTH);
 2231        string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH);
 2232        textAddress.text = $"{start}...{end}";
 2233    }
 234
 235    private void SetProfileImage(Texture2D texture)
 236    {
 0237        loadingSpinner.SetActive(false);
 0238        imageAvatarThumbnail.texture = texture;
 0239    }
 240
 241    private void OnDestroy()
 242    {
 13243        if (profile)
 1244            profile.snapshotObserver.RemoveListener(SetProfileImage);
 13245    }
 246
 247    private void CopyAddress()
 248    {
 0249        if (!profile)
 250        {
 0251            return;
 252        }
 253
 0254        DCL.Environment.i.platform.clipboard.WriteText(profile.userId);
 255
 0256        copyTooltip.gameObject.SetActive(false);
 0257        if (copyToastRoutine != null)
 258        {
 0259            StopCoroutine(copyToastRoutine);
 260        }
 261
 0262        copyToastRoutine = StartCoroutine(ShowCopyToast());
 0263    }
 264
 265    private IEnumerator ShowCopyToast()
 266    {
 0267        if (!copyToast.gameObject.activeSelf)
 268        {
 0269            copyToast.gameObject.SetActive(true);
 270        }
 271
 0272        copyToast.Show();
 0273        yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME);
 0274        copyToast.Hide();
 0275    }
 276
 26277    private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; }
 278
 26279    private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; }
 280
 281    internal void SetBackpackButtonVisibility(bool visible)
 282    {
 17283        if (visible && !buttonBackpack.gameObject.activeSelf)
 2284            buttonBackpack.gameObject.SetActive(true);
 15285        else if (!visible && buttonBackpack.gameObject.activeSelf)
 14286            buttonBackpack.gameObject.SetActive(false);
 15287    }
 288
 289    internal void ActivateProfileNameEditionMode(bool activate)
 290    {
 2291        if (profile != null && profile.hasClaimedName)
 0292            return;
 293
 2294        editNameTooltipGO.SetActive(!activate);
 2295        textName.gameObject.SetActive(!activate);
 2296        inputName.gameObject.SetActive(activate);
 297
 2298        if (activate)
 299        {
 1300            inputName.text = textName.text;
 1301            inputName.Select();
 302        }
 2303    }
 304
 6305    private void UpdateCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLimit}"
 306
 2307    internal void SetProfileName(string newName) { textName.text = newName; }
 308
 0309    internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); }
 310
 311    internal bool IsValidAvatarName(string name)
 312    {
 0313        if (nameRegex == null)
 0314            return true;
 315
 0316        return nameRegex.IsMatch(name);
 317    }
 318}