< 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:32
Coverable lines:76
Total lines:180
Line coverage:57.8% (44 of 76)
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
 38    public bool selected
 39    {
 040        get { return selectedValue; }
 41        set
 42        {
 825943            selectedValue = value;
 825944            SetSelection(selectedValue);
 825945        }
 46    }
 47
 48    protected virtual void SetSelection(bool isSelected)
 49    {
 825950        if (selectionHighlight != null)
 825951            selectionHighlight.enabled = isSelected;
 825952    }
 53
 371854    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 55
 56    protected new virtual void Awake()
 57    {
 19858        base.Awake();
 19859        thumbnail.sprite = null;
 19860        warningPanel.SetActive(false);
 19861        hideWarningPanel.SetActive(false);
 62
 19863        if (!EnvironmentSettings.RUNNING_TESTS)
 064            view = GetComponentInParent<AvatarEditorHUDView>();
 19865    }
 66
 67    protected override void OnClick()
 68    {
 069        OnClicked?.Invoke(this);
 070        warningPanel.SetActive(false);
 071    }
 72
 73    public virtual void Initialize(WearableItem w, bool isSelected, int amount)
 74    {
 406875        wearableItem = w;
 406876        selected = isSelected;
 406877        amountContainer.gameObject.SetActive(amount > 1);
 406878        amountText.text = $"x{amount.ToString()}";
 79
 406880        if (gameObject.activeInHierarchy)
 10881            GetThumbnail();
 406882    }
 83
 84    public void OnPointerEnter(PointerEventData eventData)
 85    {
 086        if (!ShowReplacementWarningPanel())
 087            ShowHidingWarningPanel();
 088    }
 89
 90    private bool ShowReplacementWarningPanel()
 91    {
 092        if (getEquippedWearablesReplacedByFunc == null) return false;
 093        var shouldShow = getEquippedWearablesReplacedByFunc(wearableItem);
 094        warningPanel.SetActive(shouldShow);
 095        return shouldShow;
 96    }
 97
 98    private bool ShowHidingWarningPanel()
 99    {
 0100        if (getEquippedWearablesHiddenBy == null) return false;
 0101        var shouldShow = getEquippedWearablesHiddenBy(wearableItem);
 0102        hideWarningPanel.SetActive(shouldShow);
 0103        return shouldShow;
 104    }
 105
 106    public void OnPointerExit(PointerEventData eventData)
 107    {
 0108        warningPanel.SetActive(false);
 0109        hideWarningPanel.SetActive(false);
 0110    }
 111
 112    public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) =>
 0113        getEquippedWearablesHiddenBy = function;
 114
 115    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 0116        getEquippedWearablesReplacedByFunc = function;
 117
 118    private void OnThumbnailReady(Asset_Texture texture)
 119    {
 0120        SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 121
 0122        if (thumbnail.sprite != null)
 0123            Destroy(thumbnail.sprite);
 124
 0125        thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture);
 126
 0127        if (view != null)
 128        {
 0129            if (view.avatarEditorCanvas.enabled)
 0130                AudioScriptableObjects.listItemAppear.Play(true);
 131        }
 0132    }
 133
 396134    private void OnEnable() { GetThumbnail(); }
 135
 136    protected virtual void OnDestroy()
 137    {
 198138        ForgetThumbnail();
 198139        OnClicked = null;
 198140    }
 141
 0142    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 143
 144    private void GetThumbnail()
 145    {
 306146        string url = wearableItem?.ComposeThumbnailUrl();
 147
 306148        if (url == loadedThumbnailURL)
 149        {
 108150            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 108151            return;
 152        }
 153
 198154        if (wearableItem == null || string.IsNullOrEmpty(url))
 155        {
 8156            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 8157            return;
 158        }
 159
 190160        loadedThumbnailURL = url;
 190161        var newLoadedThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 190162        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 190163        loadedThumbnailPromise = newLoadedThumbnailPromise;
 190164    }
 165
 166    private void SetLoadingAnimationTrigger(int id)
 167    {
 116168        if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null)
 0169            return;
 170
 116171        loadingAnimator.SetTrigger(id);
 116172    }
 173
 174    private void ForgetThumbnail()
 175    {
 198176        ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise);
 198177        loadedThumbnailURL = null;
 198178        loadedThumbnailPromise = null;
 198179    }
 180}