< 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:80
Uncovered lines:23
Coverable lines:103
Total lines:233
Line coverage:77.6% (80 of 103)
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%
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%110100%
ShowRetryLoading(...)0%110100%
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
 5436132    public void UpdateSelectorLayout() { Utils.ForceUpdateLayout(content); }
 133
 134    private void ShowCompatibleWithBodyShape()
 135    {
 901136        using (Dictionary<string, ItemToggle>.Enumerator iterator = itemToggles.GetEnumerator())
 137        {
 2279138            while (iterator.MoveNext())
 139            {
 1378140                ItemToggle current = iterator.Current.Value;
 1378141                bool active = current.wearableItem.SupportsBodyShape(currentBodyShape);
 1378142                current.gameObject.SetActive(active);
 143            }
 901144        }
 901145    }
 146
 147    public void Select(string itemID)
 148    {
 2107149        ItemToggle toggle = GetItemToggleByID(itemID);
 2107150        if (toggle != null)
 1080151            toggle.selected = true;
 2107152    }
 153
 154    public void SetWearableLoadingSpinner(string wearableID, bool isActive)
 155    {
 2982156        ItemToggle toggle = GetItemToggleByID(wearableID);
 2982157        if (toggle != null)
 1491158            toggle.SetLoadingSpinner(isActive);
 2982159    }
 160
 161    public void Unselect(string itemID)
 162    {
 928163        ItemToggle toggle = GetItemToggleByID(itemID);
 928164        if (toggle != null)
 464165            toggle.selected = false;
 928166    }
 167
 168    public void UnselectAll()
 169    {
 1821170        using (var iterator = itemToggles.GetEnumerator())
 171        {
 4651172            while (iterator.MoveNext())
 173            {
 2830174                iterator.Current.Value.selected = false;
 175            }
 1821176        }
 1821177    }
 178
 0179    private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); }
 180
 0181    private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); }
 182
 183    private ItemToggle GetItemToggleByID(string itemID)
 184    {
 6017185        if (string.IsNullOrEmpty(itemID))
 0186            return null;
 6017187        return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null;
 188    }
 189
 190    public void ShowLoading(bool isActive)
 191    {
 1192        loadingSpinner.SetActive(isActive);
 1193        loadingSpinner.transform.SetAsLastSibling();
 1194    }
 195
 196    public void ShowRetryLoading(bool isActive)
 197    {
 1198        loadingRetry.SetActive(isActive);
 1199        loadingRetry.transform.SetAsLastSibling();
 1200    }
 201
 0202    private void RetryLoading() { OnRetryClicked?.Invoke(); }
 203
 204    private CollectionGroup CreateCollectionGroupIfNeeded(string collectionId, string collectionName)
 205    {
 4242206        if (currentCollectionGroups.ContainsKey(collectionId))
 3706207            return currentCollectionGroups[collectionId];
 208
 536209        CollectionGroup newCollectionGroup = Instantiate(collectionGroupPrefab, content);
 536210        newCollectionGroup.Configure(collectionId, collectionName);
 536211        currentCollectionGroups.Add(collectionId, newCollectionGroup);
 212
 536213        if (collectionId == DECENTRALAND_COLLECTION_ID)
 536214            newCollectionGroup.transform.SetAsFirstSibling();
 215        else
 0216            newCollectionGroup.transform.SetAsLastSibling();
 217
 536218        return newCollectionGroup;
 219    }
 220
 221    private bool RemoveCollectionGroupIfNeeded(string collectionId)
 222    {
 2884223        currentCollectionGroups.TryGetValue(collectionId, out CollectionGroup collectionGroupToRemove);
 11536224        if (collectionGroupToRemove != null && itemToggles.Count(x => x.Value.collectionId == collectionId) == 0)
 225        {
 0226            currentCollectionGroups.Remove(collectionId);
 0227            Destroy(collectionGroupToRemove.gameObject);
 0228            return true;
 229        }
 230
 2884231        return false;
 232    }
 233}