< Summary

Class:NFTPromptHUDController
Assembly:NFTPromptHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NFTPromptHUD/NFTPromptHUDController.cs
Covered lines:39
Uncovered lines:52
Coverable lines:91
Total lines:195
Line coverage:42.8% (39 of 91)
Covered branches:0
Total branches:0

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%
SetNFT(...)0%6200%
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;
 2using DCL.Helpers.NFT;
 3using UnityEngine;
 4using Object = UnityEngine.Object;
 5
 6public class NFTPromptHUDController : IHUD
 7{
 8    internal const string VIEW_PREFAB_PATH = "NFTPromptHUD";
 9    internal const string COULD_NOT_FETCH_NFT_FROM_API = "Couldn't fetch NFT: '{0}/{1}'.";
 10    internal const string DOES_NOT_SUPPORT_POLYGON = "Warning: OpenSea API does not support fetching Polygon assets.";
 11
 012    internal INFTPromptHUDView view { get; private set; }
 13
 14    private readonly OwnersInfoController ownersInfoController;
 15
 16    private Coroutine fetchNFTRoutine = null;
 17    private NFTInfoSingleAsset? lastNFTInfo = null;
 18
 19    private bool isPointerInTooltipArea = false;
 20    private bool isPointerInOwnerArea = false;
 21
 722    public NFTPromptHUDController()
 23    {
 724        view = Object.Instantiate(Resources.Load<GameObject>(VIEW_PREFAB_PATH))
 25            .GetComponent<NFTPromptHUDView>();
 726        view.SetActive(false);
 27
 728        view.OnOwnerLabelPointerEnter += ShowOwnersTooltip;
 729        view.OnOwnerLabelPointerExit += TryHideOwnersTooltip;
 730        view.OnOwnersTooltipFocusLost += OnOwnersTooltipFocusLost;
 731        view.OnOwnersTooltipFocus += OnOwnersTooltipFocus;
 732        view.OnViewAllPressed += ShowOwnersPopup;
 733        view.OnOwnersPopupClosed += HideOwnersPopup;
 34
 735        ownersInfoController = new OwnersInfoController(view.GetOwnerElementPrefab());
 736        DataStore.i.onOpenNFTPrompt.OnChange += OpenNftInfoDialog;
 737    }
 38
 39    public void OpenNftInfoDialog(NFTPromptModel model, NFTPromptModel prevModel)
 40    {
 141        if (!view.IsActive())
 42        {
 143            HideOwnersTooltip(true);
 144            HideOwnersPopup(true);
 45
 146            if (fetchNFTRoutine != null)
 047                CoroutineStarter.Stop(fetchNFTRoutine);
 48
 149            if (lastNFTInfo != null && lastNFTInfo.Value.Equals(model.contactAddress, model.tokenId))
 50            {
 051                SetNFT(lastNFTInfo.Value, model.comment, false);
 052                return;
 53            }
 54
 155            view.SetLoading();
 56
 157            fetchNFTRoutine = CoroutineStarter.Start(NFTUtils.FetchNFTInfoSingleAsset(model.contactAddress, model.tokenI
 058                (nftInfo) => SetNFT(nftInfo, model.comment, true),
 059                (error) => view.OnError(string.Format(COULD_NOT_FETCH_NFT_FROM_API + " " + error + ". " + DOES_NOT_SUPPO
 60            ));
 61        }
 162    }
 63
 64    public void SetVisibility(bool visible)
 65    {
 166        view.SetActive(visible);
 67
 168        if (!visible)
 69        {
 070            HideOwnersTooltip(true);
 071            HideOwnersPopup(true);
 72        }
 73
 174        AudioScriptableObjects.dialogOpen.Play(true);
 175    }
 76
 77    public void Dispose()
 78    {
 779        view.OnOwnerLabelPointerEnter -= ShowOwnersTooltip;
 780        view.OnOwnerLabelPointerExit -= TryHideOwnersTooltip;
 781        view.OnOwnersTooltipFocusLost -= OnOwnersTooltipFocusLost;
 782        view.OnOwnersTooltipFocus -= OnOwnersTooltipFocus;
 783        view.OnViewAllPressed -= ShowOwnersPopup;
 784        view.OnOwnersPopupClosed -= HideOwnersPopup;
 85
 786        CoroutineStarter.Stop(fetchNFTRoutine);
 87
 788        view?.Dispose();
 89
 790        DataStore.i.onOpenNFTPrompt.OnChange -= OpenNftInfoDialog;
 791    }
 92
 93    private void SetNFT(NFTInfoSingleAsset info, string comment, bool shouldRefreshOwners)
 94    {
 095        lastNFTInfo = info;
 096        view.SetNFTInfo(info, comment);
 097        if (shouldRefreshOwners)
 98        {
 099            ownersInfoController.SetOwners(info.owners);
 100        }
 0101    }
 102
 103    private void ShowOwnersTooltip()
 104    {
 0105        isPointerInOwnerArea = true;
 106
 0107        if (lastNFTInfo == null || lastNFTInfo.Value.owners.Length <= 1)
 0108            return;
 109
 0110        var tooltip = view.GetOwnersTooltip();
 111
 0112        if (tooltip.IsActive())
 113        {
 0114            tooltip.KeepOnScreen();
 0115            return;
 116        }
 117
 0118        tooltip.SetElements(ownersInfoController.GetElements());
 0119        tooltip.Show();
 0120    }
 121
 122    void TryHideOwnersTooltip()
 123    {
 0124        isPointerInOwnerArea = false;
 125
 0126        if (!isPointerInTooltipArea)
 127        {
 0128            HideOwnersTooltip();
 129        }
 0130    }
 131
 132    void OnOwnersTooltipFocusLost()
 133    {
 0134        isPointerInTooltipArea = false;
 0135        if (!isPointerInOwnerArea)
 136        {
 0137            HideOwnersTooltip();
 138        }
 0139    }
 140
 141    void OnOwnersTooltipFocus()
 142    {
 0143        isPointerInTooltipArea = true;
 0144        var tooltip = view.GetOwnersTooltip();
 0145        if (tooltip.IsActive())
 146        {
 0147            tooltip.KeepOnScreen();
 148        }
 0149    }
 150
 0151    private void HideOwnersTooltip() { HideOwnersTooltip(false); }
 152
 153    private void HideOwnersTooltip(bool instant)
 154    {
 1155        var tooltip = view.GetOwnersTooltip();
 1156        if (!tooltip.IsActive())
 0157            return;
 158
 1159        tooltip.Hide(instant);
 1160    }
 161
 162    private void ShowOwnersPopup()
 163    {
 0164        HideOwnersTooltip(true);
 0165        isPointerInTooltipArea = false;
 0166        isPointerInOwnerArea = false;
 167
 0168        var popup = view.GetOwnersPopup();
 169
 0170        if (popup.IsActive())
 0171            return;
 172
 0173        popup.SetElements(ownersInfoController.GetElements());
 0174        popup.Show();
 0175    }
 176
 0177    private void HideOwnersPopup() { HideOwnersPopup(false); }
 178
 2179    private void HideOwnersPopup(bool instant) { view.GetOwnersPopup().Hide(instant); }
 180
 181    internal static string FormatOwnerAddress(string address, int maxCharacters)
 182    {
 183        const string ellipsis = "...";
 184
 0185        if (address.Length <= maxCharacters)
 186        {
 0187            return address;
 188        }
 189        else
 190        {
 0191            int segmentLength = Mathf.FloorToInt((maxCharacters - ellipsis.Length) * 0.5f);
 0192            return $"{address.Substring(0, segmentLength)}{ellipsis}{address.Substring(address.Length - segmentLength, s
 193        }
 194    }
 195}