< 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:44
Uncovered lines:33
Coverable lines:77
Total lines:182
Line coverage:57.1% (44 of 77)
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.012085.71%
OnClick()0%6200%
Initialize(...)0%220100%
OnPointerEnter(...)0%6200%
ShowReplacementWarningPanel()0%6200%
ShowHidingWarningPanel()0%6200%
OnPointerExit(...)0%2100%
SetHideOtherWerablesToastStrategy(...)0%2100%
SetReplaceOtherWearablesToastStrategy(...)0%2100%
OnThumbnailReady(...)0%20400%
OnEnable()0%110100%
OnDestroy()0%110100%
CallOnSellClicked()0%6200%
GetThumbnail()0%660100%
SetLoadingAnimationTrigger(...)0%3.143075%
ForgetThumbnail()0%110100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL;
 4using DCL.Configuration;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8using UnityEngine.UI;
 9
 10public class ItemToggle : UIButton, IPointerEnterHandler, IPointerExitHandler
 11{
 112    private static readonly int LOADING_ANIMATOR_TRIGGER_LOADED = Animator.StringToHash("Loaded");
 13
 14    public event System.Action<ItemToggle> OnClicked;
 15    public event System.Action<ItemToggle> OnSellClicked;
 16
 017    public WearableItem wearableItem { get; private set; }
 18
 19    public Image thumbnail;
 20    public Image selectionHighlight;
 21    [SerializeField] private GameObject warningPanel;
 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
 28    private bool selectedValue;
 29
 30    private string loadedThumbnailURL;
 31    private AssetPromise_Texture loadedThumbnailPromise;
 32
 33    private AvatarEditorHUDView view;
 34
 35    private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc;
 36    private Func<WearableItem, bool> getEquippedWearablesHiddenBy;
 37
 038    public string collectionId { get; set; }
 39
 40    public bool selected
 41    {
 042        get { return selectedValue; }
 43        set
 44        {
 861645            selectedValue = value;
 861646            SetSelection(selectedValue);
 861647        }
 48    }
 49
 50    protected virtual void SetSelection(bool isSelected)
 51    {
 861652        if (selectionHighlight != null)
 861653            selectionHighlight.enabled = isSelected;
 861654    }
 55
 298256    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 57
 58    protected new virtual void Awake()
 59    {
 11560        base.Awake();
 11561        thumbnail.sprite = null;
 11562        warningPanel.SetActive(false);
 11563        hideWarningPanel.SetActive(false);
 64
 11565        if (!EnvironmentSettings.RUNNING_TESTS)
 066            view = GetComponentInParent<AvatarEditorHUDView>();
 11567    }
 68
 69    protected override void OnClick()
 70    {
 071        OnClicked?.Invoke(this);
 072        warningPanel.SetActive(false);
 073    }
 74
 75    public virtual void Initialize(WearableItem w, bool isSelected, int amount)
 76    {
 424277        wearableItem = w;
 424278        selected = isSelected;
 424279        amountContainer.gameObject.SetActive(amount > 1);
 424280        amountText.text = $"x{amount.ToString()}";
 81
 424282        if (gameObject.activeInHierarchy)
 2183            GetThumbnail();
 424284    }
 85
 86    public void OnPointerEnter(PointerEventData eventData)
 87    {
 088        if (!ShowReplacementWarningPanel())
 089            ShowHidingWarningPanel();
 090    }
 91
 92    private bool ShowReplacementWarningPanel()
 93    {
 094        if (getEquippedWearablesReplacedByFunc == null) return false;
 095        var shouldShow = getEquippedWearablesReplacedByFunc(wearableItem);
 096        warningPanel.SetActive(shouldShow);
 097        return shouldShow;
 98    }
 99
 100    private bool ShowHidingWarningPanel()
 101    {
 0102        if (getEquippedWearablesHiddenBy == null) return false;
 0103        var shouldShow = getEquippedWearablesHiddenBy(wearableItem);
 0104        hideWarningPanel.SetActive(shouldShow);
 0105        return shouldShow;
 106    }
 107
 108    public void OnPointerExit(PointerEventData eventData)
 109    {
 0110        warningPanel.SetActive(false);
 0111        hideWarningPanel.SetActive(false);
 0112    }
 113
 114    public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) =>
 0115        getEquippedWearablesHiddenBy = function;
 116
 117    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 0118        getEquippedWearablesReplacedByFunc = function;
 119
 120    private void OnThumbnailReady(Asset_Texture texture)
 121    {
 0122        SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 123
 0124        if (thumbnail.sprite != null)
 0125            Destroy(thumbnail.sprite);
 126
 0127        thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture);
 128
 0129        if (view != null)
 130        {
 0131            if (view.avatarEditorCanvas.enabled)
 0132                AudioScriptableObjects.listItemAppear.Play(true);
 133        }
 0134    }
 135
 230136    private void OnEnable() { GetThumbnail(); }
 137
 138    protected virtual void OnDestroy()
 139    {
 115140        ForgetThumbnail();
 115141        OnClicked = null;
 115142    }
 143
 0144    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 145
 146    private void GetThumbnail()
 147    {
 136148        string url = wearableItem?.ComposeThumbnailUrl();
 149
 136150        if (url == loadedThumbnailURL)
 151        {
 21152            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 21153            return;
 154        }
 155
 115156        if (wearableItem == null || string.IsNullOrEmpty(url))
 157        {
 11158            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 11159            return;
 160        }
 161
 104162        loadedThumbnailURL = url;
 104163        var newLoadedThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 104164        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 104165        loadedThumbnailPromise = newLoadedThumbnailPromise;
 104166    }
 167
 168    private void SetLoadingAnimationTrigger(int id)
 169    {
 32170        if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null)
 0171            return;
 172
 32173        loadingAnimator.SetTrigger(id);
 32174    }
 175
 176    private void ForgetThumbnail()
 177    {
 115178        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 115179        loadedThumbnailURL = null;
 115180        loadedThumbnailPromise = null;
 115181    }
 182}