< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController(...)0%16.0516094.29%
ChangeVisibilityForBuilderInWorld(...)0%2100%
SetVisibility(...)0%12.3310071.43%
Dispose()0%5.015092.31%
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%
UpdateProfileDescription(...)0%440100%

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 UnityEngine.Events;
 8using Environment = DCL.Environment;
 9using WaitUntil = UnityEngine.WaitUntil;
 10
 11public class ProfileHUDController : IHUD
 12{
 13    private readonly IUserProfileBridge userProfileBridge;
 14
 15    [Serializable]
 16    public struct Configuration
 17    {
 18        public bool connectedWallet;
 19    }
 20
 21    private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name";
 22    private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet";
 23    private const string URL_MANA_PURCHASE = "https://account.decentraland.org";
 24    private const string URL_TERMS_OF_USE = "https://decentraland.org/terms";
 25    private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy";
 26    private const float FETCH_MANA_INTERVAL = 60;
 27
 28    internal ProfileHUDView view;
 29    internal AvatarEditorHUDController avatarEditorHud;
 30
 4631    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 32    private IMouseCatcher mouseCatcher;
 33    private Coroutine fetchManaIntervalRoutine = null;
 34    private Coroutine fetchPolygonManaIntervalRoutine = null;
 35
 036    public RectTransform backpackTooltipReference { get => view.backpackTooltipReference; }
 37
 38    public event Action OnOpen;
 39    public event Action OnClose;
 40
 1941    public ProfileHUDController(IUserProfileBridge userProfileBridge)
 42    {
 1943        this.userProfileBridge = userProfileBridge;
 1944        mouseCatcher = InitialSceneReferences.i?.mouseCatcher;
 45
 46
 1947        view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("ProfileHUD")).GetComponent<ProfileHUDView>();
 1948        view.name = "_ProfileHUD";
 49
 1950        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange += ChangeVisibilityForBuilderInWor
 51
 1952        SetBackpackButtonVisibility(false);
 1953        view.connectedWalletSection.SetActive(false);
 1954        view.nonConnectedWalletSection.SetActive(false);
 1955        view.ActivateDescriptionEditionMode(false);
 56
 1957        view.buttonBackpack.onClick.AddListener(OpenBackpackWindow);
 1958        view.buttonLogOut.onClick.AddListener(WebInterface.LogOut);
 1959        view.buttonSignUp.onClick.AddListener(WebInterface.RedirectToSignUp);
 1960        view.buttonClaimName.onClick.AddListener(() => WebInterface.OpenURL(URL_CLAIM_NAME));
 1961        view.buttonTermsOfServiceForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1962        view.buttonPrivacyPolicyForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1963        view.buttonTermsOfServiceForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE);
 1964        view.buttonPrivacyPolicyForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 1965        view.inputName.onSubmit.AddListener(UpdateProfileName);
 1966        view.descriptionEditionInput.onSubmit.AddListener(UpdateProfileDescription);
 1967        view.OnOpen += () =>
 68        {
 169            WebInterface.RequestOwnProfileUpdate();
 170            OnOpen?.Invoke();
 071        };
 2072        view.OnClose += () => OnClose?.Invoke();
 73
 1974        if (view.manaCounterView)
 75        {
 1976            view.manaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1977            view.manaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCHASE));
 78        }
 79
 1980        if (view.polygonManaCounterView)
 81        {
 1982            view.polygonManaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO);
 1983            view.polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCH
 84        }
 85
 1986        ownUserProfile.OnUpdate += OnProfileUpdated;
 1987        if (mouseCatcher != null)
 1988            mouseCatcher.OnMouseLock += OnMouseLocked;
 89
 1990        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 91        {
 092            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 093            KernelConfig.i.OnChange += OnKernelConfigChanged;
 94        }
 1995    }
 96
 097    public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); }
 98
 99    public void SetVisibility(bool visible)
 100    {
 3101        view?.SetVisibility(visible);
 102
 3103        if (visible && fetchManaIntervalRoutine == null)
 104        {
 2105            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 2106        }
 1107        else if (!visible && fetchManaIntervalRoutine != null)
 108        {
 0109            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0110            fetchManaIntervalRoutine = null;
 111        }
 112
 3113        if (visible && fetchPolygonManaIntervalRoutine == null)
 114        {
 2115            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 2116        }
 1117        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 118        {
 0119            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0120            fetchPolygonManaIntervalRoutine = null;
 121        }
 1122    }
 123
 124    public void Dispose()
 125    {
 19126        if (fetchManaIntervalRoutine != null)
 127        {
 2128            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 2129            fetchManaIntervalRoutine = null;
 130        }
 131
 19132        if (view)
 133        {
 19134            GameObject.Destroy(view.gameObject);
 135        }
 19136        ownUserProfile.OnUpdate -= OnProfileUpdated;
 19137        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor
 19138        if (mouseCatcher != null)
 19139            mouseCatcher.OnMouseLock -= OnMouseLocked;
 140
 19141        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 142        {
 0143            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 144        }
 145
 19146        view.descriptionPreviewInput.onSubmit.RemoveListener(UpdateProfileDescription);
 19147    }
 148
 10149    void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); }
 150
 0151    void OnMouseLocked() { HideProfileMenu(); }
 152
 153    IEnumerator ManaIntervalRoutine()
 154    {
 0155        while (true)
 156        {
 2157            WebInterface.FetchBalanceOfMANA();
 2158            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 159        }
 160    }
 161
 162    IEnumerator PolygonManaIntervalRoutine()
 163    {
 0164        while (true)
 165        {
 4166            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 2167            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 2168            yield return promise;
 2169            SetPolygonManaBalance(promise.value);
 170
 2171            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0172        }
 173    }
 174
 175    /// <summary>
 176    /// Set an amount of MANA on the HUD.
 177    /// </summary>
 178    /// <param name="balance">Amount of MANA.</param>
 2179    public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); }
 180
 4181    public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); }
 182
 183    /// <summary>
 184    /// Configure an AvatarEditorHUDController for the Backpack button.
 185    /// </summary>
 186    /// <param name="controller">The avatar editor controller to asign.</param>
 187    public void AddBackpackWindow(AvatarEditorHUDController controller)
 188    {
 3189        if (controller == null)
 190        {
 1191            Debug.LogWarning("AddBackpackWindow >>> Backpack window doesn't exist yet!");
 1192            return;
 193        }
 194
 2195        avatarEditorHud = controller;
 2196        SetBackpackButtonVisibility(true);
 2197    }
 198
 199    /// <summary>
 200    /// Show/Hide the Backpack button.
 201    /// </summary>
 202    /// <param name="visible">True for showing the button.</param>
 46203    public void SetBackpackButtonVisibility(bool visible) { view?.SetBackpackButtonVisibility(avatarEditorHud != null &&
 204
 205    private void OpenBackpackWindow()
 206    {
 0207        if (avatarEditorHud == null)
 0208            return;
 209
 0210        avatarEditorHud.SetVisibility(true);
 0211        HideProfileMenu();
 0212    }
 213
 214    /// <summary>
 215    /// Close the Profile menu.
 216    /// </summary>
 0217    public void HideProfileMenu() { view?.HideMenu(); }
 218
 219    private void UpdateProfileName(string newName)
 220    {
 0221        if (view.inputName.wasCanceled)
 0222            return;
 223
 0224        if (!view.IsValidAvatarName(newName))
 225        {
 0226            view.inputName.ActivateInputField();
 0227            return;
 228        }
 229
 0230        if (view != null)
 231        {
 0232            view.SetProfileName(newName);
 0233            view.ActivateProfileNameEditionMode(false);
 234        }
 235
 0236        userProfileBridge.SaveUnverifiedName(newName);
 0237    }
 238
 0239    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre
 240
 241    private void UpdateProfileDescription(string description)
 242    {
 2243        if (view.descriptionEditionInput.wasCanceled
 244            || !ownUserProfile.hasConnectedWeb3
 245            || description.Length > view.descriptionEditionInput.characterLimit)
 246        {
 1247            view.ActivateDescriptionEditionMode(false);
 1248            return;
 249        }
 250
 1251        view.SetDescription(description);
 1252        view.ActivateDescriptionEditionMode(false);
 1253        userProfileBridge.SaveDescription(description);
 1254    }
 255}