< 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:193
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
 010    internal INFTPromptHUDView view { get; private set; }
 11
 12    private readonly OwnersInfoController ownersInfoController;
 13
 14    private Coroutine fetchNFTRoutine = null;
 15    private NFTInfoSingleAsset? lastNFTInfo = null;
 16
 17    private bool isPointerInTooltipArea = false;
 18    private bool isPointerInOwnerArea = false;
 19
 720    public NFTPromptHUDController()
 21    {
 722        view = Object.Instantiate(Resources.Load<GameObject>(VIEW_PREFAB_PATH))
 23                     .GetComponent<NFTPromptHUDView>();
 724        view.SetActive(false);
 25
 726        view.OnOwnerLabelPointerEnter += ShowOwnersTooltip;
 727        view.OnOwnerLabelPointerExit += TryHideOwnersTooltip;
 728        view.OnOwnersTooltipFocusLost += OnOwnersTooltipFocusLost;
 729        view.OnOwnersTooltipFocus += OnOwnersTooltipFocus;
 730        view.OnViewAllPressed += ShowOwnersPopup;
 731        view.OnOwnersPopupClosed += HideOwnersPopup;
 32
 733        ownersInfoController = new OwnersInfoController(view.GetOwnerElementPrefab());
 734        DataStore.i.onOpenNFTPrompt.OnChange += OpenNftInfoDialog;
 735    }
 36
 37    public void OpenNftInfoDialog(NFTPromptModel model, NFTPromptModel prevModel)
 38    {
 139        if (!view.IsActive())
 40        {
 141            HideOwnersTooltip(true);
 142            HideOwnersPopup(true);
 43
 144            if (fetchNFTRoutine != null)
 045                CoroutineStarter.Stop(fetchNFTRoutine);
 46
 147            if (lastNFTInfo != null && lastNFTInfo.Value.Equals(model.contactAddress, model.tokenId))
 48            {
 049                SetNFT(lastNFTInfo.Value, model.comment, false);
 050                return;
 51            }
 52
 153            view.SetLoading();
 54
 155            fetchNFTRoutine = CoroutineStarter.Start(NFTHelper.FetchNFTInfoSingleAsset(model.contactAddress, model.token
 056                (nftInfo) => SetNFT(nftInfo, model.comment, true),
 057                (error) => view.OnError(error)
 58            ));
 59        }
 160    }
 61
 62    public void SetVisibility(bool visible)
 63    {
 164        view.SetActive(visible);
 65
 166        if (!visible)
 67        {
 068            HideOwnersTooltip(true);
 069            HideOwnersPopup(true);
 70        }
 71
 172        AudioScriptableObjects.dialogOpen.Play(true);
 173    }
 74
 75    public void Dispose()
 76    {
 777        view.OnOwnerLabelPointerEnter -= ShowOwnersTooltip;
 778        view.OnOwnerLabelPointerExit -= TryHideOwnersTooltip;
 779        view.OnOwnersTooltipFocusLost -= OnOwnersTooltipFocusLost;
 780        view.OnOwnersTooltipFocus -= OnOwnersTooltipFocus;
 781        view.OnViewAllPressed -= ShowOwnersPopup;
 782        view.OnOwnersPopupClosed -= HideOwnersPopup;
 83
 784        CoroutineStarter.Stop(fetchNFTRoutine);
 85
 786        view?.Dispose();
 87
 788        DataStore.i.onOpenNFTPrompt.OnChange -= OpenNftInfoDialog;
 789    }
 90
 91    private void SetNFT(NFTInfoSingleAsset info, string comment, bool shouldRefreshOwners)
 92    {
 093        lastNFTInfo = info;
 094        view.SetNFTInfo(info, comment);
 095        if (shouldRefreshOwners)
 96        {
 097            ownersInfoController.SetOwners(info.owners);
 98        }
 099    }
 100
 101    private void ShowOwnersTooltip()
 102    {
 0103        isPointerInOwnerArea = true;
 104
 0105        if (lastNFTInfo == null || lastNFTInfo.Value.owners.Length <= 1)
 0106            return;
 107
 0108        var tooltip = view.GetOwnersTooltip();
 109
 0110        if (tooltip.IsActive())
 111        {
 0112            tooltip.KeepOnScreen();
 0113            return;
 114        }
 115
 0116        tooltip.SetElements(ownersInfoController.GetElements());
 0117        tooltip.Show();
 0118    }
 119
 120    void TryHideOwnersTooltip()
 121    {
 0122        isPointerInOwnerArea = false;
 123
 0124        if (!isPointerInTooltipArea)
 125        {
 0126            HideOwnersTooltip();
 127        }
 0128    }
 129
 130    void OnOwnersTooltipFocusLost()
 131    {
 0132        isPointerInTooltipArea = false;
 0133        if (!isPointerInOwnerArea)
 134        {
 0135            HideOwnersTooltip();
 136        }
 0137    }
 138
 139    void OnOwnersTooltipFocus()
 140    {
 0141        isPointerInTooltipArea = true;
 0142        var tooltip = view.GetOwnersTooltip();
 0143        if (tooltip.IsActive())
 144        {
 0145            tooltip.KeepOnScreen();
 146        }
 0147    }
 148
 0149    private void HideOwnersTooltip() { HideOwnersTooltip(false); }
 150
 151    private void HideOwnersTooltip(bool instant)
 152    {
 1153        var tooltip = view.GetOwnersTooltip();
 1154        if (!tooltip.IsActive())
 0155            return;
 156
 1157        tooltip.Hide(instant);
 1158    }
 159
 160    private void ShowOwnersPopup()
 161    {
 0162        HideOwnersTooltip(true);
 0163        isPointerInTooltipArea = false;
 0164        isPointerInOwnerArea = false;
 165
 0166        var popup = view.GetOwnersPopup();
 167
 0168        if (popup.IsActive())
 0169            return;
 170
 0171        popup.SetElements(ownersInfoController.GetElements());
 0172        popup.Show();
 0173    }
 174
 0175    private void HideOwnersPopup() { HideOwnersPopup(false); }
 176
 2177    private void HideOwnersPopup(bool instant) { view.GetOwnersPopup().Hide(instant); }
 178
 179    internal static string FormatOwnerAddress(string address, int maxCharacters)
 180    {
 181        const string ellipsis = "...";
 182
 0183        if (address.Length <= maxCharacters)
 184        {
 0185            return address;
 186        }
 187        else
 188        {
 0189            int segmentLength = Mathf.FloorToInt((maxCharacters - ellipsis.Length) * 0.5f);
 0190            return $"{address.Substring(0, segmentLength)}{ellipsis}{address.Substring(address.Length - segmentLength, s
 191        }
 192    }
 193}