< Summary

Class:NFTPromptHUDView
Assembly:NFTPromptHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NFTPromptHUD/NFTPromptHUDView.cs
Covered lines:70
Uncovered lines:120
Coverable lines:190
Total lines:452
Line coverage:36.8% (70 of 190)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Dispose()0%3.143075%
Hide()0%3.033085.71%
GetOwnersPopup()0%110100%
GetOwnersTooltip()0%110100%
SetActive(...)0%110100%
IsActive()0%110100%
GetOwnerElementPrefab()0%110100%
SetLoading()0%22095.65%
SetNFTInfo(...)0%1821300%
Show()0%110100%
FetchNFTImage()0%20400%
UpdateTexture(...)0%2100%
SetNFTImageSize(...)0%6200%
ShortDecimals(...)0%30500%
SetTransparentBackground()0%110100%
SetTokenSymbol(...)0%2100%
OpenMarketUrl()0%6200%
OnError(...)0%2100%
OnDestroy()0%2.012088.89%
OnViewAllOwnersPressed()0%6200%
OnOwnersTooltipGainFocus()0%6200%
OnOwnersTooltipLostFocus()0%6200%
OnOwnersPopupClose()0%6200%
OwnerLabelPointerEnter()0%6200%
OwnerLabelPointerExit()0%6200%
ShowMainLoading(...)0%2.062075%
ShowMainErrorFeedback(...)0%3.333066.67%
ShowImageLoading(...)0%2.062075%
ShowImageErrorFeedback(...)0%12300%

File(s)

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

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.UI;
 4using TMPro;
 5using DCL;
 6using DCL.Helpers;
 7using DCL.Helpers.NFT;
 8using DCL.Interface;
 9using System.Collections;
 10
 11internal interface INFTPromptHUDView : IDisposable
 12{
 13    event Action OnOwnerLabelPointerEnter;
 14    event Action OnOwnerLabelPointerExit;
 15    event Action OnOwnersTooltipFocusLost;
 16    event Action OnOwnersTooltipFocus;
 17    event Action OnViewAllPressed;
 18    event Action OnOwnersPopupClosed;
 19    void SetActive(bool active);
 20    bool IsActive();
 21    IOwnersTooltipView GetOwnersTooltip();
 22    IOwnersPopupView GetOwnersPopup();
 23    OwnerInfoElement GetOwnerElementPrefab();
 24    void SetLoading();
 25    void SetNFTInfo(NFTInfoSingleAsset info, string comment);
 26    void OnError(string error);
 27}
 28
 29internal class NFTPromptHUDView : MonoBehaviour, INFTPromptHUDView
 30{
 31    private const string MULTIPLE_OWNERS_FORMAT = "{0} owners";
 32    private const int ADDRESS_MAX_CHARS = 11;
 33
 34    public event Action OnOwnerLabelPointerEnter;
 35    public event Action OnOwnerLabelPointerExit;
 36    public event Action OnOwnersTooltipFocusLost;
 37    public event Action OnOwnersTooltipFocus;
 38    public event Action OnViewAllPressed;
 39    public event Action OnOwnersPopupClosed;
 40
 41    [SerializeField] internal GameObject content;
 42    [SerializeField] internal GameObject nftContent;
 43    [SerializeField] internal GameObject mainErrorFeedbackContent;
 44    [SerializeField] internal GameObject imageErrorFeedbackContent;
 45
 46    [SerializeField] RawImage imageNft;
 47    [SerializeField] Image imageNftBackground;
 48    [SerializeField] TextMeshProUGUI textNftName;
 49    [SerializeField] TextMeshProUGUI textOwner;
 50    [SerializeField] TextMeshProUGUI textMultipleOwner;
 51    [SerializeField] UIHoverCallback multipleOwnersContainer;
 52
 53    [Header("Last Sale")] [SerializeField] TextMeshProUGUI textLastSaleSymbol;
 54    [SerializeField] TextMeshProUGUI textLastSalePrice;
 55    [SerializeField] TextMeshProUGUI textLastSaleNeverSold;
 56
 57    [Header("Price")] [SerializeField] TextMeshProUGUI textPriceSymbol;
 58    [SerializeField] TextMeshProUGUI textPrice;
 59    [SerializeField] TextMeshProUGUI textPriceNotForSale;
 60
 61    [Header("Description & Comment")]
 62    [SerializeField]
 63    TextMeshProUGUI textDescription;
 64
 65    [SerializeField] TextMeshProUGUI textComment;
 66    [SerializeField] GameObject containerDescription;
 67    [SerializeField] GameObject containerComment;
 68
 69    [Header("Spinners")] [SerializeField] GameObject spinnerGeneral;
 70    [SerializeField] GameObject spinnerNftImage;
 71
 72    [Header("Buttons")] [SerializeField] internal Button buttonClose;
 73    [SerializeField] internal Button buttonCancel;
 74    [SerializeField] internal Button buttonOpenMarket;
 75    [SerializeField] TextMeshProUGUI textOpenMarketButton;
 76
 77    [Header("Owners")]
 78    [SerializeField] internal OwnerInfoElement ownerElementPrefab;
 79    [SerializeField] internal OwnersTooltipView ownersTooltip;
 80    [SerializeField] internal OwnersPopupView ownersPopup;
 81
 82    Coroutine fetchNFTImageRoutine = null;
 83
 84    private string nftTokenId;
 85    bool backgroundColorSet = false;
 86    string marketUrl = null;
 87
 88    private bool isDestroyed = false;
 89    internal INFTAssetLoadHelper nftAssetLoadHelper;
 90
 91    private void Awake()
 92    {
 793        name = "_NFTPromptHUD";
 94
 795        buttonClose.onClick.AddListener(Hide);
 796        buttonCancel.onClick.AddListener(Hide);
 797        buttonOpenMarket.onClick.AddListener(OpenMarketUrl);
 98
 799        multipleOwnersContainer.OnPointerEnter += OwnerLabelPointerEnter;
 7100        multipleOwnersContainer.OnPointerExit += OwnerLabelPointerExit;
 7101        ownersTooltip.OnViewAllPressed += OnViewAllOwnersPressed;
 7102        ownersTooltip.OnFocusLost += OnOwnersTooltipLostFocus;
 7103        ownersTooltip.OnFocus += OnOwnersTooltipGainFocus;
 7104        ownersPopup.OnClosePopup += OnOwnersPopupClose;
 7105    }
 106
 107    public void Dispose()
 108    {
 7109        if (!isDestroyed)
 110        {
 7111            Destroy(gameObject);
 112        }
 113
 7114        nftAssetLoadHelper?.Dispose();
 0115    }
 116
 117    internal void Hide()
 118    {
 1119        content.SetActive(false);
 120
 1121        nftAssetLoadHelper?.Dispose();
 122
 1123        if (fetchNFTImageRoutine != null)
 0124            StopCoroutine(fetchNFTImageRoutine);
 125
 1126        fetchNFTImageRoutine = null;
 127
 1128        AudioScriptableObjects.dialogClose.Play(true);
 1129    }
 130
 1131    IOwnersPopupView INFTPromptHUDView.GetOwnersPopup() { return ownersPopup; }
 132
 1133    IOwnersTooltipView INFTPromptHUDView.GetOwnersTooltip() { return ownersTooltip; }
 134
 16135    void INFTPromptHUDView.SetActive(bool active) { content.SetActive(active); }
 136
 1137    bool INFTPromptHUDView.IsActive() { return content.activeSelf; }
 138
 7139    OwnerInfoElement INFTPromptHUDView.GetOwnerElementPrefab() { return ownerElementPrefab; }
 140
 141    void INFTPromptHUDView.SetLoading()
 142    {
 1143        Show();
 144
 1145        if (fetchNFTImageRoutine != null)
 0146            StopCoroutine(fetchNFTImageRoutine);
 147
 1148        SetTransparentBackground();
 149
 1150        imageNft.gameObject.SetActive(false);
 1151        textNftName.gameObject.SetActive(false);
 1152        textOwner.gameObject.SetActive(false);
 1153        multipleOwnersContainer.gameObject.SetActive(false);
 1154        textLastSaleSymbol.gameObject.SetActive(false);
 1155        textLastSalePrice.gameObject.SetActive(false);
 1156        textLastSaleNeverSold.gameObject.SetActive(false);
 1157        textPriceSymbol.gameObject.SetActive(false);
 1158        textPrice.gameObject.SetActive(false);
 1159        textPriceNotForSale.gameObject.SetActive(false);
 1160        containerDescription.SetActive(false);
 1161        containerComment.SetActive(false);
 1162        buttonCancel.gameObject.SetActive(false);
 1163        buttonOpenMarket.gameObject.SetActive(false);
 164
 1165        nftContent.SetActive(false);
 1166        ShowImageLoading(false);
 1167        ShowMainLoading(true);
 1168        ShowMainErrorFeedback(false);
 1169    }
 170
 171    void INFTPromptHUDView.SetNFTInfo(NFTInfoSingleAsset info, string comment)
 172    {
 0173        Show();
 174
 0175        ShowMainLoading(false);
 0176        nftContent.SetActive(true);
 177
 0178        nftTokenId = info.tokenId;
 0179        SetTransparentBackground();
 0180        backgroundColorSet = info.backgroundColor != null;
 0181        if (backgroundColorSet)
 182        {
 0183            imageNftBackground.color = info.backgroundColor.Value;
 184        }
 185
 0186        textNftName.text = info.name;
 0187        textNftName.gameObject.SetActive(true);
 188
 0189        bool hasMultipleOwners = info.owners.Length > 1;
 0190        if (hasMultipleOwners)
 191        {
 0192            textMultipleOwner.text = string.Format(MULTIPLE_OWNERS_FORMAT, info.owners.Length);
 0193        }
 194        else
 195        {
 0196            textOwner.text = info.owners.Length == 1
 197                ? NFTPromptHUDController.FormatOwnerAddress(info.owners[0].owner, ADDRESS_MAX_CHARS)
 198                : NFTPromptHUDController.FormatOwnerAddress("0x0000000000000000000000000000000000000000", ADDRESS_MAX_CH
 199        }
 0200        textOwner.gameObject.SetActive(!hasMultipleOwners);
 0201        multipleOwnersContainer.gameObject.SetActive(hasMultipleOwners);
 202
 0203        if (!string.IsNullOrEmpty(info.lastSaleAmount))
 204        {
 0205            textLastSalePrice.text = ShortDecimals(info.lastSaleAmount, 4);
 0206            textLastSalePrice.gameObject.SetActive(true);
 0207        }
 208        else
 209        {
 0210            textLastSaleNeverSold.gameObject.SetActive(true);
 211        }
 212
 0213        if (!string.IsNullOrEmpty(info.currentPrice))
 214        {
 0215            textPrice.text = ShortDecimals(info.currentPrice, 4);
 0216            textPrice.gameObject.SetActive(true);
 217
 0218            if (info.currentPriceToken != null)
 219            {
 0220                SetTokenSymbol(textPriceSymbol, info.currentPriceToken.Value.symbol);
 221            }
 0222        }
 223        else
 224        {
 0225            textPriceNotForSale.gameObject.SetActive(true);
 226        }
 227
 0228        if (info.lastSaleToken != null)
 229        {
 0230            SetTokenSymbol(textLastSaleSymbol, info.lastSaleToken.Value.symbol);
 231        }
 232
 0233        if (!string.IsNullOrEmpty(info.description))
 234        {
 0235            textDescription.text = info.description;
 0236            containerDescription.SetActive(true);
 237        }
 238
 0239        if (!string.IsNullOrEmpty(comment))
 240        {
 0241            textComment.text = comment;
 0242            containerComment.SetActive(true);
 243        }
 244
 0245        textOpenMarketButton.text = "VIEW";
 0246        if (info.marketInfo != null)
 247        {
 0248            textOpenMarketButton.text = $"{textOpenMarketButton.text} ON {info.marketInfo.Value.name.ToUpper()}";
 249        }
 250
 0251        marketUrl = null;
 0252        if (!string.IsNullOrEmpty(info.marketLink))
 253        {
 0254            marketUrl = info.marketLink;
 0255        }
 0256        else if (!string.IsNullOrEmpty(info.assetLink))
 257        {
 0258            marketUrl = info.assetLink;
 259        }
 260
 0261        buttonCancel.gameObject.SetActive(true);
 0262        buttonOpenMarket.gameObject.SetActive(true);
 263
 0264        fetchNFTImageRoutine = StartCoroutine(FetchNFTImage(info));
 0265    }
 266
 267    private void Show()
 268    {
 1269        content.SetActive(true);
 1270        Utils.UnlockCursor();
 1271    }
 272
 273    private IEnumerator FetchNFTImage(NFTInfoSingleAsset nftInfo)
 274    {
 0275        ShowImageErrorFeedback(false);
 0276        ShowImageLoading(true);
 277
 0278        if (nftAssetLoadHelper != null)
 0279            nftAssetLoadHelper.Dispose();
 280
 0281        nftAssetLoadHelper = new NFTAssetLoadHelper();
 0282        yield return nftAssetLoadHelper.LoadNFTAsset(
 283            nftInfo.previewImageUrl,
 284            OnSuccess: nftAsset =>
 285            {
 0286                nftAsset.OnTextureUpdate += UpdateTexture;
 287
 0288                if (!(nftAsset is Asset_Gif))
 289                {
 0290                    if (!backgroundColorSet)
 291                    {
 0292                        SetTransparentBackground();
 293                    }
 294                }
 295
 0296                UpdateTexture(nftAsset.previewAsset.texture);
 0297                SetNFTImageSize(nftAsset.previewAsset.texture);
 0298                imageNft.gameObject.SetActive(true);
 0299                ShowImageLoading(false);
 0300            },
 301            OnFail:
 0302            (exc) => { ShowImageErrorFeedback(true); });
 0303    }
 304
 305    private void UpdateTexture(Texture2D texture)
 306    {
 0307        imageNft.texture = texture;
 0308    }
 309
 310    private void SetNFTImageSize(Texture2D texture)
 311    {
 0312        RectTransform rt = (RectTransform)imageNft.transform.parent;
 313
 314        float h, w;
 315
 0316        if (texture.height > texture.width)
 317        {
 0318            h = rt.rect.height;
 0319            w = h * (texture.width / (float)texture.height);
 0320        }
 321        else
 322        {
 0323            w = rt.rect.width;
 0324            h = w * (texture.height / (float)texture.width);
 325        }
 326
 0327        imageNft.rectTransform.sizeDelta = new Vector2(w, h);
 0328    }
 329
 330    private string ShortDecimals(string value, int decimalCount)
 331    {
 0332        int pointPosition = value.IndexOf('.');
 333
 0334        if (pointPosition <= 0)
 0335            return value;
 336
 0337        string ret = value.Substring(0, pointPosition + Mathf.Min(value.Length - pointPosition, decimalCount + 1));
 338
 0339        for (int i = ret.Length - 1; i >= 0; i--)
 340        {
 0341            if (ret[i] == '.')
 342            {
 0343                return ret.Substring(0, i);
 344            }
 345
 0346            if (ret[i] != '0')
 347            {
 0348                return ret.Substring(0, i + 1);
 349            }
 350        }
 351
 0352        return ret;
 353    }
 354
 355    private void SetTransparentBackground()
 356    {
 1357        imageNftBackground.color = new Color(
 358            imageNftBackground.color.r,
 359            imageNftBackground.color.g,
 360            imageNftBackground.color.b,
 361            0f);
 1362    }
 363
 364    private void SetTokenSymbol(TextMeshProUGUI textToken, string symbol)
 365    {
 0366        textToken.text = symbol;
 0367        textToken.gameObject.SetActive(true);
 0368    }
 369
 370    private void OpenMarketUrl()
 371    {
 0372        if (!string.IsNullOrEmpty(marketUrl))
 373        {
 0374            WebInterface.OpenURL(marketUrl);
 0375            AnalyticsHelper.SendExternalLinkAnalytic(marketUrl, nftTokenId);
 0376        }
 377        else
 378        {
 0379            Hide();
 380        }
 0381    }
 382
 383    void INFTPromptHUDView.OnError(string error)
 384    {
 0385        Debug.LogError(error);
 0386        ShowMainErrorFeedback(true);
 0387    }
 388
 389    private void OnDestroy()
 390    {
 7391        isDestroyed = true;
 392
 7393        multipleOwnersContainer.OnPointerEnter -= OwnerLabelPointerEnter;
 7394        multipleOwnersContainer.OnPointerExit -= OwnerLabelPointerExit;
 7395        ownersTooltip.OnViewAllPressed -= OnViewAllOwnersPressed;
 7396        ownersTooltip.OnFocusLost -= OnOwnersTooltipLostFocus;
 7397        ownersTooltip.OnFocus -= OnOwnersTooltipGainFocus;
 7398        ownersPopup.OnClosePopup -= OnOwnersPopupClose;
 399
 7400        nftAssetLoadHelper?.Dispose();
 0401    }
 402
 0403    private void OnViewAllOwnersPressed() { OnViewAllPressed?.Invoke(); }
 404
 0405    private void OnOwnersTooltipGainFocus() { OnOwnersTooltipFocus?.Invoke(); }
 406
 0407    private void OnOwnersTooltipLostFocus() { OnOwnersTooltipFocusLost?.Invoke(); }
 408
 0409    private void OnOwnersPopupClose() { OnOwnersPopupClosed?.Invoke(); }
 410
 0411    private void OwnerLabelPointerEnter() { OnOwnerLabelPointerEnter?.Invoke(); }
 412
 0413    private void OwnerLabelPointerExit() { OnOwnerLabelPointerExit?.Invoke(); }
 414
 415    private void ShowMainLoading(bool isVisible)
 416    {
 1417        if (spinnerGeneral == null)
 0418            return;
 419
 1420        spinnerGeneral.SetActive(isVisible);
 1421    }
 422
 423    private void ShowMainErrorFeedback(bool isVisible)
 424    {
 1425        if (mainErrorFeedbackContent == null)
 0426            return;
 427
 1428        if (isVisible)
 0429            ShowMainLoading(false);
 430
 1431        mainErrorFeedbackContent.SetActive(isVisible);
 1432    }
 433
 434    private void ShowImageLoading(bool isVisible)
 435    {
 1436        if (spinnerNftImage == null)
 0437            return;
 438
 1439        spinnerNftImage.SetActive(isVisible);
 1440    }
 441
 442    private void ShowImageErrorFeedback(bool isVisible)
 443    {
 0444        if (imageErrorFeedbackContent == null)
 0445            return;
 446
 0447        if (isVisible)
 0448            ShowImageLoading(false);
 449
 0450        imageErrorFeedbackContent.SetActive(isVisible);
 0451    }
 452}