< Summary

Class:SectionToggle
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs
Covered lines:19
Uncovered lines:5
Coverable lines:24
Total lines:91
Line coverage:79.1% (19 of 24)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetInfo()0%110100%
SetInfo(...)0%4.024088.89%
SelectToggle()0%6200%
OnDestroy()0%220100%

File(s)

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

#LineLine coverage
 1using TMPro;
 2using UnityEngine;
 3using UnityEngine.UI;
 4using static UnityEngine.UI.Toggle;
 5
 6public interface ISectionToggle
 7{
 8    /// <summary>
 9    /// Event that will be triggered when the toggle is selected.
 10    /// </summary>
 11    ToggleEvent onSelect { get; set; }
 12
 13    /// <summary>
 14    /// Get the toggle info.
 15    /// </summary>
 16    /// <returns>Model with all the toggle info.</returns>
 17    SectionToggleModel GetInfo();
 18
 19    /// <summary>
 20    /// Set the toggle info.
 21    /// </summary>
 22    /// <param name="model">Model with all the toggle info.</param>
 23    void SetInfo(SectionToggleModel model);
 24
 25    /// <summary>
 26    /// Invoke the action of selecting the toggle.
 27    /// </summary>
 28    void SelectToggle();
 29}
 30
 31public class SectionToggle : MonoBehaviour, ISectionToggle
 32{
 33    [SerializeField] private Toggle toggle;
 34    [SerializeField] private TMP_Text sectionText;
 35    [SerializeField] private Image sectionImage;
 36
 37    public ToggleEvent onSelect
 38    {
 11039        get { return toggle?.onValueChanged; }
 40        set
 41        {
 26742            toggle?.onValueChanged.RemoveAllListeners();
 26743            toggle?.onValueChanged.AddListener((isOn) =>
 44            {
 4545                if (isOn)
 4546                    value?.Invoke(isOn);
 4547            });
 26748        }
 49    }
 50
 51    public SectionToggleModel GetInfo()
 52    {
 453        return new SectionToggleModel
 54        {
 55            icon = sectionImage.sprite,
 56            title = sectionText.text,
 57            onSelect = onSelect
 58        };
 59    }
 60
 61    public void SetInfo(SectionToggleModel model)
 62    {
 26763        if (model == null)
 064            return;
 65
 26766        if (sectionText != null)
 26767            sectionText.text = model.title;
 68
 26769        if (sectionImage != null)
 70        {
 26771            sectionImage.enabled = model.icon != null;
 26772            sectionImage.sprite = model.icon;
 73        }
 74
 26775        onSelect = model.onSelect;
 26776    }
 77
 78    public void SelectToggle()
 79    {
 080        if (toggle == null)
 081            return;
 82
 083        toggle.isOn = true;
 084    }
 85
 86    private void OnDestroy()
 87    {
 43388        if (toggle != null)
 43389            toggle.onValueChanged.RemoveAllListeners();
 43390    }
 91}