< Summary

Class:SectionSelectorComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionSelectorComponentView.cs
Covered lines:33
Uncovered lines:14
Coverable lines:47
Total lines:138
Line coverage:70.2% (33 of 47)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionSelectorComponentView()0%110100%
PostInitialization()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.062075%
SetSections(...)0%220100%
GetSection(...)0%220100%
GetAllSections()0%2100%
CreateSection(...)0%4.123050%
IntantiateSectionToggle(...)0%2.012087.5%
InstantiateSectionToggleOnEditor()0%12300%
RemoveAllInstantiatedSections()0%6.226081.82%
DestroyGameObjectOnEditor()0%12300%

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    /// Fill the model and updates the section selector with this data.
 9    /// </summary>
 10    /// <param name="model">Data to configure the section selector.</param>
 11    void Configure(SectionSelectorComponentModel model);
 12
 13    /// <summary>
 14    /// Set the sections of the selector.
 15    /// </summary>
 16    /// <param name="sections">List of UI components.</param>
 17    void SetSections(List<SectionToggleModel> sections);
 18
 19    /// <summary>
 20    /// Get a section of the section selector.
 21    /// </summary>
 22    /// <param name="index">Index of the list of sections.</param>
 23    /// <returns>A specific section toggle.</returns>
 24    ISectionToggle GetSection(int index);
 25
 26    /// <summary>
 27    /// Get all the sections of the section selector.
 28    /// </summary>
 29    /// <returns>The list of sections.</returns>
 30    List<ISectionToggle> GetAllSections();
 31}
 32
 33public class SectionSelectorComponentView : BaseComponentView, ISectionSelectorComponentView
 34{
 35    [Header("Prefab References")]
 36    [SerializeField] internal SectionToggle sectionToggleTemplate;
 37
 38    [Header("Configuration")]
 39    [SerializeField] internal SectionSelectorComponentModel model;
 40
 5141    private List<ISectionToggle> instantiatedSections = new List<ISectionToggle>();
 42
 6243    public override void PostInitialization() { Configure(model); }
 44
 45    public void Configure(SectionSelectorComponentModel model)
 46    {
 3247        this.model = model;
 3248        RefreshControl();
 3249    }
 50
 51    public override void RefreshControl()
 52    {
 5153        if (model == null)
 054            return;
 55
 5156        SetSections(model.sections);
 5157    }
 58
 59    public void SetSections(List<SectionToggleModel> sections)
 60    {
 5561        model.sections = sections;
 62
 5563        RemoveAllInstantiatedSections();
 64
 64465        for (int i = 0; i < sections.Count; i++)
 66        {
 26767            CreateSection(sections[i], $"Section_{i}");
 68        }
 5569    }
 70
 71    public ISectionToggle GetSection(int index)
 72    {
 12773        if (index >= instantiatedSections.Count)
 1574            return null;
 75
 11276        return instantiatedSections[index];
 77    }
 78
 079    public List<ISectionToggle> GetAllSections() { return instantiatedSections; }
 80
 81    internal void CreateSection(SectionToggleModel newSectionModel, string name)
 82    {
 26783        if (Application.isPlaying)
 84        {
 26785            IntantiateSectionToggle(newSectionModel, name);
 26786        }
 87        else
 88        {
 089            if (isActiveAndEnabled)
 090                StartCoroutine(InstantiateSectionToggleOnEditor(newSectionModel, name));
 91        }
 092    }
 93
 94    internal void IntantiateSectionToggle(SectionToggleModel newSectionModel, string name)
 95    {
 26796        if (sectionToggleTemplate == null)
 097            return;
 98
 26799        SectionToggle newGO = Instantiate(sectionToggleTemplate, transform);
 267100        newGO.name = name;
 267101        newGO.SetInfo(newSectionModel);
 267102        newGO.gameObject.SetActive(true);
 267103        instantiatedSections.Add(newGO);
 267104    }
 105
 106    internal IEnumerator InstantiateSectionToggleOnEditor(SectionToggleModel sectionModel, string name)
 107    {
 0108        yield return null;
 0109        IntantiateSectionToggle(sectionModel, name);
 0110    }
 111
 112    internal void RemoveAllInstantiatedSections()
 113    {
 942114        foreach (Transform child in transform)
 115        {
 415116            if (child.gameObject == sectionToggleTemplate.gameObject)
 117                continue;
 118
 359119            if (Application.isPlaying)
 120            {
 359121                Destroy(child.gameObject);
 359122            }
 123            else
 124            {
 0125                if (isActiveAndEnabled)
 0126                    StartCoroutine(DestroyGameObjectOnEditor(child.gameObject));
 127            }
 128        }
 129
 56130        instantiatedSections.Clear();
 56131    }
 132
 133    internal IEnumerator DestroyGameObjectOnEditor(GameObject go)
 134    {
 0135        yield return null;
 0136        DestroyImmediate(go);
 0137    }
 138}