< Summary

Class:SmartItemActionEventAdapter
Assembly:BuilderInWorldEntityInformation
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/EntityInformation/SmartItems/Parameters/Actions/SmartItemActionEventAdapter.cs
Covered lines:3
Uncovered lines:90
Coverable lines:93
Total lines:194
Line coverage:3.2% (3 of 93)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SmartItemActionEventAdapter()0%110100%
Start()0%2100%
OptionSelected(...)0%12300%
GetContent()0%2100%
RemoveActionable()0%6200%
ResetActionable()0%2100%
SetContent(...)0%6200%
SelectedEntity(...)0%6200%
GenerateParametersFromSelectedOption()0%2100%
GenerateParametersFromIndex(...)0%12300%
GenerateActionDropdownContent(...)0%20400%
GenerateEntityDropdownContent()0%30500%
GetThumbnail(...)0%30500%
SetThumbnail(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/EntityInformation/SmartItems/Parameters/Actions/SmartItemActionEventAdapter.cs

#LineLine coverage
 1using DCL;
 2using DCL.Components;
 3using DCL.Models;
 4using System.Collections;
 5using System.Collections.Generic;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.UI;
 9
 10public class SmartItemActionEventAdapter : MonoBehaviour
 11{
 12    public TMP_Dropdown entityDropDown;
 13    public TMP_Dropdown actionDropDown;
 14    public TMP_Dropdown optionsDropDown;
 15    public SmartItemListView smartItemListView;
 16
 17    public System.Action<SmartItemActionEventAdapter> OnActionableRemove;
 18
 19    private SmartItemActionEvent actionEvent;
 20
 21    private DCLBuilderInWorldEntity selectedEntity;
 222    private List<DCLBuilderInWorldEntity> filteredList = new List<DCLBuilderInWorldEntity>();
 23
 224    private Dictionary<DCLBuilderInWorldEntity, Sprite> entitySpriteDict = new Dictionary<DCLBuilderInWorldEntity, Sprit
 225    private Dictionary<string, DCLBuilderInWorldEntity> entityPromiseKeeperDict = new Dictionary<string, DCLBuilderInWor
 26
 27    private void Start()
 28    {
 029        entityDropDown.onValueChanged.AddListener(SelectedEntity);
 030        actionDropDown.onValueChanged.AddListener(GenerateParametersFromIndex);
 031        optionsDropDown.onValueChanged.AddListener(OptionSelected);
 032        optionsDropDown.SetValueWithoutNotify(-1);
 033    }
 34
 35    private void OptionSelected(int index)
 36    {
 37        switch (index)
 38        {
 39            case 0:
 040                ResetActionable();
 041                break;
 42            case 1:
 043                RemoveActionable();
 44                break;
 45        }
 46
 047        optionsDropDown.SetValueWithoutNotify(1);
 048    }
 49
 050    public SmartItemActionEvent GetContent() { return actionEvent; }
 51
 52    public void RemoveActionable()
 53    {
 054        OnActionableRemove?.Invoke(this);
 055        Destroy(gameObject);
 056    }
 57
 58    public void ResetActionable()
 59    {
 060        actionEvent.smartItemActionable.values.Clear();
 061        SetContent(actionEvent);
 062    }
 63
 64    public void SetContent(SmartItemActionEvent actionEvent)
 65    {
 066        this.actionEvent = actionEvent;
 067        filteredList = BuilderInWorldUtils.FilterEntitiesBySmartItemComponentAndActions(actionEvent.entityList);
 68
 069        GenerateEntityDropdownContent();
 070        foreach (DCLBuilderInWorldEntity entity in filteredList)
 71        {
 072            GetThumbnail(entity);
 73        }
 74
 075        SelectedEntity(entityDropDown.value);
 076    }
 77
 78    private void SelectedEntity(int number)
 79    {
 080        if (!filteredList[number].rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntityComponent com
 81        {
 082            return;
 83        }
 84
 085        actionEvent.smartItemActionable.entityId = filteredList[number].rootEntity.entityId;
 086        selectedEntity = filteredList[number];
 087        GenerateActionDropdownContent(filteredList[number].GetSmartItemActions());
 88
 089        GenerateParametersFromSelectedOption();
 090    }
 91
 092    private void GenerateParametersFromSelectedOption() { GenerateParametersFromIndex(actionDropDown.value); }
 93
 94    private void GenerateParametersFromIndex(int index)
 95    {
 096        string label = actionDropDown.options[index].text;
 97
 098        SmartItemAction selectedAction = null;
 099        foreach (SmartItemAction action in selectedEntity.GetSmartItemActions())
 100        {
 0101            if (action.label == label)
 102            {
 0103                selectedAction = action;
 0104                break;
 105            }
 106        }
 107
 0108        actionEvent.smartItemActionable.actionId = selectedAction.id;
 0109        smartItemListView.SetEntityList(actionEvent.entityList);
 0110        smartItemListView.SetSmartItemParameters(selectedAction.parameters, actionEvent.smartItemActionable.values);
 0111    }
 112
 113    private void GenerateActionDropdownContent(SmartItemAction[] actions)
 114    {
 0115        actionDropDown.ClearOptions();
 116
 0117        actionDropDown.options = new List<TMP_Dropdown.OptionData>();
 118
 0119        List<string> optionsLabelList = new List<string>();
 0120        int index = 0;
 0121        int indexToUse = 0;
 122
 0123        foreach (SmartItemAction action in actions)
 124        {
 0125            optionsLabelList.Add(action.label);
 0126            if (!string.IsNullOrEmpty(actionEvent.smartItemActionable.actionId) &&
 127                action.id == actionEvent.smartItemActionable.actionId)
 0128                indexToUse = index;
 129
 0130            index++;
 131        }
 132
 0133        actionDropDown.AddOptions(optionsLabelList);
 0134        actionDropDown.SetValueWithoutNotify(indexToUse);
 0135    }
 136
 137    private void GenerateEntityDropdownContent()
 138    {
 0139        entityDropDown.ClearOptions();
 140
 0141        entityDropDown.options = new List<TMP_Dropdown.OptionData>();
 142
 0143        List<TMP_Dropdown.OptionData> optionsList = new List<TMP_Dropdown.OptionData>();
 144
 0145        int index = 0;
 0146        int indexToUse = 0;
 147
 0148        foreach (DCLBuilderInWorldEntity entity in filteredList)
 149        {
 0150            var item = new TMP_Dropdown.OptionData();
 0151            item.text = entity.GetDescriptiveName();
 0152            if (entitySpriteDict.ContainsKey(entity))
 0153                item.image = entitySpriteDict[entity];
 0154            optionsList.Add(item);
 155
 0156            if (!string.IsNullOrEmpty(actionEvent.smartItemActionable.entityId) &&
 157                entity.rootEntity.entityId == actionEvent.smartItemActionable.entityId)
 0158                indexToUse = index;
 159
 0160            index++;
 161        }
 162
 0163        entityDropDown.AddOptions(optionsList);
 0164        entityDropDown.SetValueWithoutNotify(indexToUse);
 0165    }
 166
 167    private void GetThumbnail(DCLBuilderInWorldEntity entity)
 168    {
 0169        var url = entity.GetCatalogItemAssociated()?.thumbnailURL;
 170
 0171        if (string.IsNullOrEmpty(url))
 0172            return;
 173
 0174        string newLoadedThumbnailURL = url;
 0175        var newLoadedThumbnailPromise = new AssetPromise_Texture(url);
 176
 0177        string promiseId = newLoadedThumbnailPromise.GetId().ToString();
 0178        if (!entityPromiseKeeperDict.ContainsKey(promiseId))
 0179            entityPromiseKeeperDict.Add(promiseId, entity);
 0180        newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail;
 0181        newLoadedThumbnailPromise.OnFailEvent += x => { Debug.Log($"Error downloading: {url}"); };
 182
 0183        AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise);
 0184    }
 185
 186    public void SetThumbnail(Asset_Texture texture)
 187    {
 0188        if (!entityPromiseKeeperDict.ContainsKey(texture.id.ToString()))
 0189            return;
 0190        Sprite spriteToUse = Sprite.Create(texture.texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0
 0191        entitySpriteDict.Add(entityPromiseKeeperDict[texture.id.ToString()], spriteToUse);
 0192        GenerateEntityDropdownContent();
 0193    }
 194}