< Summary

Class:ItemToggle
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ItemToggle.cs
Covered lines:37
Uncovered lines:55
Coverable lines:92
Total lines:216
Line coverage:40.2% (37 of 92)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ItemToggle()0%110100%
SetSelection(...)0%220100%
SetLoadingSpinner(...)0%220100%
Awake()0%2.012087.5%
OnClick()0%6200%
Initialize(...)0%220100%
OnPointerEnter(...)0%20400%
IsIncompatible()0%6200%
IsReplacingWearables()0%6200%
IsHidingWearables()0%6200%
OnPointerExit(...)0%2100%
SetHideOtherWerablesToastStrategy(...)0%2100%
SetBodyShapeCompatibilityStrategy(...)0%2100%
SetReplaceOtherWearablesToastStrategy(...)0%2100%
OnEnable()0%110100%
OnDestroy()0%2100%
CallOnSellClicked()0%6200%
SetColorScale()0%12300%
GetThumbnail()0%550100%
OnThumbnailReady(...)0%12300%
SetLoadingAnimationTrigger(...)0%330100%
Hide()0%110100%
SetCallbacks(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ItemToggle.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Configuration;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7using UnityEngine.UI;
 8
 9public class ItemToggle : UIButton, IPointerEnterHandler, IPointerExitHandler
 10{
 111    private static readonly int LOADING_ANIMATOR_TRIGGER_LOADED = Animator.StringToHash("Loaded");
 12
 13    public event Action<ItemToggle> OnClicked;
 14    public event Action<ItemToggle> OnSellClicked;
 15
 016    public WearableItem wearableItem { get; private set; }
 17
 18    public Image thumbnail;
 19    public Image selectionHighlight;
 20    [SerializeField] private GameObject warningPanel;
 21    [SerializeField] private GameObject incompatibleWarningPanel;
 22    [SerializeField] private GameObject hideWarningPanel;
 23    [SerializeField] private GameObject loadingSpinner;
 24    [SerializeField] internal RectTransform amountContainer;
 25    [SerializeField] internal Animator loadingAnimator;
 26    [SerializeField] internal TextMeshProUGUI amountText;
 27    [SerializeField] internal GameObject root;
 28    [SerializeField] private GameObject disabledOverlay;
 29    [SerializeField] internal Material grayScaleMaterial;
 30    [SerializeField] internal Button selectButton;
 31
 32    private bool selectedValue;
 33
 34    private AvatarEditorHUDView view;
 35
 36    private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc;
 37    private Func<WearableItem, bool> getEquippedWearablesHiddenBy;
 38    private Func<WearableItem, bool> getBodyShapeCompatibility;
 39
 040    public string collectionId { get; set; }
 41
 42    public bool selected
 43    {
 044        get { return selectedValue; }
 45        set
 46        {
 1132747            selectedValue = value;
 1132748            SetSelection(selectedValue);
 1132749        }
 50    }
 51
 52    protected virtual void SetSelection(bool isSelected)
 53    {
 1132754        if (selectionHighlight != null)
 1132755            selectionHighlight.enabled = isSelected;
 1132756    }
 57
 1600258    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 59
 60    protected new virtual void Awake()
 61    {
 84662        base.Awake();
 84663        thumbnail.sprite = null;
 84664        warningPanel.SetActive(false);
 84665        hideWarningPanel.SetActive(false);
 84666        incompatibleWarningPanel.SetActive(false);
 84667        if (!EnvironmentSettings.RUNNING_TESTS)
 068            view = GetComponentInParent<AvatarEditorHUDView>();
 84669    }
 70
 71    protected override void OnClick()
 72    {
 073        OnClicked?.Invoke(this);
 074        warningPanel.SetActive(false);
 075    }
 76
 77    public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin)
 78    {
 644779        root.gameObject.SetActive(true);
 80
 644781        wearableItem = w;
 644782        selected = isSelected;
 644783        amountContainer.gameObject.SetActive(amount > 1);
 644784        amountText.text = $"x{amount.ToString()}";
 85
 644786        if (gameObject.activeInHierarchy)
 6687            GetThumbnail();
 644788    }
 89
 90    public void OnPointerEnter(PointerEventData eventData)
 91    {
 092        incompatibleWarningPanel.SetActive(false);
 093        warningPanel.SetActive(false);
 094        hideWarningPanel.SetActive(false);
 095        if(IsIncompatible())
 96        {
 097            incompatibleWarningPanel.SetActive(true);
 098        }
 099        else if(IsReplacingWearables())
 100        {
 0101            warningPanel.SetActive(true);
 0102        }
 0103        else if(IsHidingWearables())
 104        {
 0105            hideWarningPanel.SetActive(true);
 106        }
 0107    }
 108
 109    private bool IsIncompatible()
 110    {
 0111        if(getBodyShapeCompatibility == null) return false;
 0112        return getBodyShapeCompatibility(wearableItem);
 113    }
 114
 115    private bool IsReplacingWearables()
 116    {
 0117        if (getEquippedWearablesReplacedByFunc == null) return false;
 0118        return getEquippedWearablesReplacedByFunc(wearableItem);
 119    }
 120
 121    private bool IsHidingWearables()
 122    {
 0123        if (getEquippedWearablesHiddenBy == null) return false;
 0124        return getEquippedWearablesHiddenBy(wearableItem);
 125    }
 126
 127    public void OnPointerExit(PointerEventData eventData)
 128    {
 0129        warningPanel.SetActive(false);
 0130        hideWarningPanel.SetActive(false);
 0131        incompatibleWarningPanel.SetActive(false);
 0132    }
 133
 134    public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) =>
 0135        getEquippedWearablesHiddenBy = function;
 136
 137    public void SetBodyShapeCompatibilityStrategy(Func<WearableItem, bool> function) =>
 0138        getBodyShapeCompatibility = function;
 139
 140    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 0141        getEquippedWearablesReplacedByFunc = function;
 142
 143    private void OnEnable()
 144    {
 846145        GetThumbnail();
 846146    }
 147
 148    protected virtual void OnDestroy()
 149    {
 0150        OnClicked = null;
 0151    }
 152
 0153    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 154
 155    private void SetColorScale()
 156    {
 0157        if(getBodyShapeCompatibility != null && getBodyShapeCompatibility(wearableItem))
 158        {
 0159            thumbnail.material = grayScaleMaterial;
 0160            thumbnail.SetMaterialDirty();
 0161            disabledOverlay.SetActive(true);
 0162            selectButton.interactable = false;
 0163        }
 164        else
 165        {
 0166            thumbnail.material = null;
 0167            disabledOverlay.SetActive(false);
 0168            selectButton.interactable = true;
 169        }
 0170    }
 171
 172    private void GetThumbnail()
 173    {
 912174        string url = wearableItem?.ComposeThumbnailUrl();
 175
 912176        if (wearableItem == null || string.IsNullOrEmpty(url))
 177        {
 780178            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 780179            return;
 180        }
 181
 132182        ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 132183    }
 184
 185    private void OnThumbnailReady(Asset_Texture texture)
 186    {
 0187        SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 188
 0189        thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated);
 190
 0191        if (view != null && wasCreated)
 192        {
 0193            if (view.avatarEditorCanvas.enabled)
 0194                AudioScriptableObjects.listItemAppear.Play(true);
 195        }
 0196        SetColorScale();
 0197    }
 198
 199    private void SetLoadingAnimationTrigger(int id)
 200    {
 780201        if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null)
 752202            return;
 203
 28204        loadingAnimator.SetTrigger(id);
 28205    }
 206
 207    public void Hide()
 208    {
 58209209        root.gameObject.SetActive(false);
 58209210    }
 211    public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked)
 212    {
 0213        OnClicked = toggleClicked;
 0214        OnSellClicked = sellClicked;
 0215    }
 216}