< Summary

Class:ProfileHUDView
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs
Covered lines:92
Uncovered lines:42
Coverable lines:134
Total lines:400
Line coverage:68.6% (92 of 134)
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    protected 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    protected internal Button buttonClaimName;
 72
 73    [SerializeField]
 74    protected internal Button buttonCopyAddress;
 75
 76    [SerializeField]
 77    protected internal Button buttonLogOut;
 78
 79    [SerializeField]
 80    protected internal Button buttonSignUp;
 81
 82    [SerializeField]
 83    protected internal Button_OnPointerDown buttonTermsOfServiceForConnectedWallets;
 84
 85    [SerializeField]
 86    protected internal Button_OnPointerDown buttonPrivacyPolicyForConnectedWallets;
 87
 88    [SerializeField]
 89    protected internal Button_OnPointerDown buttonTermsOfServiceForNonConnectedWallets;
 90
 91    [SerializeField]
 92    protected internal Button_OnPointerDown buttonPrivacyPolicyForNonConnectedWallets;
 93
 94    [Header("Name Edition")]
 95    [SerializeField]
 96    protected internal Button_OnPointerDown buttonEditName;
 97
 98    [SerializeField]
 99    protected 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        buttonLogOut.gameObject.SetActive(active);
 5271    }
 272
 10273    private void ForceLayoutToRefreshSize() { LayoutRebuilder.ForceRebuildLayoutImmediate(mainRootLayout); }
 274
 275    private void SetActiveUnverifiedNameGOs(bool active)
 276    {
 40277        for (int i = 0; i < hideOnNameClaimed.Length; i++)
 278        {
 15279            hideOnNameClaimed[i].SetActive(active);
 280        }
 5281    }
 282
 283    private void HandleProfileAddress(UserProfile userProfile)
 284    {
 5285        string address = userProfile.userId;
 5286        string start = address.Substring(0, ADDRESS_CHUNK_LENGTH);
 5287        string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH);
 5288        textAddress.text = $"{start}...{end}";
 5289    }
 290
 291    private void SetProfileImage(Texture2D texture)
 292    {
 0293        loadingSpinner.SetActive(false);
 0294        imageAvatarThumbnail.texture = texture;
 0295    }
 296
 297    private void OnDestroy()
 298    {
 17299        if (profile)
 4300            profile.snapshotObserver.RemoveListener(SetProfileImage);
 17301    }
 302
 303    private void CopyAddress()
 304    {
 0305        if (!profile)
 306        {
 0307            return;
 308        }
 309
 0310        Environment.i.platform.clipboard.WriteText(profile.userId);
 311
 0312        copyTooltip.gameObject.SetActive(false);
 0313        if (copyToastRoutine != null)
 314        {
 0315            StopCoroutine(copyToastRoutine);
 316        }
 317
 0318        copyToastRoutine = StartCoroutine(ShowCopyToast());
 0319    }
 320
 321    private IEnumerator ShowCopyToast()
 322    {
 0323        if (!copyToast.gameObject.activeSelf)
 324        {
 0325            copyToast.gameObject.SetActive(true);
 326        }
 327
 0328        copyToast.Show();
 0329        yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME);
 0330        copyToast.Hide();
 0331    }
 332
 34333    private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; }
 334
 34335    private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; }
 336
 337    internal void ActivateProfileNameEditionMode(bool activate)
 338    {
 2339        if (profile != null && profile.hasClaimedName)
 0340            return;
 341
 2342        textName.gameObject.SetActive(!activate);
 2343        inputName.gameObject.SetActive(activate);
 344
 2345        if (activate)
 346        {
 1347            inputName.text = textName.text;
 1348            inputName.Select();
 349        }
 2350    }
 351
 6352    private void UpdateNameCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLim
 353
 2354    internal void SetProfileName(string newName) { textName.text = newName; }
 355
 0356    internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); }
 357
 358    internal bool IsValidAvatarName(string name)
 359    {
 0360        if (nameRegex == null)
 0361            return true;
 362
 0363        return nameRegex.IsMatch(name);
 364    }
 365
 366    internal void ActivateDescriptionEditionMode(bool active)
 367    {
 22368        charLimitDescriptionContainer.SetActive(active);
 22369        descriptionEditionInput.gameObject.SetActive(active);
 22370        descriptionPreviewInput.gameObject.SetActive(!active);
 371
 22372        if (active)
 373        {
 3374            descriptionEditionInput.text = descriptionPreviewInput.text;
 3375            StartCoroutine(SelectComponentOnNextFrame(descriptionEditionInput));
 376        }
 22377    }
 378
 379    private IEnumerator SelectComponentOnNextFrame(Selectable selectable)
 380    {
 3381        yield return null;
 0382        selectable.Select();
 0383    }
 384
 385    internal void SetDescription(string description)
 386    {
 7387        descriptionPreviewInput.text = description;
 7388        descriptionEditionInput.text = description;
 7389    }
 390
 18391    private void UpdateDescriptionCharLimit(string newValue) { textCharLimitDescription.text = $"{newValue.Length}/{desc
 392
 10393    private void SetDescriptionEnabled(bool enabled) { descriptionContainer.SetActive(enabled); }
 394
 395    public void SetCardAsFullScreenMenuMode(bool isActive)
 396    {
 0397        buttonToggleMenu.gameObject.SetActive(!isActive);
 0398        mainCanvas.sortingOrder = isActive ? 4 : 1;
 0399    }
 400}