< Summary

Class:ButtonComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Button/ButtonComponentView.cs
Covered lines:29
Uncovered lines:7
Coverable lines:36
Total lines:113
Line coverage:80.5% (29 of 36)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PostInitialization()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.022083.33%
Dispose()0%2.032080%
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; set; }
 11
 12    /// <summary>
 13    /// Fill the model and updates the button with this data.
 14    /// </summary>
 15    /// <param name="model">Data to configure the button.</param>
 16    void Configure(ButtonComponentModel model);
 17
 18    /// <summary>
 19    /// Set the button text.
 20    /// </summary>
 21    /// <param name="newText">New text.</param>
 22    void SetText(string newText);
 23
 24    /// <summary>
 25    /// Set the button icon.
 26    /// </summary>
 27    /// <param name="newIcon">New Icon. Null for hide the icon.</param>
 28    void SetIcon(Sprite newIcon);
 29}
 30
 31public class ButtonComponentView : BaseComponentView, IButtonComponentView
 32{
 33    [Header("Prefab References")]
 34    [SerializeField] internal Button button;
 35    [SerializeField] internal TMP_Text text;
 36    [SerializeField] internal Image icon;
 37
 38    [Header("Configuration")]
 39    [SerializeField] internal ButtonComponentModel model;
 40
 41    public Button.ButtonClickedEvent onClick
 42    {
 43        get
 44        {
 17745            if (button == null)
 046                return null;
 47
 17748            return button.onClick;
 49        }
 50        set
 51        {
 7952            model.onClick = value;
 53
 7954            if (button != null)
 55            {
 7956                button.onClick.RemoveAllListeners();
 7957                button.onClick.AddListener(() =>
 58                {
 059                    value?.Invoke();
 060                });
 61            }
 7962        }
 63    }
 64
 14665    public override void PostInitialization() { Configure(model); }
 66
 67    public virtual void Configure(ButtonComponentModel model)
 68    {
 7669        this.model = model;
 7670        RefreshControl();
 7671    }
 72
 73    public override void RefreshControl()
 74    {
 7975        if (model == null)
 076            return;
 77
 7978        SetText(model.text);
 7979        SetIcon(model.icon);
 7980        onClick = model.onClick;
 7981    }
 82
 83    public override void Dispose()
 84    {
 15385        base.Dispose();
 86
 15387        if (button == null)
 088            return;
 89
 15390        button.onClick.RemoveAllListeners();
 15391    }
 92
 93    public void SetText(string newText)
 94    {
 8295        model.text = newText;
 96
 8297        if (text == null)
 098            return;
 99
 82100        text.text = newText;
 82101    }
 102
 103    public void SetIcon(Sprite newIcon)
 104    {
 85105        model.icon = newIcon;
 106
 85107        if (icon == null)
 0108            return;
 109
 85110        icon.gameObject.SetActive(newIcon != null);
 85111        icon.sprite = newIcon;
 85112    }
 113}