< Summary

Class:BaseComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/BaseComponentView.cs
Covered lines:40
Uncovered lines:4
Coverable lines:44
Total lines:176
Line coverage:90.9% (40 of 44)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnEnable()0%110100%
OnDisable()0%110100%
Start()0%110100%
Update()0%110100%
Show(...)0%220100%
Hide(...)0%220100%
OnFocus()0%220100%
OnLoseFocus()0%220100%
OnScreenSizeChanged()0%110100%
Dispose()0%220100%
OnPointerEnter(...)0%110100%
OnPointerExit(...)0%110100%
OnDestroy()0%110100%
OnScreenSizeModified(...)0%6200%
RaiseOnScreenSizeChangedAfterDelay()0%330100%
Create[T](...)0%110100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Collections;
 4using UnityEngine;
 5using UnityEngine.EventSystems;
 6
 7namespace UIComponents.Scripts.Components
 8{
 9    public abstract class BaseComponentView<TModel> : BaseComponentView, IBaseComponentView<TModel>
 10        where TModel : IEquatable<TModel>, new()
 11    {
 12        [field: SerializeField]
 13        protected TModel model { get; private set; } = new ();
 14
 15        public void SetModel(TModel newModel)
 16        {
 17            if (!Equals(model, newModel))
 18            {
 19                model = newModel;
 20                RefreshControl();
 21            }
 22        }
 23    }
 24}
 25
 26public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable
 27{
 28    /// <summary>
 29    /// It will inform if the UI Component is currently visible or not.
 30    /// </summary>
 31    bool isVisible { get; }
 32
 33    /// <summary>
 34    /// It will be triggered when UI Component is focused.
 35    /// </summary>
 36    event Action<bool> onFocused;
 37
 38    /// <summary>
 39    /// It will inform if the UI Component is focused or not.
 40    /// </summary>
 41    bool isFocused { get; }
 42
 43    /// <summary>
 44    /// Updates the UI component with the current model configuration.
 45    /// </summary>
 46    void RefreshControl();
 47
 48    /// <summary>
 49    /// Shows the UI component.
 50    /// </summary>
 51    /// <param name="instant">True for not apply progressive animation.</param>
 52    void Show(bool instant = false);
 53
 54    /// <summary>
 55    /// Hides the UI component.
 56    /// </summary>
 57    /// <param name="instant">True for not apply progressive animation.</param>
 58    void Hide(bool instant = false);
 59
 60    /// <summary>
 61    /// It is called when the focus is set into the component.
 62    /// </summary>
 63    void OnFocus();
 64
 65    /// <summary>
 66    /// It is called when the focus is lost from the component.
 67    /// </summary>
 68    void OnLoseFocus();
 69}
 70
 71public interface IComponentModelConfig<T> where T : BaseComponentModel
 72{
 73    /// <summary>
 74    /// Fill the model and updates the component with this data.
 75    /// </summary>
 76    /// <param name="newModel">Data to configure the component.</param>
 77    void Configure(T newModel);
 78}
 79
 80public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView
 81{
 82    internal BaseComponentModel baseModel;
 83    public ShowHideAnimator showHideAnimator;
 84
 42085    public virtual bool isVisible { get; private set; }
 86    protected bool isDestroyed = false;
 87
 88    public event Action<bool> onFocused;
 2238889    public bool isFocused { get; private set; }
 90
 91    public virtual void Awake()
 92    {
 1432193        showHideAnimator = GetComponent<ShowHideAnimator>();
 1432194        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 1432195    }
 96
 4364297    public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); }
 98
 4459299    public virtual void OnDisable() { OnLoseFocus(); }
 100
 3210101    public virtual void Start() { }
 102
 79937103    public virtual void Update() { }
 104
 105    public abstract void RefreshControl();
 106
 107    public virtual void Show(bool instant = false)
 108    {
 126109        if (showHideAnimator == null)
 37110            return;
 111
 89112        showHideAnimator.Show(instant);
 89113        isVisible = true;
 89114    }
 115
 116    public virtual void Hide(bool instant = false)
 117    {
 355118        if (showHideAnimator == null)
 39119            return;
 120
 316121        showHideAnimator.Hide(instant);
 316122        isVisible = false;
 316123    }
 124
 125    public virtual void OnFocus()
 126    {
 43127        isFocused = true;
 43128        onFocused?.Invoke(true);
 2129    }
 130
 131    public virtual void OnLoseFocus()
 132    {
 22339133        isFocused = false;
 22339134        onFocused?.Invoke(false);
 110135    }
 136
 471137    public virtual void OnScreenSizeChanged() { }
 138
 139    public virtual void Dispose()
 140    {
 20417141        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 142
 20417143        if (!isDestroyed)
 6228144            Destroy(gameObject);
 20417145    }
 146
 12147    public virtual void OnPointerEnter(PointerEventData eventData) { OnFocus(); }
 148
 12149    public virtual void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); }
 150
 151    private void OnDestroy()
 152    {
 14186153        isDestroyed = true;
 14186154        Dispose();
 14186155    }
 156
 157    internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 158    {
 0159        if (!gameObject.activeInHierarchy)
 0160            return;
 161
 0162        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 0163    }
 164
 165    internal IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 166    {
 21821167        yield return null;
 444168        OnScreenSizeChanged();
 444169    }
 170
 171    public static T Create<T>(string resourceName) where T : BaseComponentView
 172    {
 571173        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 571174        return buttonComponentView;
 175    }
 176}