< Summary

Class:SectionSelectorComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionSelectorComponentView.cs
Covered lines:44
Uncovered lines:12
Coverable lines:56
Total lines:152
Line coverage:78.5% (44 of 56)
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%
DisableSection(...)0%220100%
EnableSection(...)0%6200%
GetAllSections()0%110100%
CreateSection(...)0%2.012088.89%
RemoveAllInstantiatedSections()0%6.296080%
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<Sect
 28{
 29    [Header("Prefab References")]
 30    [SerializeField] internal SectionToggle sectionToggleTemplate;
 31
 32    [Header("Configuration")]
 33    [SerializeField] internal SectionSelectorComponentModel model;
 34
 13135    internal List<ISectionToggle> instantiatedSections = new List<ISectionToggle>();
 36
 37    public override void Awake()
 38    {
 12339        base.Awake();
 40
 12341        RegisterCurrentInstantiatedSections();
 12342    }
 43
 44    public void Configure(SectionSelectorComponentModel newModel)
 45    {
 146        model = newModel;
 147        RefreshControl();
 148    }
 49
 50    public override void RefreshControl()
 51    {
 1452        if (model == null)
 053            return;
 54
 1455        SetSections(model.sections);
 1456    }
 57
 58    public void SetSections(List<SectionToggleModel> sections)
 59    {
 1860        model.sections = sections;
 61
 1862        RemoveAllInstantiatedSections();
 63
 20464        for (int i = 0; i < sections.Count; i++)
 65        {
 8466            CreateSection(sections[i], $"Section_{i}");
 67        }
 68
 1869        if (instantiatedSections.Count > 0)
 1870            instantiatedSections[0].SelectToggle();
 1871    }
 72
 73    public ISectionToggle GetSection(int index)
 74    {
 99275        if (index >= instantiatedSections.Count)
 076            return null;
 77
 99278        return instantiatedSections[index];
 79    }
 80
 81    public void DisableSection(int index)
 82    {
 4183        if (index < instantiatedSections.Count)
 4184            instantiatedSections[index].GameObject.SetActive(false);
 4185    }
 86
 87    public void EnableSection(int index)
 88    {
 089        if (index < instantiatedSections.Count)
 090            instantiatedSections[index].GameObject.SetActive(true);
 091    }
 92
 9793    public List<ISectionToggle> GetAllSections() { return instantiatedSections; }
 94
 95    internal void CreateSection(SectionToggleModel newSectionModel, string name)
 96    {
 8597        if (sectionToggleTemplate == null)
 098            return;
 99
 85100        SectionToggle newGO = Instantiate(sectionToggleTemplate, transform);
 85101        newGO.name = name;
 85102        newGO.gameObject.SetActive(true);
 85103        newGO.SetInfo(newSectionModel);
 85104        newGO.SelectToggle();
 85105        instantiatedSections.Add(newGO);
 85106    }
 107
 108    internal void RemoveAllInstantiatedSections()
 109    {
 204110        foreach (Transform child in transform)
 111        {
 83112            if (child.gameObject == sectionToggleTemplate.gameObject)
 113                continue;
 114
 64115            if (Application.isPlaying)
 116            {
 64117                Destroy(child.gameObject);
 118            }
 119            else
 120            {
 0121                if (isActiveAndEnabled)
 0122                    StartCoroutine(DestroyGameObjectOnEditor(child.gameObject));
 123            }
 124        }
 125
 19126        instantiatedSections.Clear();
 19127    }
 128
 129    internal IEnumerator DestroyGameObjectOnEditor(GameObject go)
 130    {
 0131        yield return null;
 0132        DestroyImmediate(go);
 0133    }
 134
 135    internal void RegisterCurrentInstantiatedSections()
 136    {
 123137        instantiatedSections.Clear();
 138
 1294139        foreach (Transform child in transform)
 140        {
 524141            if (child.gameObject == sectionToggleTemplate.gameObject ||
 142                child.name.Contains("TooltipRef"))
 143                continue;
 144
 401145            SectionToggle existingSection = child.GetComponent<SectionToggle>();
 401146            if (existingSection != null)
 401147                instantiatedSections.Add(existingSection);
 148            else
 0149                Destroy(child.gameObject);
 150        }
 123151    }
 152}