< 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:38
Uncovered lines:27
Coverable lines:65
Total lines:153
Line coverage:58.4% (38 of 65)
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.022083.33%
OnClick()0%6200%
Initialize(...)0%220100%
OnPointerEnter(...)0%30500%
OnPointerExit(...)0%2100%
OnThumbnailReady(...)0%20400%
OnEnable()0%110100%
OnDestroy()0%110100%
CallOnSellClicked()0%6200%
GetThumbnail()0%6.176083.33%
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 loadingSpinner;
 23    [SerializeField] internal RectTransform amountContainer;
 24    [SerializeField] internal Animator loadingAnimator;
 25    [SerializeField] internal TextMeshProUGUI amountText;
 26
 27    private bool selectedValue;
 28
 29    private string loadedThumbnailURL;
 30    private AssetPromise_Texture loadedThumbnailPromise;
 31
 32    private AvatarEditorHUDView view;
 33
 34    //Todo change this for a confirmation popup or implement it in a more elegant way
 35    public static Func<WearableItem, List<WearableItem>> getEquippedWearablesReplacedByFunc;
 36
 37    public bool selected
 38    {
 039        get { return selectedValue; }
 40        set
 41        {
 791542            selectedValue = value;
 791543            SetSelection(selectedValue);
 791544        }
 45    }
 46
 47    protected virtual void SetSelection(bool isSelected)
 48    {
 791549        if (selectionHighlight != null)
 791550            selectionHighlight.enabled = isSelected;
 791551    }
 52
 271853    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 54
 55    protected new virtual void Awake()
 56    {
 27657        base.Awake();
 27658        thumbnail.sprite = null;
 27659        warningPanel.SetActive(false);
 60
 27661        if (!EnvironmentSettings.RUNNING_TESTS)
 062            view = GetComponentInParent<AvatarEditorHUDView>();
 27663    }
 64
 65    protected override void OnClick()
 66    {
 067        OnClicked?.Invoke(this);
 068        warningPanel.SetActive(false);
 069    }
 70
 71    public virtual void Initialize(WearableItem w, bool isSelected, int amount)
 72    {
 389673        wearableItem = w;
 389674        selected = isSelected;
 389675        amountContainer.gameObject.SetActive(amount > 1);
 389676        amountText.text = $"x{amount.ToString()}";
 77
 389678        if (gameObject.activeInHierarchy)
 27679            GetThumbnail();
 389680    }
 81
 82    public void OnPointerEnter(PointerEventData eventData)
 83    {
 084        List<WearableItem> toReplace = getEquippedWearablesReplacedByFunc(wearableItem);
 085        if (wearableItem == null || toReplace.Count == 0)
 086            return;
 087        if (toReplace.Count == 1)
 88        {
 089            WearableItem w = toReplace[0];
 090            if (w.data.category == wearableItem.data.category)
 091                return;
 92        }
 93
 094        warningPanel.SetActive(true);
 095    }
 96
 097    public void OnPointerExit(PointerEventData eventData) { warningPanel.SetActive(false); }
 98
 99    private void OnThumbnailReady(Asset_Texture texture)
 100    {
 0101        loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 102
 0103        if (thumbnail.sprite != null)
 0104            Destroy(thumbnail.sprite);
 105
 0106        thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture);
 107
 0108        if (view != null)
 109        {
 0110            if (view.avatarEditorCanvas.enabled)
 0111                AudioScriptableObjects.listItemAppear.Play(true);
 112        }
 0113    }
 114
 552115    private void OnEnable() { GetThumbnail(); }
 116
 117    protected virtual void OnDestroy()
 118    {
 276119        ForgetThumbnail();
 276120        OnClicked = null;
 276121    }
 122
 0123    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 124
 125    private void GetThumbnail()
 126    {
 552127        string url = wearableItem?.ComposeThumbnailUrl();
 128
 552129        if (url == loadedThumbnailURL)
 130        {
 276131            loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 276132            return;
 133        }
 134
 276135        if (wearableItem == null || string.IsNullOrEmpty(url))
 136        {
 0137            loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 0138            return;
 139        }
 140
 276141        loadedThumbnailURL = url;
 276142        var newLoadedThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 276143        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 276144        loadedThumbnailPromise = newLoadedThumbnailPromise;
 276145    }
 146
 147    private void ForgetThumbnail()
 148    {
 276149        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 276150        loadedThumbnailURL = null;
 276151        loadedThumbnailPromise = null;
 276152    }
 153}