< 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:63
Uncovered lines:17
Coverable lines:80
Total lines:181
Line coverage:78.7% (63 of 80)
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%5.035088.89%
RemoveItemToggle(...)0%12300%
RemoveAllItemToggle()0%220100%
SetBodyShape(...)0%2.032080%
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%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Runtime.CompilerServices;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7[assembly: InternalsVisibleTo("AvatarEditorHUDTests")]
 8
 9public class ItemSelector : MonoBehaviour
 10{
 11    [SerializeField]
 12    internal ItemToggleFactory itemToggleFactory;
 13
 14    [SerializeField]
 15    private RectTransform itemContainer;
 16
 17    [SerializeField]
 18    internal GameObject loadingSpinner;
 19
 20    [SerializeField]
 21    internal GameObject loadingRetry;
 22
 23    [SerializeField]
 24    internal Button loadingRetryButton;
 25
 26    public event System.Action<string> OnItemClicked;
 27    public event System.Action<string> OnSellClicked;
 28    public event System.Action OnRetryClicked;
 29
 84630    internal Dictionary<string, ItemToggle> itemToggles = new Dictionary<string, ItemToggle>();
 31
 32    private string currentBodyShape;
 33
 34    private void Awake()
 35    {
 13636        Application.quitting += () =>
 37        {
 038            OnItemClicked = null;
 039        };
 40
 13641        loadingRetryButton.onClick.AddListener(RetryLoading);
 13642    }
 43
 27244    private void OnDestroy() { loadingRetryButton.onClick.RemoveListener(RetryLoading); }
 45
 46    public void AddItemToggle(WearableItem item, int amount,
 47        Func<WearableItem, bool> hideOtherWearablesToastStrategy,
 48        Func<WearableItem, bool> replaceOtherWearablesToastStrategy)
 49    {
 406850        if (item == null)
 051            return;
 406852        if (itemToggles.ContainsKey(item.id))
 053            return;
 54
 55        ItemToggle newToggle;
 406856        if (item.IsCollectible())
 57        {
 3658            newToggle = itemToggleFactory.CreateItemToggleFromRarity(item.rarity, itemContainer);
 3659            newToggle.transform.SetAsFirstSibling();
 3660        }
 61        else
 62        {
 403263            newToggle = itemToggleFactory.CreateBaseWearable(itemContainer);
 64        }
 65
 406866        newToggle.Initialize(item, false, amount);
 406867        newToggle.SetHideOtherWerablesToastStrategy(hideOtherWearablesToastStrategy);
 406868        newToggle.SetReplaceOtherWearablesToastStrategy(replaceOtherWearablesToastStrategy);
 406869        newToggle.OnClicked += ToggleClicked;
 406870        newToggle.OnSellClicked += SellClicked;
 406871        itemToggles.Add(item.id, newToggle);
 72
 406873        bool active = string.IsNullOrEmpty(currentBodyShape) || item.SupportsBodyShape(currentBodyShape);
 406874        newToggle.gameObject.SetActive(active);
 406875    }
 76
 77    public void RemoveItemToggle(string itemID)
 78    {
 079        if (string.IsNullOrEmpty(itemID))
 080            return;
 81
 082        ItemToggle toggle = GetItemToggleByID(itemID);
 083        if (toggle == null)
 084            return;
 85
 086        itemToggles.Remove(itemID);
 087        Destroy(toggle.gameObject);
 088    }
 89
 90    public void RemoveAllItemToggle()
 91    {
 261092        using (var it = itemToggles.GetEnumerator())
 93        {
 538294            while (it.MoveNext())
 95            {
 277296                Destroy(it.Current.Value.gameObject);
 97            }
 261098        }
 99
 2610100        itemToggles.Clear();
 2610101    }
 102
 103    public void SetBodyShape(string bodyShape)
 104    {
 850105        if (currentBodyShape == bodyShape)
 0106            return;
 107
 850108        currentBodyShape = bodyShape;
 850109        ShowCompatibleWithBodyShape();
 850110    }
 111
 112    private void ShowCompatibleWithBodyShape()
 113    {
 850114        using (Dictionary<string, ItemToggle>.Enumerator iterator = itemToggles.GetEnumerator())
 115        {
 2150116            while (iterator.MoveNext())
 117            {
 1300118                ItemToggle current = iterator.Current.Value;
 1300119                bool active = current.wearableItem.SupportsBodyShape(currentBodyShape);
 1300120                current.gameObject.SetActive(active);
 121            }
 850122        }
 850123    }
 124
 125    public void Select(string itemID)
 126    {
 2006127        ItemToggle toggle = GetItemToggleByID(itemID);
 2006128        if (toggle != null)
 1028129            toggle.selected = true;
 2006130    }
 131
 132    public void SetWearableLoadingSpinner(string wearableID, bool isActive)
 133    {
 3726134        ItemToggle toggle = GetItemToggleByID(wearableID);
 3726135        if (toggle != null)
 1859136            toggle.SetLoadingSpinner(isActive);
 3726137    }
 138
 139    public void Unselect(string itemID)
 140    {
 886141        ItemToggle toggle = GetItemToggleByID(itemID);
 886142        if (toggle != null)
 443143            toggle.selected = false;
 886144    }
 145
 146    public void UnselectAll()
 147    {
 1750148        using (var iterator = itemToggles.GetEnumerator())
 149        {
 4470150            while (iterator.MoveNext())
 151            {
 2720152                iterator.Current.Value.selected = false;
 153            }
 1750154        }
 1750155    }
 156
 0157    private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); }
 158
 0159    private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); }
 160
 161    private ItemToggle GetItemToggleByID(string itemID)
 162    {
 6618163        if (string.IsNullOrEmpty(itemID))
 0164            return null;
 6618165        return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null;
 166    }
 167
 168    public void ShowLoading(bool isActive)
 169    {
 1170        loadingSpinner.SetActive(isActive);
 1171        loadingSpinner.transform.SetAsLastSibling();
 1172    }
 173
 174    public void ShowRetryLoading(bool isActive)
 175    {
 1176        loadingRetry.SetActive(isActive);
 1177        loadingRetry.transform.SetAsLastSibling();
 1178    }
 179
 0180    private void RetryLoading() { OnRetryClicked?.Invoke(); }
 181}