< Summary

Class:CatalogItemAdapter
Assembly:BuilderInWorldCatalog
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Catalog/UI/Adapters/CatalogItemAdapter.cs
Covered lines:37
Uncovered lines:35
Coverable lines:72
Total lines:169
Line coverage:51.3% (37 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetContent()0%2100%
SetContent(...)0%9.499081.82%
GetThumbnail()0%4.014091.67%
ClearThumbnailPromise()0%220100%
SetLoadingActive(...)0%2.52050%
EnableDragMode(...)0%3.013088.89%
SetFavorite(...)0%6200%
AdapterStartDragging(...)0%220100%
FavoriteIconClicked()0%6200%
SceneObjectClicked()0%12300%
SetThumbnail(...)0%20400%
OnBeginDrag(...)0%2100%
OnDrag(...)0%6200%
OnEndDrag(...)0%6200%
OnPointerEnter(...)0%6200%
OnPointerExit(...)0%6200%
ItemAdapterIsOnScreen()0%6200%
OnDestroy()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Catalog/UI/Adapters/CatalogItemAdapter.cs

#LineLine coverage
 1using DCL;
 2using UnityEngine;
 3using UnityEngine.EventSystems;
 4using UnityEngine.UI;
 5
 6public class CatalogItemAdapter : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerEnterHandler,
 7{
 8    public RawImage thumbnailImg;
 9    public Image backgroundImg;
 10    public CanvasGroup canvasGroup;
 11    public GameObject lockedGO;
 12    public GameObject loadingGO;
 13
 14    [Header("Smart Items")]
 15    public GameObject smartItemGO;
 16
 17    public Color smartItemColor;
 18    public Color normalColor;
 19
 20    [Header("Favorites")]
 21    public Color offFavoriteColor;
 22
 23    public Color onFavoriteColor;
 24    public Image favImg;
 25
 26    public System.Action<CatalogItem> OnCatalogItemClicked;
 27    public System.Action<CatalogItem, CatalogItemAdapter> OnCatalogItemFavorite;
 28    public System.Action<CatalogItem, CatalogItemAdapter, BaseEventData> OnAdapterStartDrag;
 29    public System.Action<PointerEventData> OnAdapterDrag, OnAdapterEndDrag;
 30    public System.Action<PointerEventData, CatalogItemAdapter> OnPointerEnterInAdapter;
 31    public System.Action<PointerEventData, CatalogItemAdapter> OnPointerExitInAdapter;
 32
 33    private CatalogItem catalogItem;
 34    private string loadedThumbnailURL;
 35    private AssetPromise_Texture loadedThumbnailPromise;
 36
 37    private const float ADAPTER_DRAGGING_SIZE_SCALE = 0.75f;
 38
 039    public CatalogItem GetContent() { return catalogItem; }
 40
 41    public void SetContent(CatalogItem catalogItem)
 42    {
 943        this.catalogItem = catalogItem;
 44
 945        if (favImg != null)
 946            favImg.color = catalogItem.IsFavorite() ? onFavoriteColor : offFavoriteColor;
 47
 948        if (backgroundImg != null)
 049            backgroundImg.color = catalogItem.IsSmartItem() ? smartItemColor : normalColor;
 950        smartItemGO.SetActive(catalogItem.IsSmartItem());
 51
 952        GetThumbnail();
 53
 954        lockedGO.gameObject.SetActive(false);
 55
 956        if (catalogItem.IsNFT() && BIWNFTController.i.IsNFTInUse(catalogItem.id))
 057            lockedGO.gameObject.SetActive(true);
 958    }
 59
 60    private void GetThumbnail()
 61    {
 962        SetLoadingActive(true);
 63
 964        if (catalogItem == null)
 065            return;
 66
 967        var url = catalogItem.GetThumbnailUrl();
 68
 969        if (string.IsNullOrEmpty(url) || url == loadedThumbnailURL)
 870            return;
 71
 172        ClearThumbnailPromise();
 173        loadedThumbnailPromise = new AssetPromise_Texture(url);
 74
 175        loadedThumbnailPromise.OnSuccessEvent += x =>
 76        {
 077            loadedThumbnailURL = url;
 078            SetThumbnail(x);
 079        };
 80
 181        loadedThumbnailPromise.OnFailEvent += (x, error) =>
 82        {
 083            Debug.Log($"Error downloading: {url}, Exception: {error}");
 084            SetLoadingActive(false);
 085        };
 86
 187        AssetPromiseKeeper_Texture.i.Keep(loadedThumbnailPromise);
 188    }
 89
 90    private void ClearThumbnailPromise()
 91    {
 692        if (loadedThumbnailPromise != null)
 93        {
 194            loadedThumbnailPromise.ClearEvents();
 195            AssetPromiseKeeper_Texture.i.Forget(loadedThumbnailPromise);
 196            loadedThumbnailPromise = null;
 97        }
 698    }
 99
 100    private void SetLoadingActive(bool isActive)
 101    {
 9102        if (loadingGO == null)
 9103            return;
 104
 0105        loadingGO.SetActive(isActive);
 0106    }
 107
 108    public void EnableDragMode(Vector2 sizeDelta)
 109    {
 4110        RectTransform newAdapterRT = GetComponent<RectTransform>();
 4111        canvasGroup.blocksRaycasts = false;
 4112        canvasGroup.alpha = 0.6f;
 113
 4114        if (favImg != null)
 4115            favImg.enabled = false;
 116
 4117        if (backgroundImg != null)
 0118            backgroundImg.enabled = false;
 119
 4120        newAdapterRT.sizeDelta = sizeDelta * ADAPTER_DRAGGING_SIZE_SCALE;
 4121    }
 122
 123    public void SetFavorite(bool isOn)
 124    {
 0125        if (isOn)
 0126            favImg.color = onFavoriteColor;
 127        else
 0128            favImg.color = offFavoriteColor;
 0129    }
 130
 18131    public void AdapterStartDragging(BaseEventData baseEventData) { OnAdapterStartDrag?.Invoke(catalogItem, this, baseEv
 132
 0133    public void FavoriteIconClicked() { OnCatalogItemFavorite?.Invoke(catalogItem, this); }
 134
 135    public void SceneObjectClicked()
 136    {
 0137        if (!lockedGO.gameObject.activeSelf)
 0138            OnCatalogItemClicked?.Invoke(catalogItem);
 0139    }
 140
 141    public void SetThumbnail(Asset_Texture texture)
 142    {
 0143        if (thumbnailImg != null)
 144        {
 0145            thumbnailImg.enabled = true;
 0146            thumbnailImg.texture = texture.texture;
 0147            favImg.gameObject.SetActive(true);
 148
 0149            if (gameObject.activeInHierarchy && ItemAdapterIsOnScreen())
 0150                AudioScriptableObjects.listItemAppear.Play();
 151        }
 152
 0153        SetLoadingActive(false);
 0154    }
 155
 0156    public void OnBeginDrag(PointerEventData eventData) { AdapterStartDragging(eventData); }
 157
 0158    public void OnDrag(PointerEventData eventData) { OnAdapterDrag?.Invoke(eventData); }
 159
 0160    public void OnEndDrag(PointerEventData eventData) { OnAdapterEndDrag?.Invoke(eventData); }
 161
 0162    public void OnPointerEnter(PointerEventData eventData) { OnPointerEnterInAdapter?.Invoke(eventData, this); }
 163
 0164    public void OnPointerExit(PointerEventData eventData) { OnPointerExitInAdapter?.Invoke(eventData, this); }
 165
 0166    private bool ItemAdapterIsOnScreen() { return (transform.position.y > 0 && transform.position.y < Screen.height); }
 167
 10168    private void OnDestroy() { ClearThumbnailPromise(); }
 169}