< Summary

Class:NFTPromptHUDController
Assembly:NFTPromptHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NFTPromptHUD/NFTPromptHUDController.cs
Covered lines:46
Uncovered lines:51
Coverable lines:97
Total lines:209
Line coverage:47.4% (46 of 97)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:18
Method coverage:50% (9 of 18)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NFTPromptHUDController(...)0%110100%
OpenNftInfoDialog(...)0%5.515072.73%
SetVisibility(...)0%2.152066.67%
Dispose()0%220100%
OpenPromptRequest(...)0%110100%
SetNFT(...)0%12300%
ShowOwnersTooltip()0%20400%
TryHideOwnersTooltip()0%6200%
OnOwnersTooltipFocusLost()0%6200%
OnOwnersTooltipFocus()0%6200%
HideOwnersTooltip()0%2100%
HideOwnersTooltip(...)0%2.032080%
ShowOwnersPopup()0%6200%
HideOwnersPopup()0%2100%
HideOwnersPopup(...)0%110100%
FormatOwnerAddress(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NFTPromptHUD/NFTPromptHUDController.cs

#LineLine coverage
 1using DCL.Helpers.NFT;
 2using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces;
 3using RPC.Context;
 4using UnityEngine;
 5using Object = UnityEngine.Object;
 6
 7public class NFTPromptHUDController : IHUD
 8{
 9    internal const string VIEW_PREFAB_PATH = "NFTPromptHUD";
 10    internal const string COULD_NOT_FETCH_NFT_FROM_API = "Couldn't fetch NFT: '{0}/{1}'.";
 11    internal const string DOES_NOT_SUPPORT_POLYGON = "Warning: OpenSea API does not support fetching Polygon assets.";
 12
 14413    internal INFTPromptHUDView view { get; private set; }
 14
 15    private readonly OwnersInfoController ownersInfoController;
 16    private readonly RestrictedActionsContext restrictedActionsContext;
 17    private readonly BaseVariable<NFTPromptModel> openNftPromptVariable;
 18
 19    private Coroutine fetchNFTRoutine = null;
 20    private NFTInfo? lastNFTInfo;
 21
 22    private bool isPointerInTooltipArea = false;
 23    private bool isPointerInOwnerArea = false;
 24
 825    public NFTPromptHUDController(RestrictedActionsContext restrictedActionsContext, BaseVariable<NFTPromptModel> openNf
 26    {
 827        view = Object.Instantiate(Resources.Load<GameObject>(VIEW_PREFAB_PATH))
 28            .GetComponent<NFTPromptHUDView>();
 829        view.SetActive(false);
 30
 831        view.OnOwnerLabelPointerEnter += ShowOwnersTooltip;
 832        view.OnOwnerLabelPointerExit += TryHideOwnersTooltip;
 833        view.OnOwnersTooltipFocusLost += OnOwnersTooltipFocusLost;
 834        view.OnOwnersTooltipFocus += OnOwnersTooltipFocus;
 835        view.OnViewAllPressed += ShowOwnersPopup;
 836        view.OnOwnersPopupClosed += HideOwnersPopup;
 37
 838        ownersInfoController = new OwnersInfoController(view.GetOwnerElementPrefab());
 39
 840        this.openNftPromptVariable = openNftPromptVariable;
 841        openNftPromptVariable.OnChange += OpenNftInfoDialog;
 42
 843        this.restrictedActionsContext = restrictedActionsContext;
 844        restrictedActionsContext.OpenNftPrompt += OpenPromptRequest;
 845    }
 46
 47    public void OpenNftInfoDialog(NFTPromptModel model, NFTPromptModel prevModel)
 48    {
 249        if (!view.IsActive())
 50        {
 251            HideOwnersTooltip(true);
 252            HideOwnersPopup(true);
 53
 254            if (fetchNFTRoutine != null)
 055                CoroutineStarter.Stop(fetchNFTRoutine);
 56
 257            if (lastNFTInfo != null && lastNFTInfo.Value.Equals(model.contactAddress, model.tokenId))
 58            {
 059                SetNFT(lastNFTInfo.Value, model.comment, false);
 060                return;
 61            }
 62
 263            view.SetLoading();
 64
 265            fetchNFTRoutine = CoroutineStarter.Start(NFTUtils.FetchNFTInfoSingleAsset(model.chain, model.contactAddress,
 066                (nftInfo) => SetNFT(nftInfo, model.comment, true),
 067                (error) => view.OnError(string.Format(COULD_NOT_FETCH_NFT_FROM_API + " " + error + ". " + DOES_NOT_SUPPO
 68            ));
 69        }
 270    }
 71
 72    public void SetVisibility(bool visible)
 73    {
 174        view.SetActive(visible);
 75
 176        if (!visible)
 77        {
 078            HideOwnersTooltip(true);
 079            HideOwnersPopup(true);
 80        }
 81
 182        AudioScriptableObjects.dialogOpen.Play(true);
 183    }
 84
 85    public void Dispose()
 86    {
 887        view.OnOwnerLabelPointerEnter -= ShowOwnersTooltip;
 888        view.OnOwnerLabelPointerExit -= TryHideOwnersTooltip;
 889        view.OnOwnersTooltipFocusLost -= OnOwnersTooltipFocusLost;
 890        view.OnOwnersTooltipFocus -= OnOwnersTooltipFocus;
 891        view.OnViewAllPressed -= ShowOwnersPopup;
 892        view.OnOwnersPopupClosed -= HideOwnersPopup;
 93
 894        CoroutineStarter.Stop(fetchNFTRoutine);
 95
 896        view?.Dispose();
 97
 898        openNftPromptVariable.OnChange -= OpenNftInfoDialog;
 899        restrictedActionsContext.OpenNftPrompt -= OpenPromptRequest;
 8100    }
 101
 102    private void OpenPromptRequest(string chain, string contractAddress, string tokenId)
 103    {
 1104        openNftPromptVariable.Set(new NFTPromptModel(chain, contractAddress, tokenId, string.Empty));
 1105    }
 106
 107    private void SetNFT(NFTInfo info, string comment, bool shouldRefreshOwners)
 108    {
 0109        lastNFTInfo = info;
 0110        view.SetNFTInfo(info, comment);
 0111        if (shouldRefreshOwners)
 112        {
 0113            if(info.owners != null) ownersInfoController.SetOwners(info.owners);
 114        }
 0115    }
 116
 117    private void ShowOwnersTooltip()
 118    {
 0119        isPointerInOwnerArea = true;
 120
 0121        if (lastNFTInfo == null || lastNFTInfo.Value.owners.Length <= 1)
 0122            return;
 123
 0124        var tooltip = view.GetOwnersTooltip();
 125
 0126        if (tooltip.IsActive())
 127        {
 0128            tooltip.KeepOnScreen();
 0129            return;
 130        }
 131
 0132        tooltip.SetElements(ownersInfoController.GetElements());
 0133        tooltip.Show();
 0134    }
 135
 136    void TryHideOwnersTooltip()
 137    {
 0138        isPointerInOwnerArea = false;
 139
 0140        if (!isPointerInTooltipArea)
 141        {
 0142            HideOwnersTooltip();
 143        }
 0144    }
 145
 146    void OnOwnersTooltipFocusLost()
 147    {
 0148        isPointerInTooltipArea = false;
 0149        if (!isPointerInOwnerArea)
 150        {
 0151            HideOwnersTooltip();
 152        }
 0153    }
 154
 155    void OnOwnersTooltipFocus()
 156    {
 0157        isPointerInTooltipArea = true;
 0158        var tooltip = view.GetOwnersTooltip();
 0159        if (tooltip.IsActive())
 160        {
 0161            tooltip.KeepOnScreen();
 162        }
 0163    }
 164
 0165    private void HideOwnersTooltip() { HideOwnersTooltip(false); }
 166
 167    private void HideOwnersTooltip(bool instant)
 168    {
 2169        var tooltip = view.GetOwnersTooltip();
 2170        if (!tooltip.IsActive())
 0171            return;
 172
 2173        tooltip.Hide(instant);
 2174    }
 175
 176    private void ShowOwnersPopup()
 177    {
 0178        HideOwnersTooltip(true);
 0179        isPointerInTooltipArea = false;
 0180        isPointerInOwnerArea = false;
 181
 0182        var popup = view.GetOwnersPopup();
 183
 0184        if (popup.IsActive())
 0185            return;
 186
 0187        popup.SetElements(ownersInfoController.GetElements());
 0188        popup.Show();
 0189    }
 190
 0191    private void HideOwnersPopup() { HideOwnersPopup(false); }
 192
 4193    private void HideOwnersPopup(bool instant) { view.GetOwnersPopup().Hide(instant); }
 194
 195    internal static string FormatOwnerAddress(string address, int maxCharacters)
 196    {
 197        const string ellipsis = "...";
 198
 0199        if (address.Length <= maxCharacters)
 200        {
 0201            return address;
 202        }
 203        else
 204        {
 0205            int segmentLength = Mathf.FloorToInt((maxCharacters - ellipsis.Length) * 0.5f);
 0206            return $"{address.Substring(0, segmentLength)}{ellipsis}{address.Substring(address.Length - segmentLength, s
 207        }
 208    }
 209}