< Summary

Class:ItemSelector
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ItemSelector.cs
Covered lines:75
Uncovered lines:29
Coverable lines:104
Total lines:235
Line coverage:72.1% (75 of 104)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ItemSelector()0%110100%
Awake()0%110100%
OnDestroy()0%110100%
AddItemToggle(...)0%6.096086.36%
RemoveItemToggle(...)0%12300%
RemoveAllItemToggle()0%220100%
SetBodyShape(...)0%2.032080%
OnEnable()0%110100%
UpdateSelectorLayout()0%110100%
ShowCompatibleWithBodyShape()0%220100%
Select(...)0%220100%
SetWearableLoadingSpinner(...)0%220100%
Unselect(...)0%220100%
UnselectAll()0%220100%
ToggleClicked(...)0%6200%
SellClicked(...)0%6200%
GetItemToggleByID(...)0%3.333066.67%
ShowLoading(...)0%2100%
ShowRetryLoading(...)0%2100%
RetryLoading()0%6200%
CreateCollectionGroupIfNeeded(...)0%3.013088.89%
RemoveCollectionGroupIfNeeded(...)0%4.123050%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Runtime.CompilerServices;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9[assembly: InternalsVisibleTo("AvatarEditorHUDTests")]
 10
 11public class ItemSelector : MonoBehaviour
 12{
 13    private const string DECENTRALAND_COLLECTION_ID = "Decentraland";
 14
 15    [SerializeField]
 16    internal ItemToggleFactory itemToggleFactory;
 17
 18    [SerializeField]
 19    internal CollectionGroup collectionGroupPrefab;
 20
 21    [SerializeField]
 22    internal RectTransform content;
 23
 24    [SerializeField]
 25    internal GameObject loadingSpinner;
 26
 27    [SerializeField]
 28    internal GameObject loadingRetry;
 29
 30    [SerializeField]
 31    internal Button loadingRetryButton;
 32
 33    public event System.Action<string> OnItemClicked;
 34    public event System.Action<string> OnSellClicked;
 35    public event System.Action OnRetryClicked;
 36
 90037    internal Dictionary<string, ItemToggle> itemToggles = new Dictionary<string, ItemToggle>();
 90038    internal Dictionary<string, CollectionGroup> currentCollectionGroups = new Dictionary<string, CollectionGroup>();
 39
 40    private string currentBodyShape;
 41
 42    private void Awake()
 43    {
 9444        Application.quitting += () =>
 45        {
 046            OnItemClicked = null;
 047        };
 48
 9449        loadingRetryButton.onClick.AddListener(RetryLoading);
 9450    }
 51
 18852    private void OnDestroy() { loadingRetryButton.onClick.RemoveListener(RetryLoading); }
 53
 54    public void AddItemToggle(
 55        WearableItem item,
 56        string collectionName,
 57        int amount,
 58        Func<WearableItem, bool> hideOtherWearablesToastStrategy,
 59        Func<WearableItem, bool> replaceOtherWearablesToastStrategy)
 60    {
 61        CollectionGroup collectionGroup;
 424262        if (item.IsFromThirdPartyCollection)
 063            collectionGroup = CreateCollectionGroupIfNeeded(item.ThirdPartyCollectionId, collectionName);
 64        else
 424265            collectionGroup = CreateCollectionGroupIfNeeded(DECENTRALAND_COLLECTION_ID, DECENTRALAND_COLLECTION_ID);
 66
 424267        if (item == null)
 068            return;
 424269        if (itemToggles.ContainsKey(item.id))
 070            return;
 71
 72        ItemToggle newToggle;
 424273        if (item.IsCollectible())
 74        {
 4275            newToggle = itemToggleFactory.CreateItemToggleFromRarity(item.rarity, collectionGroup.itemContainer);
 4276            newToggle.transform.SetAsFirstSibling();
 4277        }
 78        else
 79        {
 420080            newToggle = itemToggleFactory.CreateBaseWearable(collectionGroup.itemContainer);
 81        }
 82
 424283        newToggle.Initialize(item, false, amount);
 424284        newToggle.SetHideOtherWerablesToastStrategy(hideOtherWearablesToastStrategy);
 424285        newToggle.SetReplaceOtherWearablesToastStrategy(replaceOtherWearablesToastStrategy);
 424286        newToggle.OnClicked += ToggleClicked;
 424287        newToggle.OnSellClicked += SellClicked;
 424288        newToggle.collectionId = collectionGroup.collectionId;
 424289        itemToggles.Add(item.id, newToggle);
 90
 424291        bool active = string.IsNullOrEmpty(currentBodyShape) || item.SupportsBodyShape(currentBodyShape);
 424292        newToggle.gameObject.SetActive(active);
 424293    }
 94
 95    public void RemoveItemToggle(string itemID)
 96    {
 097        if (string.IsNullOrEmpty(itemID))
 098            return;
 99
 0100        ItemToggle toggle = GetItemToggleByID(itemID);
 0101        if (toggle == null)
 0102            return;
 103
 0104        itemToggles.Remove(itemID);
 0105        Destroy(toggle.gameObject);
 0106        RemoveCollectionGroupIfNeeded(toggle.collectionId);
 0107    }
 108
 109    public void RemoveAllItemToggle()
 110    {
 2718111        using (var it = itemToggles.GetEnumerator())
 112        {
 5602113            while (it.MoveNext())
 114            {
 2884115                Destroy(it.Current.Value.gameObject);
 2884116                RemoveCollectionGroupIfNeeded(it.Current.Value.collectionId);
 117            }
 2718118        }
 119
 2718120        itemToggles.Clear();
 2718121    }
 122
 123    public void SetBodyShape(string bodyShape)
 124    {
 901125        if (currentBodyShape == bodyShape)
 0126            return;
 127
 901128        currentBodyShape = bodyShape;
 901129        ShowCompatibleWithBodyShape();
 901130    }
 131
 188132    private void OnEnable() { UpdateSelectorLayout(); }
 133
 5624134    public void UpdateSelectorLayout() { Utils.ForceUpdateLayout(content); }
 135
 136    private void ShowCompatibleWithBodyShape()
 137    {
 901138        using (Dictionary<string, ItemToggle>.Enumerator iterator = itemToggles.GetEnumerator())
 139        {
 2279140            while (iterator.MoveNext())
 141            {
 1378142                ItemToggle current = iterator.Current.Value;
 1378143                bool active = current.wearableItem.SupportsBodyShape(currentBodyShape);
 1378144                current.gameObject.SetActive(active);
 145            }
 901146        }
 901147    }
 148
 149    public void Select(string itemID)
 150    {
 2107151        ItemToggle toggle = GetItemToggleByID(itemID);
 2107152        if (toggle != null)
 1080153            toggle.selected = true;
 2107154    }
 155
 156    public void SetWearableLoadingSpinner(string wearableID, bool isActive)
 157    {
 2982158        ItemToggle toggle = GetItemToggleByID(wearableID);
 2982159        if (toggle != null)
 1491160            toggle.SetLoadingSpinner(isActive);
 2982161    }
 162
 163    public void Unselect(string itemID)
 164    {
 928165        ItemToggle toggle = GetItemToggleByID(itemID);
 928166        if (toggle != null)
 464167            toggle.selected = false;
 928168    }
 169
 170    public void UnselectAll()
 171    {
 1821172        using (var iterator = itemToggles.GetEnumerator())
 173        {
 4651174            while (iterator.MoveNext())
 175            {
 2830176                iterator.Current.Value.selected = false;
 177            }
 1821178        }
 1821179    }
 180
 0181    private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); }
 182
 0183    private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); }
 184
 185    private ItemToggle GetItemToggleByID(string itemID)
 186    {
 6017187        if (string.IsNullOrEmpty(itemID))
 0188            return null;
 6017189        return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null;
 190    }
 191
 192    public void ShowLoading(bool isActive)
 193    {
 0194        loadingSpinner.SetActive(isActive);
 0195        loadingSpinner.transform.SetAsLastSibling();
 0196    }
 197
 198    public void ShowRetryLoading(bool isActive)
 199    {
 0200        loadingRetry.SetActive(isActive);
 0201        loadingRetry.transform.SetAsLastSibling();
 0202    }
 203
 0204    private void RetryLoading() { OnRetryClicked?.Invoke(); }
 205
 206    private CollectionGroup CreateCollectionGroupIfNeeded(string collectionId, string collectionName)
 207    {
 4242208        if (currentCollectionGroups.ContainsKey(collectionId))
 3706209            return currentCollectionGroups[collectionId];
 210
 536211        CollectionGroup newCollectionGroup = Instantiate(collectionGroupPrefab, content);
 536212        newCollectionGroup.Configure(collectionId, collectionName);
 536213        currentCollectionGroups.Add(collectionId, newCollectionGroup);
 214
 536215        if (collectionId == DECENTRALAND_COLLECTION_ID)
 536216            newCollectionGroup.transform.SetAsFirstSibling();
 217        else
 0218            newCollectionGroup.transform.SetAsLastSibling();
 219
 536220        return newCollectionGroup;
 221    }
 222
 223    private bool RemoveCollectionGroupIfNeeded(string collectionId)
 224    {
 2884225        currentCollectionGroups.TryGetValue(collectionId, out CollectionGroup collectionGroupToRemove);
 11536226        if (collectionGroupToRemove != null && itemToggles.Count(x => x.Value.collectionId == collectionId) == 0)
 227        {
 0228            currentCollectionGroups.Remove(collectionId);
 0229            Destroy(collectionGroupToRemove.gameObject);
 0230            return true;
 231        }
 232
 2884233        return false;
 234    }
 235}