< 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:89
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%2100%
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<ButtonComponentModel>
 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
 878546    public Button.ButtonClickedEvent onClick => button?.onClick;
 47
 48    public void Configure(ButtonComponentModel newModel)
 49    {
 150        model = 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
 63    public bool IsInteractable() =>
 064        button.interactable;
 65
 66    public void SetInteractable(bool isActive) =>
 067        button.interactable = isActive;
 68
 69    public void SetText(string newText)
 70    {
 671        model.text = newText;
 72
 673        if (text == null)
 074            return;
 75
 676        text.text = newText;
 677    }
 78
 79    public void SetIcon(Sprite newIcon)
 80    {
 381        model.icon = newIcon;
 82
 383        if (icon == null)
 084            return;
 85
 386        icon.gameObject.SetActive(newIcon != null);
 387        icon.sprite = newIcon;
 388    }
 89}