< Summary

Class:ProfileHUDView
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDView.cs
Covered lines:94
Uncovered lines:42
Coverable lines:136
Total lines:403
Line coverage:69.1% (94 of 136)
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%330100%
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    private HUDCanvasCameraModeController hudCanvasCameraModeController;
 145
 146    private void Awake()
 147    {
 17148        closeActionDelegate = (x) => HideMenu();
 149
 17150        buttonToggleMenu.onClick.AddListener(OpenStartMenu);
 17151        buttonCopyAddress.onClick.AddListener(CopyAddress);
 17152        buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true);
 17153        buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true);
 17154        inputName.onValueChanged.AddListener(UpdateNameCharLimit);
 17155        inputName.onDeselect.AddListener((x) => ActivateProfileNameEditionMode(false));
 17156        descriptionPreviewInput.onSelect.AddListener(x =>
 157        {
 0158            ActivateDescriptionEditionMode(true);
 0159            UpdateDescriptionCharLimit(descriptionPreviewInput.text);
 0160        });
 17161        descriptionEditionInput.onValueChanged.AddListener(UpdateDescriptionCharLimit);
 17162        descriptionEditionInput.onDeselect.AddListener(x => ActivateDescriptionEditionMode(false));
 17163        copyToast.gameObject.SetActive(false);
 17164        hudCanvasCameraModeController = new HUDCanvasCameraModeController(GetComponent<Canvas>(), DataStore.i.camera.hud
 17165    }
 166
 167    internal void SetProfile(UserProfile userProfile)
 168    {
 5169        profile = userProfile;
 5170        if (userProfile.hasClaimedName)
 171        {
 1172            HandleClaimedProfileName(userProfile);
 1173        }
 174        else
 175        {
 4176            HandleUnverifiedProfileName(userProfile);
 177        }
 178
 5179        SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3);
 5180        HandleProfileAddress(userProfile);
 5181        HandleProfileSnapshot(userProfile);
 5182        SetDescription(userProfile.description);
 5183        SetDescriptionEnabled(userProfile.hasConnectedWeb3);
 5184        ForceLayoutToRefreshSize();
 5185    }
 186
 187    internal void OpenStartMenu()
 188    {
 0189        if (isStartMenuInitialized)
 190        {
 0191            if (!DataStore.i.exploreV2.isOpen.Get())
 192            {
 0193                var exploreV2Analytics = new ExploreV2Analytics.ExploreV2Analytics();
 0194                exploreV2Analytics.SendStartMenuVisibility(
 195                    true,
 196                    ExploreUIVisibilityMethod.FromClick);
 197            }
 0198            DataStore.i.exploreV2.isOpen.Set(true);
 0199        }
 200        else
 201        {
 0202            ToggleMenu();
 203        }
 0204    }
 205
 0206    public void SetStartMenuButtonActive(bool isActive) { isStartMenuInitialized = isActive; }
 207
 208    internal void ToggleMenu()
 209    {
 2210        if (menuShowHideAnimator.isVisible)
 211        {
 1212            HideMenu();
 1213        }
 214        else
 215        {
 1216            menuShowHideAnimator.Show();
 1217            CommonScriptableObjects.isProfileHUDOpen.Set(true);
 1218            OnOpen?.Invoke();
 219        }
 1220    }
 221
 222    internal void HideMenu()
 223    {
 1224        if (menuShowHideAnimator.isVisible)
 225        {
 1226            menuShowHideAnimator.Hide();
 1227            CommonScriptableObjects.isProfileHUDOpen.Set(false);
 1228            OnClose?.Invoke();
 229        }
 1230    }
 231
 232    internal void SetVisibility(bool visible)
 233    {
 3234        if (visible && !mainShowHideAnimator.isVisible)
 0235            mainShowHideAnimator.Show();
 3236        else if (!visible && mainShowHideAnimator.isVisible)
 1237            mainShowHideAnimator.Hide();
 3238    }
 239
 240    private void HandleProfileSnapshot(UserProfile userProfile)
 241    {
 5242        loadingSpinner.SetActive(true);
 5243        userProfile.snapshotObserver.AddListener(SetProfileImage);
 5244    }
 245
 246    private void HandleClaimedProfileName(UserProfile userProfile)
 247    {
 1248        textName.text = userProfile.userName;
 1249        SetActiveUnverifiedNameGOs(false);
 1250    }
 251
 252    private void HandleUnverifiedProfileName(UserProfile userProfile)
 253    {
 4254        if (!String.IsNullOrEmpty(userProfile.userName) &&
 255            userProfile.userName.Length > NAME_POSTFIX_LENGTH)
 256        {
 4257            textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1);
 4258        }
 259        else
 260        {
 0261            textName.text = userProfile.userName;
 262        }
 263
 4264        textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}";
 4265        SetActiveUnverifiedNameGOs(true);
 4266    }
 267
 268    private void SetConnectedWalletSectionActive(bool active)
 269    {
 5270        connectedWalletSection.SetActive(active);
 5271        nonConnectedWalletSection.SetActive(!active);
 5272        buttonLogOut.gameObject.SetActive(active);
 5273    }
 274
 10275    private void ForceLayoutToRefreshSize() { LayoutRebuilder.ForceRebuildLayoutImmediate(mainRootLayout); }
 276
 277    private void SetActiveUnverifiedNameGOs(bool active)
 278    {
 40279        for (int i = 0; i < hideOnNameClaimed.Length; i++)
 280        {
 15281            hideOnNameClaimed[i].SetActive(active);
 282        }
 5283    }
 284
 285    private void HandleProfileAddress(UserProfile userProfile)
 286    {
 5287        string address = userProfile.userId;
 5288        string start = address.Substring(0, ADDRESS_CHUNK_LENGTH);
 5289        string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH);
 5290        textAddress.text = $"{start}...{end}";
 5291    }
 292
 293    private void SetProfileImage(Texture2D texture)
 294    {
 0295        loadingSpinner.SetActive(false);
 0296        imageAvatarThumbnail.texture = texture;
 0297    }
 298
 299    private void OnDestroy()
 300    {
 17301        hudCanvasCameraModeController?.Dispose();
 17302        if (profile)
 4303            profile.snapshotObserver.RemoveListener(SetProfileImage);
 17304    }
 305
 306    private void CopyAddress()
 307    {
 0308        if (!profile)
 309        {
 0310            return;
 311        }
 312
 0313        Environment.i.platform.clipboard.WriteText(profile.userId);
 314
 0315        copyTooltip.gameObject.SetActive(false);
 0316        if (copyToastRoutine != null)
 317        {
 0318            StopCoroutine(copyToastRoutine);
 319        }
 320
 0321        copyToastRoutine = StartCoroutine(ShowCopyToast());
 0322    }
 323
 324    private IEnumerator ShowCopyToast()
 325    {
 0326        if (!copyToast.gameObject.activeSelf)
 327        {
 0328            copyToast.gameObject.SetActive(true);
 329        }
 330
 0331        copyToast.Show();
 0332        yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME);
 0333        copyToast.Hide();
 0334    }
 335
 34336    private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; }
 337
 34338    private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; }
 339
 340    internal void ActivateProfileNameEditionMode(bool activate)
 341    {
 2342        if (profile != null && profile.hasClaimedName)
 0343            return;
 344
 2345        textName.gameObject.SetActive(!activate);
 2346        inputName.gameObject.SetActive(activate);
 347
 2348        if (activate)
 349        {
 1350            inputName.text = textName.text;
 1351            inputName.Select();
 352        }
 2353    }
 354
 6355    private void UpdateNameCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLim
 356
 2357    internal void SetProfileName(string newName) { textName.text = newName; }
 358
 0359    internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); }
 360
 361    internal bool IsValidAvatarName(string name)
 362    {
 0363        if (nameRegex == null)
 0364            return true;
 365
 0366        return nameRegex.IsMatch(name);
 367    }
 368
 369    internal void ActivateDescriptionEditionMode(bool active)
 370    {
 22371        charLimitDescriptionContainer.SetActive(active);
 22372        descriptionEditionInput.gameObject.SetActive(active);
 22373        descriptionPreviewInput.gameObject.SetActive(!active);
 374
 22375        if (active)
 376        {
 3377            descriptionEditionInput.text = descriptionPreviewInput.text;
 3378            StartCoroutine(SelectComponentOnNextFrame(descriptionEditionInput));
 379        }
 22380    }
 381
 382    private IEnumerator SelectComponentOnNextFrame(Selectable selectable)
 383    {
 3384        yield return null;
 0385        selectable.Select();
 0386    }
 387
 388    internal void SetDescription(string description)
 389    {
 7390        descriptionPreviewInput.text = description;
 7391        descriptionEditionInput.text = description;
 7392    }
 393
 18394    private void UpdateDescriptionCharLimit(string newValue) { textCharLimitDescription.text = $"{newValue.Length}/{desc
 395
 10396    private void SetDescriptionEnabled(bool enabled) { descriptionContainer.SetActive(enabled); }
 397
 398    public void SetCardAsFullScreenMenuMode(bool isActive)
 399    {
 0400        buttonToggleMenu.gameObject.SetActive(!isActive);
 0401        mainCanvas.sortingOrder = isActive ? 4 : 1;
 0402    }
 403}