< Summary

Class:ProfileHUDController
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDController.cs
Covered lines:50
Uncovered lines:48
Coverable lines:98
Total lines:235
Line coverage:51% (50 of 98)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController(...)0%9.029093.33%
OnSignedUp(...)0%2100%
OnLoggedOut(...)0%2100%
SetProfileCardExtended(...)0%6200%
ChangeVisibilityForBuilderInWorld(...)0%2100%
SetManaBalance(...)0%6200%
SetPolygonManaBalance(...)0%6200%
SetVisibility(...)0%29.889036.36%
Dispose()0%4.014092.31%
GetViewPrefab()0%110100%
OnProfileUpdated(...)0%6200%
ExploreV2Changed(...)0%110100%
OnKernelConfigChanged(...)0%2100%
UpdateProfileName(...)0%12300%
UpdateProfileDescription(...)0%12300%
SetAsFullScreenMenuMode(...)0%2100%
ManaIntervalRoutine()0%3.333066.67%
ContainsLinks(...)0%2100%
PolygonManaIntervalRoutine()0%30.126012.5%

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 SocialFeaturesAnalytics;
 5using System;
 6using System.Collections;
 7using System.Text.RegularExpressions;
 8using UnityEngine;
 9using Environment = DCL.Environment;
 10using WaitUntil = UnityEngine.WaitUntil;
 11
 12public class ProfileHUDController : IHUD
 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 string VIEW_NAME = "_ProfileHUD";
 26    private const string LINKS_REGEX = @"\[(.*?)\)";
 27    private const float FETCH_MANA_INTERVAL = 60;
 28
 29    public readonly IProfileHUDView view;
 30    private readonly IUserProfileBridge userProfileBridge;
 31    private readonly ISocialAnalytics socialAnalytics;
 32
 33    public event Action OnOpen;
 34    public event Action OnClose;
 35
 36    internal AvatarEditorHUDController avatarEditorHud;
 37
 1638    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 39    private Coroutine fetchManaIntervalRoutine = null;
 40    private Coroutine fetchPolygonManaIntervalRoutine = null;
 41
 42    private Regex nameRegex = null;
 43
 044    public RectTransform TutorialTooltipReference => view.TutorialReference;
 45
 246    public ProfileHUDController(
 47        IUserProfileBridge userProfileBridge,
 48        ISocialAnalytics socialAnalytics)
 49    {
 250        this.userProfileBridge = userProfileBridge;
 251        this.socialAnalytics = socialAnalytics;
 52
 253        GameObject viewGo = UnityEngine.Object.Instantiate(GetViewPrefab());
 254        viewGo.name = VIEW_NAME;
 255        view = viewGo.GetComponent<IProfileHUDView>();
 56
 257        DataStore.i.exploreV2.isOpen.OnChange += SetAsFullScreenMenuMode;
 258        DataStore.i.exploreV2.profileCardIsOpen.OnChange += SetProfileCardExtended;
 59
 260        view.SetWalletSectionEnabled(false);
 261        view.SetNonWalletSectionEnabled(false);
 262        view.SetDescriptionIsEditing(false);
 63
 264        view.LogedOutPressed += OnLoggedOut;
 265        view.SignedUpPressed += OnSignedUp;
 66
 267        view.ClaimNamePressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_CLAIM_NAME);
 68
 269        view.Opened += (object sender, EventArgs args) =>
 70        {
 071            WebInterface.RequestOwnProfileUpdate();
 072            OnOpen?.Invoke();
 073        };
 74
 275        view.Closed += (object sender, EventArgs args) => OnClose?.Invoke();
 276        view.NameSubmitted += (object sender, string name) => UpdateProfileName(name);
 277        view.DescriptionSubmitted += (object sender, string description) => UpdateProfileDescription(description);
 78
 279        view.TermsAndServicesPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_TERMS_OF_USE);
 280        view.PrivacyPolicyPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_PRIVACY_POLICY);
 81
 282        if (view.HasManaCounterView() || view.HasPolygonManaCounterView())
 83        {
 284            view.ManaInfoPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_MANA_INFO);
 285            view.ManaPurchasePressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_MANA_PURCHASE);
 86        }
 87
 288        ownUserProfile.OnUpdate += OnProfileUpdated;
 89
 290        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 91        {
 092            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 093            KernelConfig.i.OnChange += OnKernelConfigChanged;
 94        }
 95
 296        DataStore.i.exploreV2.isInitialized.OnChange += ExploreV2Changed;
 297        ExploreV2Changed(DataStore.i.exploreV2.isInitialized.Get(), false);
 298    }
 99
 100    private void OnSignedUp(object sender, EventArgs e)
 101    {
 0102        DCL.SettingsCommon.Settings.i.SaveSettings();
 0103        WebInterface.RedirectToSignUp();
 0104    }
 105
 106    private void OnLoggedOut(object sender, EventArgs e)
 107    {
 0108        DCL.SettingsCommon.Settings.i.SaveSettings();
 0109        WebInterface.LogOut();
 0110    }
 111
 112    private void SetProfileCardExtended(bool isOpenCurrent, bool previous)
 113    {
 0114        OnOpen?.Invoke();
 0115        view.ShowExpanded(isOpenCurrent);
 0116    }
 117
 118    public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) =>
 0119        view.GameObject.SetActive(current);
 120
 121    public void SetManaBalance(string balance) =>
 0122        view?.SetManaBalance(balance);
 123
 124    public void SetPolygonManaBalance(double balance) =>
 0125        view?.SetPolygonBalance(balance);
 126
 127    public void SetVisibility(bool visible)
 128    {
 1129        if (visible && fetchManaIntervalRoutine == null)
 1130            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 0131        else if (!visible && fetchManaIntervalRoutine != null)
 132        {
 0133            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0134            fetchManaIntervalRoutine = null;
 135        }
 136
 1137        if (visible && fetchPolygonManaIntervalRoutine == null)
 1138            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 0139        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 140        {
 0141            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0142            fetchPolygonManaIntervalRoutine = null;
 143        }
 0144    }
 145
 146    public void Dispose()
 147    {
 2148        view.LogedOutPressed -= OnLoggedOut;
 2149        view.SignedUpPressed -= OnSignedUp;
 150
 2151        if (fetchManaIntervalRoutine != null)
 152        {
 1153            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 1154            fetchManaIntervalRoutine = null;
 155        }
 156
 2157        if (view.GameObject)
 2158            UnityEngine.Object.Destroy(view.GameObject);
 159
 2160        ownUserProfile.OnUpdate -= OnProfileUpdated;
 161
 2162        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 0163            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 164
 2165        DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode;
 2166        DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed;
 2167    }
 168
 169    protected virtual GameObject GetViewPrefab()
 170    {
 2171        return Resources.Load<GameObject>("ProfileHUD_V2");
 172    }
 173
 174    private void OnProfileUpdated(UserProfile profile) =>
 0175        view?.SetProfile(profile);
 176
 177    private void ExploreV2Changed(bool current, bool previous) =>
 2178        view.SetStartMenuButtonActive(current);
 179
 180    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) =>
 0181        nameRegex = new Regex(current.profiles.nameValidRegex);
 182
 183    private void UpdateProfileName(string newName)
 184    {
 0185        if (nameRegex != null && !nameRegex.IsMatch(newName))
 0186            return;
 187
 0188        userProfileBridge.SaveUnverifiedName(newName);
 0189    }
 190
 191    private void UpdateProfileDescription(string description)
 192    {
 0193        if (!ownUserProfile.hasConnectedWeb3 || view.IsDesciptionIsLongerThanMaxCharacters())
 0194            return;
 195
 0196        userProfileBridge.SaveDescription(description);
 0197        socialAnalytics.SendProfileEdit(description.Length, ContainsLinks(description), PlayerActionSource.ProfileEditHU
 0198    }
 199
 200    private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode)
 201    {
 0202        view.ShowProfileIcon(!currentIsFullScreenMenuMode);
 0203    }
 204
 205    private IEnumerator ManaIntervalRoutine()
 206    {
 0207        while (true)
 208        {
 1209            WebInterface.FetchBalanceOfMANA();
 1210            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 211        }
 212    }
 213
 214    private static bool ContainsLinks(string description) =>
 0215        Regex.Matches(description, LINKS_REGEX, RegexOptions.IgnoreCase).Count > 0;
 216
 217    private IEnumerator PolygonManaIntervalRoutine()
 218    {
 0219        while (true)
 220        {
 7221            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 222
 0223            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u
 224
 225            // This can be null if theGraph is mocked
 0226            if (promise != null)
 227            {
 0228                yield return promise;
 0229                SetPolygonManaBalance(promise.value);
 230            }
 231
 0232            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0233        }
 234    }
 235}