< Summary

Class:ButtonComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Button/ButtonComponentView.cs
Covered lines:17
Uncovered lines:5
Coverable lines:22
Total lines:87
Line coverage:77.2% (17 of 22)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Configure(...)0%110100%
RefreshControl()0%2.032080%
IsInteractable()0%2100%
SetInteractable(...)0%110100%
SetText(...)0%2.032080%
SetIcon(...)0%2.022083.33%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Button/ButtonComponentView.cs

#LineLine coverage
 1using TMPro;
 2using UnityEngine;
 3using UnityEngine.UI;
 4
 5public interface IButtonComponentView
 6{
 7    /// <summary>
 8    /// Event that will be triggered when the button is clicked.
 9    /// </summary>
 10    Button.ButtonClickedEvent onClick { get; }
 11
 12    /// <summary>
 13    /// Set the button text.
 14    /// </summary>
 15    /// <param name="newText">New text.</param>
 16    void SetText(string newText);
 17
 18    /// <summary>
 19    /// Set the button icon.
 20    /// </summary>
 21    /// <param name="newIcon">New Icon. Null for hide the icon.</param>
 22    void SetIcon(Sprite newIcon);
 23
 24    /// <summary>
 25    /// Set the button clickable or not.
 26    /// </summary>
 27    /// <param name="isInteractable">Clickable or not</param>
 28    void SetInteractable(bool isInteractable);
 29
 30    /// <summary>
 31    /// Return if the button is Interactable or not
 32    /// </summary>
 33    bool IsInteractable();
 34}
 35
 36public class ButtonComponentView : BaseComponentView, IButtonComponentView, IComponentModelConfig
 37{
 38    [Header("Prefab References")]
 39    [SerializeField] internal Button button;
 40    [SerializeField] internal TMP_Text text;
 41    [SerializeField] internal Image icon;
 42
 43    [Header("Configuration")]
 44    [SerializeField] internal ButtonComponentModel model;
 45
 046    public Button.ButtonClickedEvent onClick => button?.onClick;
 47
 48    public void Configure(BaseComponentModel newModel)
 49    {
 150        model = (ButtonComponentModel)newModel;
 151        RefreshControl();
 152    }
 53
 54    public override void RefreshControl()
 55    {
 156        if (model == null)
 057            return;
 58
 159        SetText(model.text);
 160        SetIcon(model.icon);
 161    }
 62
 063    public bool IsInteractable() { return button.interactable; }
 64
 3065    public void SetInteractable(bool isActive) { button.interactable = isActive;}
 66
 67    public void SetText(string newText)
 68    {
 669        model.text = newText;
 70
 671        if (text == null)
 072            return;
 73
 674        text.text = newText;
 675    }
 76
 77    public void SetIcon(Sprite newIcon)
 78    {
 379        model.icon = newIcon;
 80
 381        if (icon == null)
 082            return;
 83
 384        icon.gameObject.SetActive(newIcon != null);
 385        icon.sprite = newIcon;
 386    }
 87}