< Summary

Class:ProfileHUDController
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDController.cs
Covered lines:79
Uncovered lines:31
Coverable lines:110
Total lines:242
Line coverage:71.8% (79 of 110)
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%5.585071.43%
SetManaBalance(...)0%220100%
SetPolygonManaBalance(...)0%110100%
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    protected virtual GameObject GetViewPrefab()
 98    {
 1799        return Resources.Load<GameObject>("ProfileHUD");
 100    }
 101
 0102    public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); }
 103
 104    public void SetVisibility(bool visible)
 105    {
 3106        view?.SetVisibility(visible);
 107
 3108        if (visible && fetchManaIntervalRoutine == null)
 109        {
 2110            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 2111        }
 1112        else if (!visible && fetchManaIntervalRoutine != null)
 113        {
 0114            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0115            fetchManaIntervalRoutine = null;
 116        }
 117
 3118        if (visible && fetchPolygonManaIntervalRoutine == null)
 119        {
 2120            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 2121        }
 1122        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 123        {
 0124            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0125            fetchPolygonManaIntervalRoutine = null;
 126        }
 1127    }
 128
 129    public void Dispose()
 130    {
 17131        if (fetchManaIntervalRoutine != null)
 132        {
 2133            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 2134            fetchManaIntervalRoutine = null;
 135        }
 136
 17137        if (view)
 138        {
 17139            GameObject.Destroy(view.gameObject);
 140        }
 17141        ownUserProfile.OnUpdate -= OnProfileUpdated;
 17142        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor
 17143        if (mouseCatcher != null)
 17144            mouseCatcher.OnMouseLock -= OnMouseLocked;
 145
 17146        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 147        {
 0148            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 149        }
 150
 17151        view.descriptionPreviewInput.onSubmit.RemoveListener(UpdateProfileDescription);
 17152        DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode;
 153
 17154        DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed;
 17155    }
 156
 10157    void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); }
 158
 0159    void OnMouseLocked() { HideProfileMenu(); }
 160
 161    IEnumerator ManaIntervalRoutine()
 162    {
 0163        while (true)
 164        {
 2165            WebInterface.FetchBalanceOfMANA();
 2166            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 167        }
 168    }
 169
 170    IEnumerator PolygonManaIntervalRoutine()
 171    {
 0172        while (true)
 173        {
 4174            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 2175            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 2176            yield return promise;
 1177            SetPolygonManaBalance(promise.value);
 178
 1179            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0180        }
 181    }
 182
 183    /// <summary>
 184    /// Set an amount of MANA on the HUD.
 185    /// </summary>
 186    /// <param name="balance">Amount of MANA.</param>
 2187    public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); }
 188
 2189    public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); }
 190
 191    /// <summary>
 192    /// Close the Profile menu.
 193    /// </summary>
 0194    public void HideProfileMenu() { view?.HideMenu(); }
 195
 196    private void UpdateProfileName(string newName)
 197    {
 0198        if (view.inputName.wasCanceled)
 0199            return;
 200
 0201        if (!view.IsValidAvatarName(newName))
 202        {
 0203            view.inputName.ActivateInputField();
 0204            return;
 205        }
 206
 0207        if (view != null)
 208        {
 0209            view.SetProfileName(newName);
 0210            view.ActivateProfileNameEditionMode(false);
 211        }
 212
 0213        userProfileBridge.SaveUnverifiedName(newName);
 0214    }
 215
 0216    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre
 217
 218    private void UpdateProfileDescription(string description)
 219    {
 2220        if (view.descriptionEditionInput.wasCanceled
 221            || !ownUserProfile.hasConnectedWeb3
 222            || description.Length > view.descriptionEditionInput.characterLimit)
 223        {
 1224            view.ActivateDescriptionEditionMode(false);
 1225            return;
 226        }
 227
 1228        view.SetDescription(description);
 1229        view.ActivateDescriptionEditionMode(false);
 1230        userProfileBridge.SaveDescription(description);
 1231    }
 232
 233    private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode)
 234    {
 0235        view.SetCardAsFullScreenMenuMode(currentIsFullScreenMenuMode);
 236
 0237        if (currentIsFullScreenMenuMode != CommonScriptableObjects.isProfileHUDOpen.Get())
 0238            view.ToggleMenu();
 0239    }
 240
 0241    private void ExploreV2Changed(bool current, bool previous) { view.SetStartMenuButtonActive(current); }
 242}