< 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:35
Uncovered lines:35
Coverable lines:70
Total lines:171
Line coverage:50% (35 of 70)
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%
OnEnable()0%110100%
OnDestroy()0%2100%
CallOnSellClicked()0%6200%
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 hideWarningPanel;
 22    [SerializeField] private GameObject loadingSpinner;
 23    [SerializeField] internal RectTransform amountContainer;
 24    [SerializeField] internal Animator loadingAnimator;
 25    [SerializeField] internal TextMeshProUGUI amountText;
 26    [SerializeField] internal GameObject root;
 27
 28    private bool selectedValue;
 29
 30    private AvatarEditorHUDView view;
 31
 32    private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc;
 33    private Func<WearableItem, bool> getEquippedWearablesHiddenBy;
 34
 035    public string collectionId { get; set; }
 36
 37    public bool selected
 38    {
 039        get { return selectedValue; }
 40        set
 41        {
 676242            selectedValue = value;
 676243            SetSelection(selectedValue);
 676244        }
 45    }
 46
 47    protected virtual void SetSelection(bool isSelected)
 48    {
 676249        if (selectionHighlight != null)
 676250            selectionHighlight.enabled = isSelected;
 676251    }
 52
 870653    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 54
 55    protected new virtual void Awake()
 56    {
 84657        base.Awake();
 84658        thumbnail.sprite = null;
 84659        warningPanel.SetActive(false);
 84660        hideWarningPanel.SetActive(false);
 61
 84662        if (!EnvironmentSettings.RUNNING_TESTS)
 063            view = GetComponentInParent<AvatarEditorHUDView>();
 84664    }
 65
 66    protected override void OnClick()
 67    {
 068        OnClicked?.Invoke(this);
 069        warningPanel.SetActive(false);
 070    }
 71
 72    public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin)
 73    {
 281574        root.gameObject.SetActive(true);
 75
 281576        wearableItem = w;
 281577        selected = isSelected;
 281578        amountContainer.gameObject.SetActive(amount > 1);
 281579        amountText.text = $"x{amount.ToString()}";
 80
 281581        if (gameObject.activeInHierarchy)
 3482            GetThumbnail();
 281583    }
 84
 85    public void OnPointerEnter(PointerEventData eventData)
 86    {
 087        if (!ShowReplacementWarningPanel())
 088            ShowHidingWarningPanel();
 089    }
 90
 91    private bool ShowReplacementWarningPanel()
 92    {
 093        if (getEquippedWearablesReplacedByFunc == null) return false;
 094        var shouldShow = getEquippedWearablesReplacedByFunc(wearableItem);
 095        warningPanel.SetActive(shouldShow);
 096        return shouldShow;
 97    }
 98
 99    private bool ShowHidingWarningPanel()
 100    {
 0101        if (getEquippedWearablesHiddenBy == null) return false;
 0102        var shouldShow = getEquippedWearablesHiddenBy(wearableItem);
 0103        hideWarningPanel.SetActive(shouldShow);
 0104        return shouldShow;
 105    }
 106
 107    public void OnPointerExit(PointerEventData eventData)
 108    {
 0109        warningPanel.SetActive(false);
 0110        hideWarningPanel.SetActive(false);
 0111    }
 112
 113    public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) =>
 0114        getEquippedWearablesHiddenBy = function;
 115
 116    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 0117        getEquippedWearablesReplacedByFunc = function;
 118
 1692119    private void OnEnable() { GetThumbnail(); }
 120
 121    protected virtual void OnDestroy()
 122    {
 0123        OnClicked = null;
 0124    }
 125
 0126    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 127
 128    private void GetThumbnail()
 129    {
 880130        string url = wearableItem?.ComposeThumbnailUrl();
 131
 880132        if (wearableItem == null || string.IsNullOrEmpty(url))
 133        {
 768134            SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 768135            return;
 136        }
 137
 112138        ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 112139    }
 140
 141    private void OnThumbnailReady(Asset_Texture texture)
 142    {
 0143        SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED);
 144
 0145        thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated);
 146
 0147        if (view != null && wasCreated)
 148        {
 0149            if (view.avatarEditorCanvas.enabled)
 0150                AudioScriptableObjects.listItemAppear.Play(true);
 151        }
 0152    }
 153
 154    private void SetLoadingAnimationTrigger(int id)
 155    {
 768156        if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null)
 752157            return;
 158
 16159        loadingAnimator.SetTrigger(id);
 16160    }
 161
 162    public void Hide()
 163    {
 61841164        root.gameObject.SetActive(false);
 61841165    }
 166    public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked)
 167    {
 0168        OnClicked = toggleClicked;
 0169        OnSellClicked = sellClicked;
 0170    }
 171}