< Summary

Class:ProfileHUDController
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDController.cs
Covered lines:71
Uncovered lines:31
Coverable lines:102
Total lines:232
Line coverage:69.6% (71 of 102)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController()0%16.0616093.75%
ChangeVisibilityForBuilderInWorld(...)0%2100%
SetVisibility(...)0%12.3310071.43%
Dispose()0%5.015091.67%
OnProfileUpdated(...)0%220100%
OnMouseLocked()0%2100%
ManaIntervalRoutine()0%3.333066.67%
PolygonManaIntervalRoutine()0%5.585071.43%
SetManaBalance(...)0%220100%
SetPolygonManaBalance(...)0%110100%
AddBackpackWindow(...)0%220100%
SetBackpackButtonVisibility(...)0%220100%
OpenBackpackWindow()0%6200%
HideProfileMenu()0%6200%
UpdateProfileName(...)0%20400%
OnKernelConfigChanged(...)0%6200%

File(s)

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

#LineLine coverage
 1using DCL;
 2using UnityEngine;
 3using DCL.Interface;
 4using System.Collections;
 5using System;
 6using DCL.Helpers;
 7using Environment = DCL.Environment;
 8using WaitUntil = UnityEngine.WaitUntil;
 9
 10public class ProfileHUDController : IHUD
 11{
 12    [Serializable]
 13    public struct Configuration
 14    {
 15        public bool connectedWallet;
 16    }
 17
 18    private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name";
 19    private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet";
 20    private const string URL_MANA_PURCHASE = "https://account.decentraland.org";
 21    private const string URL_TERMS_OF_USE = "https://decentraland.org/terms";
 22    private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy";
 23    private const float FETCH_MANA_INTERVAL = 60;
 24
 25    internal ProfileHUDView view;
 26    internal AvatarEditorHUDController avatarEditorHud;
 27
 3228    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 29    private IMouseCatcher mouseCatcher;
 30    private Coroutine fetchManaIntervalRoutine = null;
 31    private Coroutine fetchPolygonManaIntervalRoutine = null;
 32
 033    public RectTransform backpackTooltipReference { get => view.backpackTooltipReference; }
 34
 35    public event Action OnOpen;
 36    public event Action OnClose;
 37
 1338    public ProfileHUDController()
 39    {
 1340        mouseCatcher = InitialSceneReferences.i?.mouseCatcher;
 41
 42
 1343        view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("ProfileHUD")).GetComponent<ProfileHUDView>();
 1344        view.name = "_ProfileHUD";
 45
 1346        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange += ChangeVisibilityForBuilderInWor
 47
 1348        SetBackpackButtonVisibility(false);
 1349        view.connectedWalletSection.SetActive(false);
 1350        view.nonConnectedWalletSection.SetActive(false);
 51
 1352        view.buttonBackpack.onClick.AddListener(OpenBackpackWindow);
 1353        view.buttonLogOut.onClick.AddListener(WebInterface.LogOut);
 1354        view.buttonSignUp.onClick.AddListener(WebInterface.RedirectToSignUp);
 1355        view.buttonClaimName.onClick.AddListener(() => WebInterface.OpenURL(URL_CLAIM_NAME));
 1356        view.buttonTermsOfServiceForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1357        view.buttonPrivacyPolicyForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1358        view.buttonTermsOfServiceForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1359        view.buttonPrivacyPolicyForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1360        view.inputName.onSubmit.AddListener(UpdateProfileName);
 1361        view.OnOpen += () =>
 62        {
 163            WebInterface.RequestOwnProfileUpdate();
 164            OnOpen?.Invoke();
 065        };
 1466        view.OnClose += () => OnClose?.Invoke();
 67
 1368        if (view.manaCounterView)
 69        {
 1370            view.manaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1371            view.manaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCHASE));
 72        }
 73
 1374        if (view.polygonManaCounterView)
 75        {
 1376            view.polygonManaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1377            view.polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCH
 78        }
 79
 1380        ownUserProfile.OnUpdate += OnProfileUpdated;
 1381        if (mouseCatcher != null)
 1382            mouseCatcher.OnMouseLock += OnMouseLocked;
 83
 1384        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 85        {
 086            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 087            KernelConfig.i.OnChange += OnKernelConfigChanged;
 88        }
 1389    }
 90
 091    public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); }
 92
 93    public void SetVisibility(bool visible)
 94    {
 395        view?.SetVisibility(visible);
 96
 397        if (visible && fetchManaIntervalRoutine == null)
 98        {
 299            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 2100        }
 1101        else if (!visible && fetchManaIntervalRoutine != null)
 102        {
 0103            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0104            fetchManaIntervalRoutine = null;
 105        }
 106
 3107        if (visible && fetchPolygonManaIntervalRoutine == null)
 108        {
 2109            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 2110        }
 1111        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 112        {
 0113            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0114            fetchPolygonManaIntervalRoutine = null;
 115        }
 1116    }
 117
 118    public void Dispose()
 119    {
 13120        if (fetchManaIntervalRoutine != null)
 121        {
 2122            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 2123            fetchManaIntervalRoutine = null;
 124        }
 125
 13126        if (view)
 127        {
 13128            GameObject.Destroy(view.gameObject);
 129        }
 13130        ownUserProfile.OnUpdate -= OnProfileUpdated;
 13131        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor
 13132        if (mouseCatcher != null)
 13133            mouseCatcher.OnMouseLock -= OnMouseLocked;
 134
 13135        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 136        {
 0137            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 138        }
 13139    }
 140
 4141    void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); }
 142
 0143    void OnMouseLocked() { HideProfileMenu(); }
 144
 145    IEnumerator ManaIntervalRoutine()
 146    {
 0147        while (true)
 148        {
 2149            WebInterface.FetchBalanceOfMANA();
 2150            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 151        }
 152    }
 153
 154    IEnumerator PolygonManaIntervalRoutine()
 155    {
 0156        while (true)
 157        {
 4158            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 2159            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 2160            yield return promise;
 2161            SetPolygonManaBalance(promise.value);
 162
 2163            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0164        }
 165    }
 166
 167    /// <summary>
 168    /// Set an amount of MANA on the HUD.
 169    /// </summary>
 170    /// <param name="balance">Amount of MANA.</param>
 2171    public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); }
 172
 4173    public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); }
 174
 175    /// <summary>
 176    /// Configure an AvatarEditorHUDController for the Backpack button.
 177    /// </summary>
 178    /// <param name="controller">The avatar editor controller to asign.</param>
 179    public void AddBackpackWindow(AvatarEditorHUDController controller)
 180    {
 3181        if (controller == null)
 182        {
 1183            Debug.LogWarning("AddBackpackWindow >>> Backpack window doesn't exist yet!");
 1184            return;
 185        }
 186
 2187        avatarEditorHud = controller;
 2188        SetBackpackButtonVisibility(true);
 2189    }
 190
 191    /// <summary>
 192    /// Show/Hide the Backpack button.
 193    /// </summary>
 194    /// <param name="visible">True for showing the button.</param>
 34195    public void SetBackpackButtonVisibility(bool visible) { view?.SetBackpackButtonVisibility(avatarEditorHud != null &&
 196
 197    private void OpenBackpackWindow()
 198    {
 0199        if (avatarEditorHud == null)
 0200            return;
 201
 0202        avatarEditorHud.SetVisibility(true);
 0203        HideProfileMenu();
 0204    }
 205
 206    /// <summary>
 207    /// Close the Profile menu.
 208    /// </summary>
 0209    public void HideProfileMenu() { view?.HideMenu(); }
 210
 211    private void UpdateProfileName(string newName)
 212    {
 0213        if (view.inputName.wasCanceled)
 0214            return;
 215
 0216        if (!view.IsValidAvatarName(newName))
 217        {
 0218            view.inputName.ActivateInputField();
 0219            return;
 220        }
 221
 0222        if (view != null)
 223        {
 0224            view.SetProfileName(newName);
 0225            view.ActivateProfileNameEditionMode(false);
 226        }
 227
 0228        WebInterface.SendSaveUserUnverifiedName(newName);
 0229    }
 230
 0231    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre
 232}