< Summary

Class:SectionSelectorComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionSelectorComponentView.cs
Covered lines:40
Uncovered lines:10
Coverable lines:50
Total lines:138
Line coverage:80% (40 of 50)
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.012087.5%
RemoveAllInstantiatedSections()0%6.226081.82%
DestroyGameObjectOnEditor()0%12300%
RegisterCurrentInstantiatedSections()0%5.025090%

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
 5035    internal List<ISectionToggle> instantiatedSections = new List<ISectionToggle>();
 36
 37    public override void Awake()
 38    {
 3139        base.Awake();
 40
 3141        RegisterCurrentInstantiatedSections();
 3142    }
 43
 44    public void Configure(BaseComponentModel newModel)
 45    {
 146        model = (SectionSelectorComponentModel)newModel;
 147        RefreshControl();
 148    }
 49
 50    public override void RefreshControl()
 51    {
 1752        if (model == null)
 053            return;
 54
 1755        SetSections(model.sections);
 1756    }
 57
 58    public void SetSections(List<SectionToggleModel> sections)
 59    {
 2160        model.sections = sections;
 61
 2162        RemoveAllInstantiatedSections();
 63
 27064        for (int i = 0; i < sections.Count; i++)
 65        {
 11466            CreateSection(sections[i], $"Section_{i}");
 67        }
 68
 2169        if (instantiatedSections.Count > 0)
 2170            instantiatedSections[0].SelectToggle();
 2171    }
 72
 73    public ISectionToggle GetSection(int index)
 74    {
 15275        if (index >= instantiatedSections.Count)
 076            return null;
 77
 15278        return instantiatedSections[index];
 79    }
 80
 081    public List<ISectionToggle> GetAllSections() { return instantiatedSections; }
 82
 83    internal void CreateSection(SectionToggleModel newSectionModel, string name)
 84    {
 11585        if (sectionToggleTemplate == null)
 086            return;
 87
 11588        SectionToggle newGO = Instantiate(sectionToggleTemplate, transform);
 11589        newGO.name = name;
 11590        newGO.gameObject.SetActive(true);
 11591        newGO.SetInfo(newSectionModel);
 11592        instantiatedSections.Add(newGO);
 11593    }
 94
 95    internal void RemoveAllInstantiatedSections()
 96    {
 29697        foreach (Transform child in transform)
 98        {
 12699            if (child.gameObject == sectionToggleTemplate.gameObject)
 100                continue;
 101
 104102            if (Application.isPlaying)
 103            {
 104104                Destroy(child.gameObject);
 104105            }
 106            else
 107            {
 0108                if (isActiveAndEnabled)
 0109                    StartCoroutine(DestroyGameObjectOnEditor(child.gameObject));
 110            }
 111        }
 112
 22113        instantiatedSections.Clear();
 22114    }
 115
 116    internal IEnumerator DestroyGameObjectOnEditor(GameObject go)
 117    {
 0118        yield return null;
 0119        DestroyImmediate(go);
 0120    }
 121
 122    internal void RegisterCurrentInstantiatedSections()
 123    {
 31124        instantiatedSections.Clear();
 125
 404126        foreach (Transform child in transform)
 127        {
 171128            if (child.gameObject == sectionToggleTemplate.gameObject)
 129                continue;
 130
 140131            SectionToggle existingSection = child.GetComponent<SectionToggle>();
 140132            if (existingSection != null)
 140133                instantiatedSections.Add(existingSection);
 134            else
 0135                Destroy(child.gameObject);
 136        }
 31137    }
 138}