< Summary

Class:ProfileHUDView
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs
Covered lines:91
Uncovered lines:42
Coverable lines:133
Total lines:399
Line coverage:68.4% (91 of 133)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
SetProfile(...)0%220100%
OpenStartMenu()0%12300%
SetStartMenuButtonActive(...)0%2100%
ToggleMenu()0%330100%
HideMenu()0%330100%
SetVisibility(...)0%5.25080%
HandleProfileSnapshot(...)0%110100%
HandleClaimedProfileName(...)0%110100%
HandleUnverifiedProfileName(...)0%3.033085.71%
SetConnectedWalletSectionActive(...)0%110100%
ForceLayoutToRefreshSize()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%
ActivateProfileNameEditionMode(...)0%4.034087.5%
UpdateNameCharLimit(...)0%110100%
SetProfileName(...)0%110100%
SetNameRegex(...)0%2100%
IsValidAvatarName(...)0%6200%
ActivateDescriptionEditionMode(...)0%220100%
SelectComponentOnNextFrame()0%5.673033.33%
SetDescription(...)0%110100%
UpdateDescriptionCharLimit(...)0%110100%
SetDescriptionEnabled(...)0%110100%
SetCardAsFullScreenMenuMode(...)0%12300%

File(s)

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

#LineLine coverage
 1using DCL;
 2using ExploreV2Analytics;
 3using System;
 4using System.Collections;
 5using System.Text.RegularExpressions;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.UI;
 9using Environment = DCL.Environment;
 10
 11public class ProfileHUDView : MonoBehaviour
 12{
 13    private const int ADDRESS_CHUNK_LENGTH = 6;
 14    private const int NAME_POSTFIX_LENGTH = 4;
 15    private const float COPY_TOAST_VISIBLE_TIME = 3;
 16
 17    [SerializeField]
 18    internal ShowHideAnimator mainShowHideAnimator;
 19
 20    [SerializeField]
 21    internal ShowHideAnimator menuShowHideAnimator;
 22
 23    [SerializeField]
 24    private RectTransform mainRootLayout;
 25
 26    [SerializeField]
 27    internal GameObject loadingSpinner;
 28
 29    [SerializeField]
 30    internal ShowHideAnimator copyToast;
 31
 32    [SerializeField]
 33    internal GameObject copyTooltip;
 34
 35    [SerializeField]
 36    internal InputAction_Trigger closeAction;
 37
 38    [SerializeField]
 39    internal Canvas mainCanvas;
 40
 41    [Header("Hide GOs on claimed name")]
 42    [SerializeField]
 43    internal GameObject[] hideOnNameClaimed;
 44
 45    [Header("Connected wallet sections")]
 46    [SerializeField]
 47    internal GameObject connectedWalletSection;
 48
 49    [SerializeField]
 50    internal GameObject nonConnectedWalletSection;
 51
 52    [Header("Thumbnail")]
 53    [SerializeField]
 54    internal RawImage imageAvatarThumbnail;
 55
 56    [SerializeField]
 57    internal Button buttonToggleMenu;
 58
 59    [Header("Texts")]
 60    [SerializeField]
 61    internal TextMeshProUGUI textName;
 62
 63    [SerializeField]
 64    internal TextMeshProUGUI textPostfix;
 65
 66    [SerializeField]
 67    internal TextMeshProUGUI textAddress;
 68
 69    [Header("Buttons")]
 70    [SerializeField]
 71    internal Button buttonClaimName;
 72
 73    [SerializeField]
 74    internal Button buttonCopyAddress;
 75
 76    [SerializeField]
 77    internal Button buttonLogOut;
 78
 79    [SerializeField]
 80    internal Button buttonSignUp;
 81
 82    [SerializeField]
 83    internal Button_OnPointerDown buttonTermsOfServiceForConnectedWallets;
 84
 85    [SerializeField]
 86    internal Button_OnPointerDown buttonPrivacyPolicyForConnectedWallets;
 87
 88    [SerializeField]
 89    internal Button_OnPointerDown buttonTermsOfServiceForNonConnectedWallets;
 90
 91    [SerializeField]
 92    internal Button_OnPointerDown buttonPrivacyPolicyForNonConnectedWallets;
 93
 94    [Header("Name Edition")]
 95    [SerializeField]
 96    internal Button_OnPointerDown buttonEditName;
 97
 98    [SerializeField]
 99    internal Button_OnPointerDown buttonEditNamePrefix;
 100
 101    [SerializeField]
 102    internal TMP_InputField inputName;
 103
 104    [SerializeField]
 105    internal TextMeshProUGUI textCharLimit;
 106
 107    [SerializeField]
 108    internal ManaCounterView manaCounterView;
 109
 110    [SerializeField]
 111    internal ManaCounterView polygonManaCounterView;
 112
 113    [Header("Tutorial Config")]
 114    [SerializeField]
 115    internal RectTransform tutorialTooltipReference;
 116
 117    [Header("Description")]
 118    [SerializeField]
 119    internal TMP_InputField descriptionPreviewInput;
 120
 121    [SerializeField]
 122    internal TMP_InputField descriptionEditionInput;
 123
 124    [SerializeField]
 125    internal GameObject charLimitDescriptionContainer;
 126
 127    [SerializeField]
 128    internal TextMeshProUGUI textCharLimitDescription;
 129
 130    [SerializeField]
 131    internal GameObject descriptionContainer;
 132
 0133    public RectTransform expandedMenu => mainRootLayout;
 134
 135    private InputAction_Trigger.Triggered closeActionDelegate;
 136
 137    private Coroutine copyToastRoutine = null;
 138    private UserProfile profile = null;
 139    private Regex nameRegex = null;
 140
 141    internal event Action OnOpen;
 142    internal event Action OnClose;
 143    internal bool isStartMenuInitialized = false;
 144
 145    private void Awake()
 146    {
 17147        closeActionDelegate = (x) => HideMenu();
 148
 17149        buttonToggleMenu.onClick.AddListener(OpenStartMenu);
 17150        buttonCopyAddress.onClick.AddListener(CopyAddress);
 17151        buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true);
 17152        buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true);
 17153        inputName.onValueChanged.AddListener(UpdateNameCharLimit);
 17154        inputName.onDeselect.AddListener((x) => ActivateProfileNameEditionMode(false));
 17155        descriptionPreviewInput.onSelect.AddListener(x =>
 156        {
 0157            ActivateDescriptionEditionMode(true);
 0158            UpdateDescriptionCharLimit(descriptionPreviewInput.text);
 0159        });
 17160        descriptionEditionInput.onValueChanged.AddListener(UpdateDescriptionCharLimit);
 17161        descriptionEditionInput.onDeselect.AddListener(x => ActivateDescriptionEditionMode(false));
 17162        copyToast.gameObject.SetActive(false);
 17163    }
 164
 165    internal void SetProfile(UserProfile userProfile)
 166    {
 5167        profile = userProfile;
 5168        if (userProfile.hasClaimedName)
 169        {
 1170            HandleClaimedProfileName(userProfile);
 1171        }
 172        else
 173        {
 4174            HandleUnverifiedProfileName(userProfile);
 175        }
 176
 5177        SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3);
 5178        HandleProfileAddress(userProfile);
 5179        HandleProfileSnapshot(userProfile);
 5180        SetDescription(userProfile.description);
 5181        SetDescriptionEnabled(userProfile.hasConnectedWeb3);
 5182        ForceLayoutToRefreshSize();
 5183    }
 184
 185    internal void OpenStartMenu()
 186    {
 0187        if (isStartMenuInitialized)
 188        {
 0189            if (!DataStore.i.exploreV2.isOpen.Get())
 190            {
 0191                var exploreV2Analytics = new ExploreV2Analytics.ExploreV2Analytics();
 0192                exploreV2Analytics.SendStartMenuVisibility(
 193                    true,
 194                    ExploreUIVisibilityMethod.FromClick);
 195            }
 0196            DataStore.i.exploreV2.isOpen.Set(true);
 0197        }
 198        else
 199        {
 0200            ToggleMenu();
 201        }
 0202    }
 203
 0204    public void SetStartMenuButtonActive(bool isActive) { isStartMenuInitialized = isActive; }
 205
 206    internal void ToggleMenu()
 207    {
 2208        if (menuShowHideAnimator.isVisible)
 209        {
 1210            HideMenu();
 1211        }
 212        else
 213        {
 1214            menuShowHideAnimator.Show();
 1215            CommonScriptableObjects.isProfileHUDOpen.Set(true);
 1216            OnOpen?.Invoke();
 217        }
 1218    }
 219
 220    internal void HideMenu()
 221    {
 1222        if (menuShowHideAnimator.isVisible)
 223        {
 1224            menuShowHideAnimator.Hide();
 1225            CommonScriptableObjects.isProfileHUDOpen.Set(false);
 1226            OnClose?.Invoke();
 227        }
 1228    }
 229
 230    internal void SetVisibility(bool visible)
 231    {
 3232        if (visible && !mainShowHideAnimator.isVisible)
 0233            mainShowHideAnimator.Show();
 3234        else if (!visible && mainShowHideAnimator.isVisible)
 1235            mainShowHideAnimator.Hide();
 3236    }
 237
 238    private void HandleProfileSnapshot(UserProfile userProfile)
 239    {
 5240        loadingSpinner.SetActive(true);
 5241        userProfile.snapshotObserver.AddListener(SetProfileImage);
 5242    }
 243
 244    private void HandleClaimedProfileName(UserProfile userProfile)
 245    {
 1246        textName.text = userProfile.userName;
 1247        SetActiveUnverifiedNameGOs(false);
 1248    }
 249
 250    private void HandleUnverifiedProfileName(UserProfile userProfile)
 251    {
 4252        if (!String.IsNullOrEmpty(userProfile.userName) &&
 253            userProfile.userName.Length > NAME_POSTFIX_LENGTH)
 254        {
 4255            textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1);
 4256        }
 257        else
 258        {
 0259            textName.text = userProfile.userName;
 260        }
 261
 4262        textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}";
 4263        SetActiveUnverifiedNameGOs(true);
 4264    }
 265
 266    private void SetConnectedWalletSectionActive(bool active)
 267    {
 5268        connectedWalletSection.SetActive(active);
 5269        nonConnectedWalletSection.SetActive(!active);
 5270    }
 271
 10272    private void ForceLayoutToRefreshSize() { LayoutRebuilder.ForceRebuildLayoutImmediate(mainRootLayout); }
 273
 274    private void SetActiveUnverifiedNameGOs(bool active)
 275    {
 40276        for (int i = 0; i < hideOnNameClaimed.Length; i++)
 277        {
 15278            hideOnNameClaimed[i].SetActive(active);
 279        }
 5280    }
 281
 282    private void HandleProfileAddress(UserProfile userProfile)
 283    {
 5284        string address = userProfile.userId;
 5285        string start = address.Substring(0, ADDRESS_CHUNK_LENGTH);
 5286        string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH);
 5287        textAddress.text = $"{start}...{end}";
 5288    }
 289
 290    private void SetProfileImage(Texture2D texture)
 291    {
 0292        loadingSpinner.SetActive(false);
 0293        imageAvatarThumbnail.texture = texture;
 0294    }
 295
 296    private void OnDestroy()
 297    {
 17298        if (profile)
 4299            profile.snapshotObserver.RemoveListener(SetProfileImage);
 17300    }
 301
 302    private void CopyAddress()
 303    {
 0304        if (!profile)
 305        {
 0306            return;
 307        }
 308
 0309        Environment.i.platform.clipboard.WriteText(profile.userId);
 310
 0311        copyTooltip.gameObject.SetActive(false);
 0312        if (copyToastRoutine != null)
 313        {
 0314            StopCoroutine(copyToastRoutine);
 315        }
 316
 0317        copyToastRoutine = StartCoroutine(ShowCopyToast());
 0318    }
 319
 320    private IEnumerator ShowCopyToast()
 321    {
 0322        if (!copyToast.gameObject.activeSelf)
 323        {
 0324            copyToast.gameObject.SetActive(true);
 325        }
 326
 0327        copyToast.Show();
 0328        yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME);
 0329        copyToast.Hide();
 0330    }
 331
 34332    private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; }
 333
 34334    private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; }
 335
 336    internal void ActivateProfileNameEditionMode(bool activate)
 337    {
 2338        if (profile != null && profile.hasClaimedName)
 0339            return;
 340
 2341        textName.gameObject.SetActive(!activate);
 2342        inputName.gameObject.SetActive(activate);
 343
 2344        if (activate)
 345        {
 1346            inputName.text = textName.text;
 1347            inputName.Select();
 348        }
 2349    }
 350
 6351    private void UpdateNameCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLim
 352
 2353    internal void SetProfileName(string newName) { textName.text = newName; }
 354
 0355    internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); }
 356
 357    internal bool IsValidAvatarName(string name)
 358    {
 0359        if (nameRegex == null)
 0360            return true;
 361
 0362        return nameRegex.IsMatch(name);
 363    }
 364
 365    internal void ActivateDescriptionEditionMode(bool active)
 366    {
 22367        charLimitDescriptionContainer.SetActive(active);
 22368        descriptionEditionInput.gameObject.SetActive(active);
 22369        descriptionPreviewInput.gameObject.SetActive(!active);
 370
 22371        if (active)
 372        {
 3373            descriptionEditionInput.text = descriptionPreviewInput.text;
 3374            StartCoroutine(SelectComponentOnNextFrame(descriptionEditionInput));
 375        }
 22376    }
 377
 378    private IEnumerator SelectComponentOnNextFrame(Selectable selectable)
 379    {
 3380        yield return null;
 0381        selectable.Select();
 0382    }
 383
 384    internal void SetDescription(string description)
 385    {
 7386        descriptionPreviewInput.text = description;
 7387        descriptionEditionInput.text = description;
 7388    }
 389
 18390    private void UpdateDescriptionCharLimit(string newValue) { textCharLimitDescription.text = $"{newValue.Length}/{desc
 391
 10392    private void SetDescriptionEnabled(bool enabled) { descriptionContainer.SetActive(enabled); }
 393
 394    public void SetCardAsFullScreenMenuMode(bool isActive)
 395    {
 0396        buttonToggleMenu.gameObject.SetActive(!isActive);
 0397        mainCanvas.sortingOrder = isActive ? 4 : 1;
 0398    }
 399}