< Summary

Class:ProfileHUDController
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDController.cs
Covered lines:77
Uncovered lines:34
Coverable lines:111
Total lines:250
Line coverage:69.3% (77 of 111)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController(...)0%16.0416094.44%
GetViewPrefab()0%110100%
ChangeVisibilityForBuilderInWorld(...)0%2100%
SetVisibility(...)0%12.3310071.43%
Dispose()0%5.015093.33%
OnProfileUpdated(...)0%220100%
OnMouseLocked()0%2100%
ManaIntervalRoutine()0%3.333066.67%
PolygonManaIntervalRoutine()0%10.56050%
SetManaBalance(...)0%220100%
SetPolygonManaBalance(...)0%2100%
HideProfileMenu()0%6200%
UpdateProfileName(...)0%20400%
OnKernelConfigChanged(...)0%6200%
UpdateProfileDescription(...)0%440100%
SetAsFullScreenMenuMode(...)0%6200%
ExploreV2Changed(...)0%2100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using DCL.Interface;
 4using System;
 5using System.Collections;
 6using UnityEngine;
 7using Environment = DCL.Environment;
 8using WaitUntil = UnityEngine.WaitUntil;
 9
 10public class ProfileHUDController : IHUD
 11{
 12    private readonly IUserProfileBridge userProfileBridge;
 13
 14    [Serializable]
 15    public struct Configuration
 16    {
 17        public bool connectedWallet;
 18    }
 19
 20    private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name";
 21    private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet";
 22    private const string URL_MANA_PURCHASE = "https://account.decentraland.org";
 23    private const string URL_TERMS_OF_USE = "https://decentraland.org/terms";
 24    private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy";
 25    private const float FETCH_MANA_INTERVAL = 60;
 26
 27    public readonly ProfileHUDView view;
 28    internal AvatarEditorHUDController avatarEditorHud;
 29
 4230    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 31    private IMouseCatcher mouseCatcher;
 32    private Coroutine fetchManaIntervalRoutine = null;
 33    private Coroutine fetchPolygonManaIntervalRoutine = null;
 34
 035    public RectTransform tutorialTooltipReference { get => view.tutorialTooltipReference; }
 36
 37    public event Action OnOpen;
 38    public event Action OnClose;
 39
 1740    public ProfileHUDController(IUserProfileBridge userProfileBridge)
 41    {
 1742        this.userProfileBridge = userProfileBridge;
 1743        mouseCatcher = SceneReferences.i?.mouseCatcher;
 44
 45
 1746        view = UnityEngine.Object.Instantiate(GetViewPrefab()).GetComponent<ProfileHUDView>();
 1747        view.name = "_ProfileHUD";
 48
 1749        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange += ChangeVisibilityForBuilderInWor
 1750        DataStore.i.exploreV2.profileCardIsOpen.OnChange += SetAsFullScreenMenuMode;
 51
 1752        view.connectedWalletSection.SetActive(false);
 1753        view.nonConnectedWalletSection.SetActive(false);
 1754        view.ActivateDescriptionEditionMode(false);
 55
 1756        view.buttonLogOut.onClick.AddListener(WebInterface.LogOut);
 1757        view.buttonSignUp.onClick.AddListener(WebInterface.RedirectToSignUp);
 1758        view.buttonClaimName.onClick.AddListener(() => WebInterface.OpenURL(URL_CLAIM_NAME));
 1759        view.buttonTermsOfServiceForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1760        view.buttonPrivacyPolicyForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1761        view.buttonTermsOfServiceForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1762        view.buttonPrivacyPolicyForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1763        view.inputName.onSubmit.AddListener(UpdateProfileName);
 1764        view.descriptionEditionInput.onSubmit.AddListener(UpdateProfileDescription);
 1765        view.OnOpen += () =>
 66        {
 167            WebInterface.RequestOwnProfileUpdate();
 168            OnOpen?.Invoke();
 069        };
 1870        view.OnClose += () => OnClose?.Invoke();
 71
 1772        if (view.manaCounterView)
 73        {
 1774            view.manaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1775            view.manaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCHASE));
 76        }
 77
 1778        if (view.polygonManaCounterView)
 79        {
 1780            view.polygonManaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1781            view.polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCH
 82        }
 83
 1784        ownUserProfile.OnUpdate += OnProfileUpdated;
 1785        if (mouseCatcher != null)
 1786            mouseCatcher.OnMouseLock += OnMouseLocked;
 87
 1788        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 89        {
 090            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 091            KernelConfig.i.OnChange += OnKernelConfigChanged;
 92        }
 93
 1794        DataStore.i.exploreV2.isInitialized.OnChange += ExploreV2Changed;
 1795        ExploreV2Changed(DataStore.i.exploreV2.isInitialized.Get(), false);
 1796    }
 97
 98    protected virtual GameObject GetViewPrefab()
 99    {
 17100        return Resources.Load<GameObject>("ProfileHUD");
 101    }
 102
 0103    public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); }
 104
 105    public void SetVisibility(bool visible)
 106    {
 3107        view?.SetVisibility(visible);
 108
 3109        if (visible && fetchManaIntervalRoutine == null)
 110        {
 2111            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 2112        }
 1113        else if (!visible && fetchManaIntervalRoutine != null)
 114        {
 0115            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0116            fetchManaIntervalRoutine = null;
 117        }
 118
 3119        if (visible && fetchPolygonManaIntervalRoutine == null)
 120        {
 2121            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 2122        }
 1123        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 124        {
 0125            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0126            fetchPolygonManaIntervalRoutine = null;
 127        }
 1128    }
 129
 130    public void Dispose()
 131    {
 17132        if (fetchManaIntervalRoutine != null)
 133        {
 2134            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 2135            fetchManaIntervalRoutine = null;
 136        }
 137
 17138        if (view)
 139        {
 17140            GameObject.Destroy(view.gameObject);
 141        }
 142
 17143        ownUserProfile.OnUpdate -= OnProfileUpdated;
 17144        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor
 17145        if (mouseCatcher != null)
 17146            mouseCatcher.OnMouseLock -= OnMouseLocked;
 147
 17148        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 149        {
 0150            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 151        }
 152
 17153        view.descriptionPreviewInput.onSubmit.RemoveListener(UpdateProfileDescription);
 17154        DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode;
 155
 17156        DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed;
 17157    }
 158
 10159    void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); }
 160
 0161    void OnMouseLocked() { HideProfileMenu(); }
 162
 163    IEnumerator ManaIntervalRoutine()
 164    {
 0165        while (true)
 166        {
 2167            WebInterface.FetchBalanceOfMANA();
 2168            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 169        }
 170    }
 171
 172    IEnumerator PolygonManaIntervalRoutine()
 173    {
 0174        while (true)
 175        {
 4176            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 177
 2178            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 179
 180            // This can be null if theGraph is mocked
 2181            if ( promise != null )
 182            {
 0183                yield return promise;
 0184                SetPolygonManaBalance(promise.value);
 185            }
 186
 2187            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0188        }
 189    }
 190
 191    /// <summary>
 192    /// Set an amount of MANA on the HUD.
 193    /// </summary>
 194    /// <param name="balance">Amount of MANA.</param>
 2195    public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); }
 196
 0197    public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); }
 198
 199    /// <summary>
 200    /// Close the Profile menu.
 201    /// </summary>
 0202    public void HideProfileMenu() { view?.HideMenu(); }
 203
 204    private void UpdateProfileName(string newName)
 205    {
 0206        if (view.inputName.wasCanceled)
 0207            return;
 208
 0209        if (!view.IsValidAvatarName(newName))
 210        {
 0211            view.inputName.ActivateInputField();
 0212            return;
 213        }
 214
 0215        if (view != null)
 216        {
 0217            view.SetProfileName(newName);
 0218            view.ActivateProfileNameEditionMode(false);
 219        }
 220
 0221        userProfileBridge.SaveUnverifiedName(newName);
 0222    }
 223
 0224    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre
 225
 226    private void UpdateProfileDescription(string description)
 227    {
 2228        if (view.descriptionEditionInput.wasCanceled
 229            || !ownUserProfile.hasConnectedWeb3
 230            || description.Length > view.descriptionEditionInput.characterLimit)
 231        {
 1232            view.ActivateDescriptionEditionMode(false);
 1233            return;
 234        }
 235
 1236        view.SetDescription(description);
 1237        view.ActivateDescriptionEditionMode(false);
 1238        userProfileBridge.SaveDescription(description);
 1239    }
 240
 241    private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode)
 242    {
 0243        view.SetCardAsFullScreenMenuMode(currentIsFullScreenMenuMode);
 244
 0245        if (currentIsFullScreenMenuMode != CommonScriptableObjects.isProfileHUDOpen.Get())
 0246            view.ToggleMenu();
 0247    }
 248
 0249    private void ExploreV2Changed(bool current, bool previous) { view.SetStartMenuButtonActive(current); }
 250}