< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController(...)0%16.1616091.43%
GetViewPrefab()0%110100%
SetVisibility(...)0%13.710066.67%
Dispose()0%5.075085.71%
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%110100%

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
 5130    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        DataStore.i.exploreV2.profileCardIsOpen.OnChange += SetAsFullScreenMenuMode;
 50
 1751        view.connectedWalletSection.SetActive(false);
 1752        view.nonConnectedWalletSection.SetActive(false);
 1753        view.ActivateDescriptionEditionMode(false);
 54
 1755        view.buttonLogOut.onClick.AddListener(WebInterface.LogOut);
 1756        view.buttonSignUp.onClick.AddListener(WebInterface.RedirectToSignUp);
 1757        view.buttonClaimName.onClick.AddListener(() => WebInterface.OpenURL(URL_CLAIM_NAME));
 1758        view.buttonTermsOfServiceForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1759        view.buttonPrivacyPolicyForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1760        view.buttonTermsOfServiceForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1761        view.buttonPrivacyPolicyForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1762        view.inputName.onSubmit.AddListener(UpdateProfileName);
 1763        view.descriptionEditionInput.onSubmit.AddListener(UpdateProfileDescription);
 1764        view.OnOpen += () =>
 65        {
 166            WebInterface.RequestOwnProfileUpdate();
 167            OnOpen?.Invoke();
 068        };
 1869        view.OnClose += () => OnClose?.Invoke();
 70
 1771        if (view.manaCounterView)
 72        {
 1773            view.manaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1774            view.manaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCHASE));
 75        }
 76
 1777        if (view.polygonManaCounterView)
 78        {
 1779            view.polygonManaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1780            view.polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCH
 81        }
 82
 1783        ownUserProfile.OnUpdate += OnProfileUpdated;
 1784        if (mouseCatcher != null)
 085            mouseCatcher.OnMouseLock += OnMouseLocked;
 86
 1787        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 88        {
 089            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 090            KernelConfig.i.OnChange += OnKernelConfigChanged;
 91        }
 92
 1793        DataStore.i.exploreV2.isInitialized.OnChange += ExploreV2Changed;
 1794        ExploreV2Changed(DataStore.i.exploreV2.isInitialized.Get(), false);
 1795    }
 96
 97    protected virtual GameObject GetViewPrefab()
 98    {
 1799        return Resources.Load<GameObject>("ProfileHUD");
 100    }
 101
 102    public void SetVisibility(bool visible)
 103    {
 3104        view?.SetVisibility(visible);
 105
 3106        if (visible && fetchManaIntervalRoutine == null)
 107        {
 2108            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 109        }
 1110        else if (!visible && fetchManaIntervalRoutine != null)
 111        {
 0112            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0113            fetchManaIntervalRoutine = null;
 114        }
 115
 3116        if (visible && fetchPolygonManaIntervalRoutine == null)
 117        {
 2118            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 119        }
 1120        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 121        {
 0122            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0123            fetchPolygonManaIntervalRoutine = null;
 124        }
 1125    }
 126
 127    public void Dispose()
 128    {
 17129        if (fetchManaIntervalRoutine != null)
 130        {
 2131            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 2132            fetchManaIntervalRoutine = null;
 133        }
 134
 17135        if (view)
 136        {
 17137            GameObject.Destroy(view.gameObject);
 138        }
 139
 17140        ownUserProfile.OnUpdate -= OnProfileUpdated;
 17141        if (mouseCatcher != null)
 0142            mouseCatcher.OnMouseLock -= OnMouseLocked;
 143
 17144        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 145        {
 0146            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 147        }
 148
 17149        view.descriptionPreviewInput.onSubmit.RemoveListener(UpdateProfileDescription);
 17150        DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode;
 151
 17152        DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed;
 17153    }
 154
 10155    void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); }
 156
 0157    void OnMouseLocked() { HideProfileMenu(); }
 158
 159    IEnumerator ManaIntervalRoutine()
 160    {
 0161        while (true)
 162        {
 2163            WebInterface.FetchBalanceOfMANA();
 2164            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 165        }
 166    }
 167
 168    IEnumerator PolygonManaIntervalRoutine()
 169    {
 0170        while (true)
 171        {
 9172            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 173
 1174            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 175
 176            // This can be null if theGraph is mocked
 1177            if ( promise != null )
 178            {
 0179                yield return promise;
 0180                SetPolygonManaBalance(promise.value);
 181            }
 182
 1183            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0184        }
 185    }
 186
 187    /// <summary>
 188    /// Set an amount of MANA on the HUD.
 189    /// </summary>
 190    /// <param name="balance">Amount of MANA.</param>
 2191    public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); }
 192
 0193    public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); }
 194
 195    /// <summary>
 196    /// Close the Profile menu.
 197    /// </summary>
 0198    public void HideProfileMenu() { view?.HideMenu(); }
 199
 200    private void UpdateProfileName(string newName)
 201    {
 0202        if (view.inputName.wasCanceled)
 0203            return;
 204
 0205        if (!view.IsValidAvatarName(newName))
 206        {
 0207            view.inputName.ActivateInputField();
 0208            return;
 209        }
 210
 0211        if (view != null)
 212        {
 0213            view.SetProfileName(newName);
 0214            view.ActivateProfileNameEditionMode(false);
 215        }
 216
 0217        userProfileBridge.SaveUnverifiedName(newName);
 0218    }
 219
 0220    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre
 221
 222    private void UpdateProfileDescription(string description)
 223    {
 2224        if (view.descriptionEditionInput.wasCanceled
 225            || !ownUserProfile.hasConnectedWeb3
 226            || description.Length > view.descriptionEditionInput.characterLimit)
 227        {
 1228            view.ActivateDescriptionEditionMode(false);
 1229            return;
 230        }
 231
 1232        view.SetDescription(description);
 1233        view.ActivateDescriptionEditionMode(false);
 1234        userProfileBridge.SaveDescription(description);
 1235    }
 236
 237    private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode)
 238    {
 0239        view.SetCardAsFullScreenMenuMode(currentIsFullScreenMenuMode);
 240
 0241        if (currentIsFullScreenMenuMode != CommonScriptableObjects.isProfileHUDOpen.Get())
 0242            view.ToggleMenu();
 0243    }
 244
 34245    private void ExploreV2Changed(bool current, bool previous) { view.SetStartMenuButtonActive(current); }
 246}