< 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:175
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> where TModel : IEqua
 10    {
 11        [field: SerializeField]
 12        protected TModel model { get; private set; }
 13
 14        public void SetModel(TModel newModel)
 15        {
 16            if (!Equals(model, newModel))
 17            {
 18                model = newModel;
 19                RefreshControl();
 20            }
 21        }
 22    }
 23}
 24
 25public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable
 26{
 27    /// <summary>
 28    /// It will inform if the UI Component is currently visible or not.
 29    /// </summary>
 30    bool isVisible { get; }
 31
 32    /// <summary>
 33    /// It will be triggered when UI Component is focused.
 34    /// </summary>
 35    event Action<bool> onFocused;
 36
 37    /// <summary>
 38    /// It will inform if the UI Component is focused or not.
 39    /// </summary>
 40    bool isFocused { get; }
 41
 42    /// <summary>
 43    /// Updates the UI component with the current model configuration.
 44    /// </summary>
 45    void RefreshControl();
 46
 47    /// <summary>
 48    /// Shows the UI component.
 49    /// </summary>
 50    /// <param name="instant">True for not apply progressive animation.</param>
 51    void Show(bool instant = false);
 52
 53    /// <summary>
 54    /// Hides the UI component.
 55    /// </summary>
 56    /// <param name="instant">True for not apply progressive animation.</param>
 57    void Hide(bool instant = false);
 58
 59    /// <summary>
 60    /// It is called when the focus is set into the component.
 61    /// </summary>
 62    void OnFocus();
 63
 64    /// <summary>
 65    /// It is called when the focus is lost from the component.
 66    /// </summary>
 67    void OnLoseFocus();
 68}
 69
 70public interface IComponentModelConfig<T> where T : BaseComponentModel
 71{
 72    /// <summary>
 73    /// Fill the model and updates the component with this data.
 74    /// </summary>
 75    /// <param name="newModel">Data to configure the component.</param>
 76    void Configure(T newModel);
 77}
 78
 79public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView
 80{
 81    internal BaseComponentModel baseModel;
 82    public ShowHideAnimator showHideAnimator;
 83
 38984    public virtual bool isVisible { get; private set; }
 85    protected bool isDestroyed = false;
 86
 87    public event Action<bool> onFocused;
 2186588    public bool isFocused { get; private set; }
 89
 90    public virtual void Awake()
 91    {
 1388892        showHideAnimator = GetComponent<ShowHideAnimator>();
 1388893        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 1388894    }
 95
 4284896    public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); }
 97
 4354698    public virtual void OnDisable() { OnLoseFocus(); }
 99
 3071100    public virtual void Start() { }
 101
 165777102    public virtual void Update() { }
 103
 104    public abstract void RefreshControl();
 105
 106    public virtual void Show(bool instant = false)
 107    {
 123108        if (showHideAnimator == null)
 37109            return;
 110
 86111        showHideAnimator.Show(instant);
 86112        isVisible = true;
 86113    }
 114
 115    public virtual void Hide(bool instant = false)
 116    {
 329117        if (showHideAnimator == null)
 41118            return;
 119
 288120        showHideAnimator.Hide(instant);
 288121        isVisible = false;
 288122    }
 123
 124    public virtual void OnFocus()
 125    {
 43126        isFocused = true;
 43127        onFocused?.Invoke(true);
 2128    }
 129
 130    public virtual void OnLoseFocus()
 131    {
 21816132        isFocused = false;
 21816133        onFocused?.Invoke(false);
 110134    }
 135
 471136    public virtual void OnScreenSizeChanged() { }
 137
 138    public virtual void Dispose()
 139    {
 19633140        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 141
 19633142        if (!isDestroyed)
 5877143            Destroy(gameObject);
 19633144    }
 145
 12146    public virtual void OnPointerEnter(PointerEventData eventData) { OnFocus(); }
 147
 12148    public virtual void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); }
 149
 150    private void OnDestroy()
 151    {
 13753152        isDestroyed = true;
 13753153        Dispose();
 13753154    }
 155
 156    internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 157    {
 0158        if (!gameObject.activeInHierarchy)
 0159            return;
 160
 0161        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 0162    }
 163
 164    internal IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 165    {
 21424166        yield return null;
 444167        OnScreenSizeChanged();
 444168    }
 169
 170    public static T Create<T>(string resourceName) where T : BaseComponentView
 171    {
 571172        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 571173        return buttonComponentView;
 174    }
 175}