< Summary

Class:NFTPromptHUDView
Assembly:NFTPromptHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NFTPromptHUD/NFTPromptHUDView.cs
Covered lines:61
Uncovered lines:119
Coverable lines:180
Total lines:425
Line coverage:33.8% (61 of 180)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Dispose()0%220100%
Hide()0%2.012085.71%
GetOwnersPopup()0%110100%
GetOwnersTooltip()0%110100%
SetActive(...)0%110100%
IsActive()0%110100%
GetOwnerElementPrefab()0%110100%
SetLoading()0%22095.24%
SetNFTInfo(...)0%1821300%
Show()0%110100%
FetchNFTImage()0%1321100%
SetNFTImageSize(...)0%6200%
ShortDecimals(...)0%30500%
SetSmartBackgroundColor(...)0%2100%
SetTokenSymbol(...)0%2100%
OpenMarketUrl()0%6200%
OnError(...)0%2100%
OnDestroy()0%110100%
ForgetPromises()0%3.333066.67%
SetupGifPlayer(...)0%6200%
OnViewAllOwnersPressed()0%6200%
OnOwnersTooltipGainFocus()0%6200%
OnOwnersTooltipLostFocus()0%6200%
OnOwnersPopupClose()0%6200%
OwnerLabelPointerEnter()0%6200%
OwnerLabelPointerExit()0%6200%

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
 43    [SerializeField] RawImage imageNft;
 44    [SerializeField] Image imageNftBackground;
 45    [SerializeField] TextMeshProUGUI textNftName;
 46    [SerializeField] TextMeshProUGUI textOwner;
 47    [SerializeField] TextMeshProUGUI textMultipleOwner;
 48    [SerializeField] UIHoverCallback multipleOwnersContainer;
 49
 50    [Header("Last Sale")] [SerializeField] TextMeshProUGUI textLastSaleSymbol;
 51    [SerializeField] TextMeshProUGUI textLastSalePrice;
 52    [SerializeField] TextMeshProUGUI textLastSaleNeverSold;
 53
 54    [Header("Price")] [SerializeField] TextMeshProUGUI textPriceSymbol;
 55    [SerializeField] TextMeshProUGUI textPrice;
 56    [SerializeField] TextMeshProUGUI textPriceNotForSale;
 57
 58    [Header("Description & Comment")]
 59    [SerializeField]
 60    TextMeshProUGUI textDescription;
 61
 62    [SerializeField] TextMeshProUGUI textComment;
 63    [SerializeField] GameObject containerDescription;
 64    [SerializeField] GameObject containerComment;
 65
 66    [Header("Spinners")] [SerializeField] GameObject spinnerGeneral;
 67    [SerializeField] GameObject spinnerNftImage;
 68
 69    [Header("Buttons")] [SerializeField] internal Button buttonClose;
 70    [SerializeField] internal Button buttonCancel;
 71    [SerializeField] internal Button buttonOpenMarket;
 72    [SerializeField] TextMeshProUGUI textOpenMarketButton;
 73
 74    [Header("Owners")]
 75    [SerializeField] internal OwnerInfoElement ownerElementPrefab;
 76    [SerializeField] internal OwnersTooltipView ownersTooltip;
 77    [SerializeField] internal OwnersPopupView ownersPopup;
 78
 79    Coroutine fetchNFTImageRoutine = null;
 80
 81    private IPromiseLike_TextureAsset imagePromise = null;
 82    private GifPlayer gifPlayer = null;
 83
 84    bool backgroundColorSet = false;
 85    string marketUrl = null;
 86
 87    private bool isDestroyed = false;
 88
 89    private void Awake()
 90    {
 791        name = "_NFTPromptHUD";
 92
 793        buttonClose.onClick.AddListener(Hide);
 794        buttonCancel.onClick.AddListener(Hide);
 795        buttonOpenMarket.onClick.AddListener(OpenMarketUrl);
 96
 797        multipleOwnersContainer.OnPointerEnter += OwnerLabelPointerEnter;
 798        multipleOwnersContainer.OnPointerExit += OwnerLabelPointerExit;
 799        ownersTooltip.OnViewAllPressed += OnViewAllOwnersPressed;
 7100        ownersTooltip.OnFocusLost += OnOwnersTooltipLostFocus;
 7101        ownersTooltip.OnFocus += OnOwnersTooltipGainFocus;
 7102        ownersPopup.OnClosePopup += OnOwnersPopupClose;
 7103    }
 104
 105    public void Dispose()
 106    {
 7107        if (!isDestroyed)
 108        {
 7109            Destroy(gameObject);
 110        }
 7111    }
 112
 113    internal void Hide()
 114    {
 1115        content.SetActive(false);
 116
 1117        ForgetPromises();
 118
 1119        if (fetchNFTImageRoutine != null)
 0120            StopCoroutine(fetchNFTImageRoutine);
 121
 1122        fetchNFTImageRoutine = null;
 123
 1124        AudioScriptableObjects.dialogClose.Play(true);
 1125    }
 126
 1127    IOwnersPopupView INFTPromptHUDView.GetOwnersPopup() { return ownersPopup; }
 128
 1129    IOwnersTooltipView INFTPromptHUDView.GetOwnersTooltip() { return ownersTooltip; }
 130
 16131    void INFTPromptHUDView.SetActive(bool active) { content.SetActive(active); }
 132
 1133    bool INFTPromptHUDView.IsActive() { return content.activeSelf; }
 134
 7135    OwnerInfoElement INFTPromptHUDView.GetOwnerElementPrefab() { return ownerElementPrefab; }
 136
 137    void INFTPromptHUDView.SetLoading()
 138    {
 1139        Show();
 140
 1141        if (fetchNFTImageRoutine != null)
 0142            StopCoroutine(fetchNFTImageRoutine);
 143
 1144        imageNftBackground.color = Color.white;
 145
 1146        imageNft.gameObject.SetActive(false);
 1147        textNftName.gameObject.SetActive(false);
 1148        textOwner.gameObject.SetActive(false);
 1149        multipleOwnersContainer.gameObject.SetActive(false);
 1150        textLastSaleSymbol.gameObject.SetActive(false);
 1151        textLastSalePrice.gameObject.SetActive(false);
 1152        textLastSaleNeverSold.gameObject.SetActive(false);
 1153        textPriceSymbol.gameObject.SetActive(false);
 1154        textPrice.gameObject.SetActive(false);
 1155        textPriceNotForSale.gameObject.SetActive(false);
 1156        containerDescription.SetActive(false);
 1157        containerComment.SetActive(false);
 1158        buttonCancel.gameObject.SetActive(false);
 1159        buttonOpenMarket.gameObject.SetActive(false);
 160
 1161        spinnerGeneral.SetActive(true);
 1162        spinnerNftImage.SetActive(false);
 1163    }
 164
 165    void INFTPromptHUDView.SetNFTInfo(NFTInfoSingleAsset info, string comment)
 166    {
 0167        Show();
 168
 0169        spinnerGeneral.SetActive(false);
 170
 0171        imageNftBackground.color = Color.white;
 0172        backgroundColorSet = info.backgroundColor != null;
 0173        if (backgroundColorSet)
 174        {
 0175            imageNftBackground.color = info.backgroundColor.Value;
 176        }
 177
 0178        textNftName.text = info.name;
 0179        textNftName.gameObject.SetActive(true);
 180
 0181        bool hasMultipleOwners = info.owners.Length > 1;
 0182        if (hasMultipleOwners)
 183        {
 0184            textMultipleOwner.text = string.Format(MULTIPLE_OWNERS_FORMAT, info.owners.Length);
 0185        }
 186        else
 187        {
 0188            textOwner.text = info.owners.Length == 1
 189                ? NFTPromptHUDController.FormatOwnerAddress(info.owners[0].owner, ADDRESS_MAX_CHARS)
 190                : NFTPromptHUDController.FormatOwnerAddress("0x0000000000000000000000000000000000000000", ADDRESS_MAX_CH
 191        }
 0192        textOwner.gameObject.SetActive(!hasMultipleOwners);
 0193        multipleOwnersContainer.gameObject.SetActive(hasMultipleOwners);
 194
 0195        if (!string.IsNullOrEmpty(info.lastSaleAmount))
 196        {
 0197            textLastSalePrice.text = ShortDecimals(info.lastSaleAmount, 4);
 0198            textLastSalePrice.gameObject.SetActive(true);
 0199        }
 200        else
 201        {
 0202            textLastSaleNeverSold.gameObject.SetActive(true);
 203        }
 204
 0205        if (!string.IsNullOrEmpty(info.currentPrice))
 206        {
 0207            textPrice.text = ShortDecimals(info.currentPrice, 4);
 0208            textPrice.gameObject.SetActive(true);
 209
 0210            if (info.currentPriceToken != null)
 211            {
 0212                SetTokenSymbol(textPriceSymbol, info.currentPriceToken.Value.symbol);
 213            }
 0214        }
 215        else
 216        {
 0217            textPriceNotForSale.gameObject.SetActive(true);
 218        }
 219
 0220        if (info.lastSaleToken != null)
 221        {
 0222            SetTokenSymbol(textLastSaleSymbol, info.lastSaleToken.Value.symbol);
 223        }
 224
 0225        if (!string.IsNullOrEmpty(info.description))
 226        {
 0227            textDescription.text = info.description;
 0228            containerDescription.SetActive(true);
 229        }
 230
 0231        if (!string.IsNullOrEmpty(comment))
 232        {
 0233            textComment.text = comment;
 0234            containerComment.SetActive(true);
 235        }
 236
 0237        textOpenMarketButton.text = "VIEW";
 0238        if (info.marketInfo != null)
 239        {
 0240            textOpenMarketButton.text = $"{textOpenMarketButton.text} ON {info.marketInfo.Value.name.ToUpper()}";
 241        }
 242
 0243        marketUrl = null;
 0244        if (!string.IsNullOrEmpty(info.marketLink))
 245        {
 0246            marketUrl = info.marketLink;
 0247        }
 0248        else if (!string.IsNullOrEmpty(info.assetLink))
 249        {
 0250            marketUrl = info.assetLink;
 251        }
 252
 0253        buttonCancel.gameObject.SetActive(true);
 0254        buttonOpenMarket.gameObject.SetActive(true);
 255
 0256        fetchNFTImageRoutine = StartCoroutine(FetchNFTImage(info));
 0257    }
 258
 259    private void Show()
 260    {
 1261        content.SetActive(true);
 1262        Utils.UnlockCursor();
 1263    }
 264
 265    private IEnumerator FetchNFTImage(NFTInfoSingleAsset nftInfo)
 266    {
 0267        spinnerNftImage.SetActive(true);
 268
 0269        bool imageFound = false;
 270
 0271        yield return WrappedTextureUtils.Fetch(nftInfo.previewImageUrl,
 272            promise =>
 273            {
 0274                imagePromise = promise;
 0275                imageFound = true;
 0276            });
 277
 0278        if (!imageFound)
 279        {
 0280            yield return WrappedTextureUtils.Fetch(nftInfo.originalImageUrl,
 281                promise =>
 282                {
 0283                    imagePromise = promise;
 0284                    imageFound = true;
 0285                });
 286        }
 287
 0288        if (imageFound && imagePromise?.asset != null)
 289        {
 0290            Texture2D texture = imagePromise.asset.texture;
 0291            imageNft.texture = texture;
 292
 0293            if ((imagePromise.asset is Asset_Gif gif))
 294            {
 0295                SetupGifPlayer(gif);
 0296            }
 0297            else if (!backgroundColorSet)
 298            {
 0299                SetSmartBackgroundColor(texture);
 300            }
 301
 0302            SetNFTImageSize(texture);
 303
 0304            imageNft.gameObject.SetActive(true);
 0305            spinnerNftImage.SetActive(false);
 306        }
 0307    }
 308
 309    private void SetNFTImageSize(Texture2D texture)
 310    {
 0311        RectTransform rt = (RectTransform)imageNft.transform.parent;
 312        float h, w;
 0313        if (texture.height > texture.width)
 314        {
 0315            h = rt.rect.height;
 0316            w = h * (texture.width / (float)texture.height);
 0317        }
 318        else
 319        {
 0320            w = rt.rect.width;
 0321            h = w * (texture.height / (float)texture.width);
 322        }
 323
 0324        imageNft.rectTransform.sizeDelta = new Vector2(w, h);
 0325    }
 326
 327    private string ShortDecimals(string value, int decimalCount)
 328    {
 0329        int pointPosition = value.IndexOf('.');
 0330        if (pointPosition <= 0)
 0331            return value;
 332
 0333        string ret = value.Substring(0, pointPosition + Mathf.Min(value.Length - pointPosition, decimalCount + 1));
 334
 0335        for (int i = ret.Length - 1; i >= 0; i--)
 336        {
 0337            if (ret[i] == '.')
 338            {
 0339                return ret.Substring(0, i);
 340            }
 341
 0342            if (ret[i] != '0')
 343            {
 0344                return ret.Substring(0, i + 1);
 345            }
 346        }
 347
 0348        return ret;
 349    }
 350
 0351    private void SetSmartBackgroundColor(Texture2D texture) { imageNftBackground.color = texture.GetPixel(0, 0); }
 352
 353    private void SetTokenSymbol(TextMeshProUGUI textToken, string symbol)
 354    {
 0355        textToken.text = symbol;
 0356        textToken.gameObject.SetActive(true);
 0357    }
 358
 359    private void OpenMarketUrl()
 360    {
 0361        if (!string.IsNullOrEmpty(marketUrl))
 362        {
 0363            WebInterface.OpenURL(marketUrl);
 0364        }
 365        else
 366        {
 0367            Hide();
 368        }
 0369    }
 370
 371    void INFTPromptHUDView.OnError(string error)
 372    {
 0373        Debug.LogError(error);
 0374        Hide();
 0375        Utils.LockCursor();
 0376    }
 377
 378    private void OnDestroy()
 379    {
 7380        isDestroyed = true;
 381
 7382        multipleOwnersContainer.OnPointerEnter -= OwnerLabelPointerEnter;
 7383        multipleOwnersContainer.OnPointerExit -= OwnerLabelPointerExit;
 7384        ownersTooltip.OnViewAllPressed -= OnViewAllOwnersPressed;
 7385        ownersTooltip.OnFocusLost -= OnOwnersTooltipLostFocus;
 7386        ownersTooltip.OnFocus -= OnOwnersTooltipGainFocus;
 7387        ownersPopup.OnClosePopup -= OnOwnersPopupClose;
 388
 7389        ForgetPromises();
 7390    }
 391
 392    private void ForgetPromises()
 393    {
 8394        if (imagePromise != null)
 395        {
 0396            imagePromise.Forget();
 0397            imagePromise = null;
 398        }
 8399        gifPlayer?.Dispose();
 8400        gifPlayer = null;
 8401    }
 402
 403    private void SetupGifPlayer(Asset_Gif gif)
 404    {
 0405        if (gifPlayer == null)
 406        {
 0407            gifPlayer = new GifPlayer();
 0408            gifPlayer.OnFrameTextureChanged += (texture) => { imageNft.texture = texture; };
 409        }
 0410        gifPlayer.SetGif(gif);
 0411        gifPlayer.Play(true);
 0412    }
 413
 0414    private void OnViewAllOwnersPressed() { OnViewAllPressed?.Invoke(); }
 415
 0416    private void OnOwnersTooltipGainFocus() { OnOwnersTooltipFocus?.Invoke(); }
 417
 0418    private void OnOwnersTooltipLostFocus() { OnOwnersTooltipFocusLost?.Invoke(); }
 419
 0420    private void OnOwnersPopupClose() { OnOwnersPopupClosed?.Invoke(); }
 421
 0422    private void OwnerLabelPointerEnter() { OnOwnerLabelPointerEnter?.Invoke(); }
 423
 0424    private void OwnerLabelPointerExit() { OnOwnerLabelPointerExit?.Invoke(); }
 425}