< Summary

Class:ToggleComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentView.cs
Covered lines:51
Uncovered lines:4
Coverable lines:55
Total lines:181
Line coverage:92.7% (51 of 55)
Covered branches:0
Total branches:0
Covered methods:16
Total methods:17
Method coverage:94.1% (16 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
RefreshActiveStatus()0%770100%
ToggleChanged(...)0%220100%
Configure(...)0%110100%
RefreshControl()0%2.012087.5%
SetText(...)0%2.032080%
Dispose()0%110100%
IsInteractable()0%110100%
SetInteractable(...)0%110100%
SetTextActive(...)0%110100%
SetIsOnWithoutNotify(...)0%2.022083.33%
SetChangeTextColorOnSelect(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Toggle/ToggleComponentView.cs

#LineLine coverage
 1using System;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public interface IToggleComponentView
 7{
 8    /// <summary>
 9    /// Event that will be triggered when the toggle changes.
 10    /// </summary>
 11    event Action<bool, string, string> OnSelectedChanged;
 12
 13    /// <summary>
 14    /// Id associated to the toogle.
 15    /// </summary>
 16    string id { get; }
 17
 18    /// <summary>
 19    /// On/Off state of the toggle.
 20    /// </summary>
 21    bool isOn { get; set; }
 22
 23    /// <summary>
 24    /// Text (if exists) associated to the toggle.
 25    /// </summary>
 26    string title { get; }
 27
 28    /// <summary>
 29    /// Set the toggle text.
 30    /// </summary>
 31    /// <param name="newText">New text.</param>
 32    void SetText(string newText);
 33
 34    /// <summary>
 35    /// Set toggle text active
 36    /// </summary>
 37    /// <param name="isActive">Is active</param>
 38    void SetTextActive(bool isActive);
 39
 40    /// <summary>
 41    /// Set the toggle clickable or not.
 42    /// </summary>
 43    /// <param name="isInteractable">Clickable or not</param>
 44    void SetInteractable(bool isInteractable);
 45
 46    /// <summary>
 47    /// Return if the toggle is Interactable or not
 48    /// </summary>
 49    bool IsInteractable();
 50
 51    /// <summary>
 52    /// Set the state of the toggle as On/Off without notify event.
 53    /// </summary>
 54    /// <param name="isOn"></param>
 55    void SetIsOnWithoutNotify(bool isOn);
 56
 57    void SetChangeTextColorOnSelect(bool changeTextColorOnSelect);
 58}
 59
 60public class ToggleComponentView : BaseComponentView, IToggleComponentView, IComponentModelConfig<ToggleComponentModel>
 61{
 62    [Header("Prefab References")]
 63    [SerializeField] internal Toggle toggle;
 64    [SerializeField] internal TMP_Text text;
 65    [SerializeField] GameObject activeOn = null;
 66    [SerializeField] GameObject activeOff = null;
 67    [SerializeField] Color textColorOnUnselected;
 68    [SerializeField] Color textColorOnSelected;
 69
 70    [Header("Configuration")]
 71    [SerializeField] internal ToggleComponentModel model;
 72
 73    public event Action<bool, string, string> OnSelectedChanged;
 74
 75    public string id
 76    {
 49677        get => model.id;
 175278        set => model.id = value;
 79    }
 80
 81    public bool isOn
 82    {
 481983        get => toggle.isOn;
 84        set
 85        {
 331286            model.isOn = value;
 87
 331288            if (toggle == null)
 589                return;
 90
 330791            toggle.isOn = value;
 330792            RefreshActiveStatus();
 330793        }
 94    }
 95
 096    public string title => text.text;
 97
 98    public override void Awake()
 99    {
 1911100        base.Awake();
 101
 1911102        toggle.onValueChanged.AddListener(ToggleChanged);
 1911103        RefreshActiveStatus();
 1911104    }
 105
 106    private void RefreshActiveStatus()
 107    {
 5537108        if (activeOn)
 257109            activeOn.gameObject.SetActive(isOn);
 110
 5537111        if (activeOff)
 257112            activeOff.gameObject.SetActive(!isOn);
 113
 5537114        if (text != null && model.changeTextColorOnSelect)
 4276115            text.color = isOn ? textColorOnSelected : textColorOnUnselected;
 5537116    }
 117
 118    private void ToggleChanged(bool isOn)
 119    {
 1419120        this.isOn = isOn;
 1419121        OnSelectedChanged?.Invoke(isOn, model.id, model.text);
 36122    }
 123
 124    public void Configure(ToggleComponentModel newModel)
 125    {
 1750126        model = newModel;
 1750127        RefreshControl();
 1750128    }
 129
 130    public override void RefreshControl()
 131    {
 1752132        if (model == null)
 0133            return;
 134
 1752135        id = model.id;
 1752136        isOn = model.isOn;
 1752137        SetText(model.text);
 1752138        SetTextActive(model.isTextActive);
 1752139        SetChangeTextColorOnSelect(model.changeTextColorOnSelect);
 1752140    }
 141
 142    public void SetText(string newText)
 143    {
 1753144        model.text = newText;
 145
 1753146        if (text == null)
 0147            return;
 148
 1753149        text.text = newText;
 1753150    }
 151
 152    public override void Dispose()
 153    {
 1924154        base.Dispose();
 155
 1924156        toggle.onValueChanged.RemoveAllListeners();
 1924157    }
 158
 2159    public bool IsInteractable() { return toggle.interactable; }
 160
 4161    public void SetInteractable(bool isActive) { toggle.interactable = isActive; }
 162
 163    public void SetTextActive(bool isActive)
 164    {
 1754165        model.isTextActive = isActive;
 1754166        text.gameObject.SetActive(isActive);
 1754167    }
 168
 169    public void SetIsOnWithoutNotify(bool isOn)
 170    {
 319171        model.isOn = isOn;
 172
 319173        if (toggle == null)
 0174            return;
 175
 319176        toggle.SetIsOnWithoutNotify(isOn);
 319177        RefreshActiveStatus();
 319178    }
 179
 3504180    public void SetChangeTextColorOnSelect(bool changeTextColorOnSelect) { model.changeTextColorOnSelect = changeTextCol
 181}