< Summary

Class:ProfileHUDController
Assembly:ProfileHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ProfileHUD/ProfileHUDController.cs
Covered lines:62
Uncovered lines:40
Coverable lines:102
Total lines:246
Line coverage:60.7% (62 of 102)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:19
Method coverage:47.3% (9 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProfileHUDController(...)0%44093.55%
OnSignedUp(...)0%110100%
OnLoggedOut(...)0%110100%
SetProfileCardExtended(...)0%6200%
SetManaBalance(...)0%6200%
SetPolygonManaBalance(...)0%6200%
SetVisibility(...)0%29.889036.36%
Dispose()0%44093.75%
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 Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Browser;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using DCL.MyAccount;
 7using DCL.Tasks;
 8using SocialFeaturesAnalytics;
 9using System;
 10using System.Collections;
 11using System.Text.RegularExpressions;
 12using System.Threading;
 13using UnityEngine;
 14using Environment = DCL.Environment;
 15using WaitUntil = UnityEngine.WaitUntil;
 16
 17public class ProfileHUDController : IHUD
 18{
 19    [Serializable]
 20    public struct Configuration
 21    {
 22        public bool connectedWallet;
 23    }
 24
 25    private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name";
 26    private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet";
 27    private const string URL_MANA_PURCHASE = "https://account.decentraland.org";
 28    private const string URL_TERMS_OF_USE = "https://decentraland.org/terms";
 29    private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy";
 30    private const string LINKS_REGEX = @"\[(.*?)\)";
 31    private const float FETCH_MANA_INTERVAL = 60;
 32
 33    public readonly IProfileHUDView view;
 34    private readonly IUserProfileBridge userProfileBridge;
 35    private readonly ISocialAnalytics socialAnalytics;
 36    private readonly DataStore dataStore;
 37    private readonly MyAccountCardController myAccountCardController;
 38    private readonly IBrowserBridge browserBridge;
 39
 40    public event Action OnOpen;
 41    public event Action OnClose;
 42
 43    internal AvatarEditorHUDController avatarEditorHud;
 44
 95245    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 46    private Coroutine fetchManaIntervalRoutine = null;
 47    private Coroutine fetchPolygonManaIntervalRoutine = null;
 48    private CancellationTokenSource saveNameCancellationToken;
 49    private CancellationTokenSource saveDescriptionCancellationToken;
 50
 51    private Regex nameRegex = null;
 52
 053    public RectTransform TutorialTooltipReference => view.TutorialReference;
 54
 655    public ProfileHUDController(
 56        IProfileHUDView view,
 57        IUserProfileBridge userProfileBridge,
 58        ISocialAnalytics socialAnalytics,
 59        DataStore dataStore,
 60        MyAccountCardController myAccountCardController,
 61        IBrowserBridge browserBridge)
 62    {
 663        this.userProfileBridge = userProfileBridge;
 664        this.socialAnalytics = socialAnalytics;
 665        this.dataStore = dataStore;
 666        this.view = view;
 667        this.myAccountCardController = myAccountCardController;
 668        this.browserBridge = browserBridge;
 69
 670        dataStore.exploreV2.isOpen.OnChange += SetAsFullScreenMenuMode;
 671        dataStore.exploreV2.profileCardIsOpen.OnChange += SetProfileCardExtended;
 72
 673        view.SetWalletSectionEnabled(false);
 674        view.SetNonWalletSectionEnabled(false);
 675        view.SetDescriptionIsEditing(false);
 76
 677        view.LogedOutPressed += OnLoggedOut;
 678        view.SignedUpPressed += OnSignedUp;
 79
 680        view.ClaimNamePressed += (object sender, EventArgs args) => browserBridge.OpenUrl(URL_CLAIM_NAME);
 81
 682        view.Opened += (object sender, EventArgs args) =>
 83        {
 184            userProfileBridge.RequestOwnProfileUpdate();
 185            OnOpen?.Invoke();
 186        };
 87
 788        view.Closed += (object sender, EventArgs args) => OnClose?.Invoke();
 689        view.NameSubmitted += (object sender, string name) => UpdateProfileName(name);
 690        view.DescriptionSubmitted += (object sender, string description) => UpdateProfileDescription(description);
 91
 692        view.TermsAndServicesPressed += (object sender, EventArgs args) => browserBridge.OpenUrl(URL_TERMS_OF_USE);
 693        view.PrivacyPolicyPressed += (object sender, EventArgs args) => browserBridge.OpenUrl(URL_PRIVACY_POLICY);
 94
 695        if (view.HasManaCounterView() || view.HasPolygonManaCounterView())
 96        {
 197            view.ManaInfoPressed += (object sender, EventArgs args) => browserBridge.OpenUrl(URL_MANA_INFO);
 198            view.ManaPurchasePressed += (object sender, EventArgs args) => browserBridge.OpenUrl(URL_MANA_PURCHASE);
 99        }
 100
 6101        ownUserProfile.OnUpdate += OnProfileUpdated;
 102
 6103        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 104        {
 0105            KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null));
 0106            KernelConfig.i.OnChange += OnKernelConfigChanged;
 107        }
 108
 6109        dataStore.exploreV2.isInitialized.OnChange += ExploreV2Changed;
 6110        ExploreV2Changed(dataStore.exploreV2.isInitialized.Get(), false);
 6111    }
 112
 113    private void OnSignedUp(object sender, EventArgs e)
 114    {
 1115        DCL.SettingsCommon.Settings.i.SaveSettings();
 1116        userProfileBridge.SignUp();
 1117    }
 118
 119    private void OnLoggedOut(object sender, EventArgs e)
 120    {
 1121        DCL.SettingsCommon.Settings.i.SaveSettings();
 1122        userProfileBridge.LogOut();
 1123    }
 124
 125    private void SetProfileCardExtended(bool isOpenCurrent, bool previous)
 126    {
 0127        OnOpen?.Invoke();
 0128        view.ShowExpanded(isOpenCurrent, dataStore.myAccount.isInitialized.Get());
 0129    }
 130
 131    public void SetManaBalance(string balance) =>
 0132        view?.SetManaBalance(balance);
 133
 134    public void SetPolygonManaBalance(double balance) =>
 0135        view?.SetPolygonBalance(balance);
 136
 137    public void SetVisibility(bool visible)
 138    {
 1139        if (visible && fetchManaIntervalRoutine == null)
 1140            fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine());
 0141        else if (!visible && fetchManaIntervalRoutine != null)
 142        {
 0143            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 0144            fetchManaIntervalRoutine = null;
 145        }
 146
 1147        if (visible && fetchPolygonManaIntervalRoutine == null)
 1148            fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine());
 0149        else if (!visible && fetchPolygonManaIntervalRoutine != null)
 150        {
 0151            CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine);
 0152            fetchPolygonManaIntervalRoutine = null;
 153        }
 0154    }
 155
 156    public virtual void Dispose()
 157    {
 6158        view.LogedOutPressed -= OnLoggedOut;
 6159        view.SignedUpPressed -= OnSignedUp;
 160
 6161        if (fetchManaIntervalRoutine != null)
 162        {
 1163            CoroutineStarter.Stop(fetchManaIntervalRoutine);
 1164            fetchManaIntervalRoutine = null;
 165        }
 166
 6167        if (view.GameObject)
 1168            UnityEngine.Object.Destroy(view.GameObject);
 169
 6170        ownUserProfile.OnUpdate -= OnProfileUpdated;
 171
 6172        if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS)
 0173            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 174
 6175        dataStore.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode;
 6176        dataStore.exploreV2.isInitialized.OnChange -= ExploreV2Changed;
 177
 6178        myAccountCardController.Dispose();
 6179        saveNameCancellationToken.SafeCancelAndDispose();
 6180        saveDescriptionCancellationToken.SafeCancelAndDispose();
 6181    }
 182
 183    private void OnProfileUpdated(UserProfile profile) =>
 0184        view?.SetProfile(profile);
 185
 186    private void ExploreV2Changed(bool current, bool previous) =>
 6187        view.SetStartMenuButtonActive(current);
 188
 189    private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) =>
 0190        nameRegex = new Regex(current.profiles.nameValidRegex);
 191
 192    private void UpdateProfileName(string newName)
 193    {
 0194        if (nameRegex != null && !nameRegex.IsMatch(newName))
 0195            return;
 196
 0197        saveNameCancellationToken = saveNameCancellationToken.SafeRestart();
 0198        userProfileBridge.SaveUnverifiedName(newName, saveNameCancellationToken.Token).Forget();
 0199    }
 200
 201    private void UpdateProfileDescription(string description)
 202    {
 0203        if (!ownUserProfile.hasConnectedWeb3 || view.IsDesciptionIsLongerThanMaxCharacters())
 0204            return;
 205
 0206        saveDescriptionCancellationToken = saveDescriptionCancellationToken.SafeRestart();
 0207        userProfileBridge.SaveDescription(description, saveDescriptionCancellationToken.Token).Forget();
 0208        socialAnalytics.SendProfileEdit(description.Length, ContainsLinks(description), PlayerActionSource.ProfileEditHU
 0209    }
 210
 211    private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode)
 212    {
 0213        view.ShowProfileIcon(!currentIsFullScreenMenuMode);
 0214    }
 215
 216    private IEnumerator ManaIntervalRoutine()
 217    {
 0218        while (true)
 219        {
 1220            WebInterface.FetchBalanceOfMANA();
 1221            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 222        }
 223    }
 224
 225    private static bool ContainsLinks(string description) =>
 0226        Regex.Matches(description, LINKS_REGEX, RegexOptions.IgnoreCase).Count > 0;
 227
 228    private IEnumerator PolygonManaIntervalRoutine()
 229    {
 0230        while (true)
 231        {
 471232            yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId));
 233
 0234            Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryMana(ownUserProfile.userId, 
 235
 236            // This can be null if theGraph is mocked
 0237            if (promise != null)
 238            {
 0239                yield return promise;
 0240                SetPolygonManaBalance(promise.value);
 241            }
 242
 0243            yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL);
 0244        }
 245    }
 246}