< 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

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    {
 9177        get => model.id;
 46678        set => model.id = value;
 79    }
 80
 81    public bool isOn
 82    {
 135083        get => toggle.isOn;
 84        set
 85        {
 91586            model.isOn = value;
 87
 91588            if (toggle == null)
 189                return;
 90
 91491            toggle.isOn = value;
 91492            RefreshActiveStatus();
 91493        }
 94    }
 95
 096    public string title => text.text;
 97
 98    public override void Awake()
 99    {
 576100        base.Awake();
 101
 576102        toggle.onValueChanged.AddListener(ToggleChanged);
 576103        RefreshActiveStatus();
 576104    }
 105
 106    private void RefreshActiveStatus()
 107    {
 1497108        if (activeOn)
 184109            activeOn.gameObject.SetActive(isOn);
 110
 1497111        if (activeOff)
 184112            activeOff.gameObject.SetActive(!isOn);
 113
 1497114        if (text != null && model.changeTextColorOnSelect)
 953115            text.color = isOn ? textColorOnSelected : textColorOnUnselected;
 1497116    }
 117
 118    private void ToggleChanged(bool isOn)
 119    {
 373120        this.isOn = isOn;
 373121        OnSelectedChanged?.Invoke(isOn, model.id, model.text);
 17122    }
 123
 124    public void Configure(ToggleComponentModel newModel)
 125    {
 464126        model = newModel;
 464127        RefreshControl();
 464128    }
 129
 130    public override void RefreshControl()
 131    {
 466132        if (model == null)
 0133            return;
 134
 466135        id = model.id;
 466136        isOn = model.isOn;
 466137        SetText(model.text);
 466138        SetTextActive(model.isTextActive);
 466139        SetChangeTextColorOnSelect(model.changeTextColorOnSelect);
 466140    }
 141
 142    public void SetText(string newText)
 143    {
 467144        model.text = newText;
 145
 467146        if (text == null)
 0147            return;
 148
 467149        text.text = newText;
 467150    }
 151
 152    public override void Dispose()
 153    {
 589154        base.Dispose();
 155
 589156        toggle.onValueChanged.RemoveAllListeners();
 589157    }
 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    {
 468165        model.isTextActive = isActive;
 468166        text.gameObject.SetActive(isActive);
 468167    }
 168
 169    public void SetIsOnWithoutNotify(bool isOn)
 170    {
 7171        model.isOn = isOn;
 172
 7173        if (toggle == null)
 0174            return;
 175
 7176        toggle.SetIsOnWithoutNotify(isOn);
 7177        RefreshActiveStatus();
 7178    }
 179
 932180    public void SetChangeTextColorOnSelect(bool changeTextColorOnSelect) { model.changeTextColorOnSelect = changeTextCol
 181}