< Summary

Class:SectionSelectorComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionSelectorComponentView.cs
Covered lines:41
Uncovered lines:10
Coverable lines:51
Total lines:140
Line coverage:80.3% (41 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionSelectorComponentView()0%110100%
Awake()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.062075%
SetSections(...)0%330100%
GetSection(...)0%2.152066.67%
GetAllSections()0%2100%
CreateSection(...)0%2.012088.89%
RemoveAllInstantiatedSections()0%6.226081.82%
DestroyGameObjectOnEditor()0%12300%
RegisterCurrentInstantiatedSections()0%6.046090%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionSelectorComponentView.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public interface ISectionSelectorComponentView
 6{
 7    /// <summary>
 8    /// Set the sections of the selector.
 9    /// </summary>
 10    /// <param name="sections">List of UI components.</param>
 11    void SetSections(List<SectionToggleModel> sections);
 12
 13    /// <summary>
 14    /// Get a section of the section selector.
 15    /// </summary>
 16    /// <param name="index">Index of the list of sections.</param>
 17    /// <returns>A specific section toggle.</returns>
 18    ISectionToggle GetSection(int index);
 19
 20    /// <summary>
 21    /// Get all the sections of the section selector.
 22    /// </summary>
 23    /// <returns>The list of sections.</returns>
 24    List<ISectionToggle> GetAllSections();
 25}
 26
 27public class SectionSelectorComponentView : BaseComponentView, ISectionSelectorComponentView, IComponentModelConfig
 28{
 29    [Header("Prefab References")]
 30    [SerializeField] internal SectionToggle sectionToggleTemplate;
 31
 32    [Header("Configuration")]
 33    [SerializeField] internal SectionSelectorComponentModel model;
 34
 10135    internal List<ISectionToggle> instantiatedSections = new List<ISectionToggle>();
 36
 37    public override void Awake()
 38    {
 5839        base.Awake();
 40
 5841        RegisterCurrentInstantiatedSections();
 5842    }
 43
 44    public void Configure(BaseComponentModel newModel)
 45    {
 146        model = (SectionSelectorComponentModel)newModel;
 147        RefreshControl();
 148    }
 49
 50    public override void RefreshControl()
 51    {
 1652        if (model == null)
 053            return;
 54
 1655        SetSections(model.sections);
 1656    }
 57
 58    public void SetSections(List<SectionToggleModel> sections)
 59    {
 2060        model.sections = sections;
 61
 2062        RemoveAllInstantiatedSections();
 63
 22664        for (int i = 0; i < sections.Count; i++)
 65        {
 9366            CreateSection(sections[i], $"Section_{i}");
 67        }
 68
 2069        if (instantiatedSections.Count > 0)
 2070            instantiatedSections[0].SelectToggle();
 2071    }
 72
 73    public ISectionToggle GetSection(int index)
 74    {
 79375        if (index >= instantiatedSections.Count)
 076            return null;
 77
 79378        return instantiatedSections[index];
 79    }
 80
 081    public List<ISectionToggle> GetAllSections() { return instantiatedSections; }
 82
 83    internal void CreateSection(SectionToggleModel newSectionModel, string name)
 84    {
 9485        if (sectionToggleTemplate == null)
 086            return;
 87
 9488        SectionToggle newGO = Instantiate(sectionToggleTemplate, transform);
 9489        newGO.name = name;
 9490        newGO.gameObject.SetActive(true);
 9491        newGO.SetInfo(newSectionModel);
 9492        newGO.SelectToggle();
 9493        instantiatedSections.Add(newGO);
 9494    }
 95
 96    internal void RemoveAllInstantiatedSections()
 97    {
 25098        foreach (Transform child in transform)
 99        {
 104100            if (child.gameObject == sectionToggleTemplate.gameObject)
 101                continue;
 102
 83103            if (Application.isPlaying)
 104            {
 83105                Destroy(child.gameObject);
 83106            }
 107            else
 108            {
 0109                if (isActiveAndEnabled)
 0110                    StartCoroutine(DestroyGameObjectOnEditor(child.gameObject));
 111            }
 112        }
 113
 21114        instantiatedSections.Clear();
 21115    }
 116
 117    internal IEnumerator DestroyGameObjectOnEditor(GameObject go)
 118    {
 0119        yield return null;
 0120        DestroyImmediate(go);
 0121    }
 122
 123    internal void RegisterCurrentInstantiatedSections()
 124    {
 58125        instantiatedSections.Clear();
 126
 784127        foreach (Transform child in transform)
 128        {
 334129            if (child.gameObject == sectionToggleTemplate.gameObject ||
 130                child.name.Contains("TooltipRef"))
 131                continue;
 132
 276133            SectionToggle existingSection = child.GetComponent<SectionToggle>();
 276134            if (existingSection != null)
 276135                instantiatedSections.Add(existingSection);
 136            else
 0137                Destroy(child.gameObject);
 138        }
 58139    }
 140}