< Summary

Class:BaseComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/BaseComponentView.cs
Covered lines:36
Uncovered lines:7
Coverable lines:43
Total lines:179
Line coverage:83.7% (36 of 43)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnEnable()0%110100%
Start()0%110100%
Update()0%110100%
Show(...)0%2.032080%
Hide(...)0%2.032080%
OnFocus()0%2.152066.67%
OnLoseFocus()0%2.152066.67%
OnScreenSizeChanged()0%110100%
Dispose()0%220100%
OnPointerEnter(...)0%110100%
OnPointerExit(...)0%110100%
OnDestroy()0%110100%
OnScreenSizeModified(...)0%2.062075%
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
 7public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable
 8{
 9    /// <summary>
 10    /// It will inform if the UI Component is currently visible or not.
 11    /// </summary>
 12    bool isVisible { get; }
 13
 14    /// <summary>
 15    /// It will be triggered when UI Component is focused.
 16    /// </summary>
 17    event Action<bool> onFocused;
 18
 19    /// <summary>
 20    /// It will inform if the UI Component is focused or not.
 21    /// </summary>
 22    bool isFocused { get; }
 23
 24    /// <summary>
 25    /// It is called at the beginning of the UI component lifecycle.
 26    /// </summary>
 27    void Awake();
 28
 29    /// <summary>
 30    /// It is called each time the component is enabled.
 31    /// </summary>
 32    void OnEnable();
 33
 34    /// <summary>
 35    /// It is called just after the UI component has been initialized.
 36    /// </summary>
 37    void Start();
 38
 39    /// <summary>
 40    /// It is called once per frame.
 41    /// </summary>
 42    void Update();
 43
 44    /// <summary>
 45    /// Updates the UI component with the current model configuration.
 46    /// </summary>
 47    void RefreshControl();
 48
 49    /// <summary>
 50    /// Shows the UI component.
 51    /// </summary>
 52    /// <param name="instant">True for not apply progressive animation.</param>
 53    void Show(bool instant = false);
 54
 55    /// <summary>
 56    /// Hides the UI component.
 57    /// </summary>
 58    /// <param name="instant">True for not apply progressive animation.</param>
 59    void Hide(bool instant = false);
 60
 61    /// <summary>
 62    /// It is called when the focus is set into the component.
 63    /// </summary>
 64    void OnFocus();
 65
 66    /// <summary>
 67    /// It is called when the focus is lost from the component.
 68    /// </summary>
 69    void OnLoseFocus();
 70
 71    /// <summary>
 72    /// It is called just after the screen size has changed.
 73    /// </summary>
 74    void OnScreenSizeChanged();
 75}
 76
 77public interface IComponentModelConfig
 78{
 79    /// <summary>
 80    /// Fill the model and updates the component with this data.
 81    /// </summary>
 82    /// <param name="newModel">Data to configure the component.</param>
 83    void Configure(BaseComponentModel newModel);
 84}
 85
 86public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView
 87{
 88    internal BaseComponentModel baseModel;
 89    internal ShowHideAnimator showHideAnimator;
 90
 091    public bool isVisible { get; private set; }
 92    private bool isDestroyed = false;
 93
 94    public event Action<bool> onFocused;
 095    public bool isFocused { get; private set; }
 96
 97    public virtual void Awake()
 98    {
 405499        showHideAnimator = GetComponent<ShowHideAnimator>();
 4054100        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 4054101    }
 102
 8536103    public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); }
 104
 1251105    public virtual void Start() { }
 106
 2425107    public virtual void Update() { }
 108
 109    public abstract void RefreshControl();
 110
 111    public virtual void Show(bool instant = false)
 112    {
 20113        if (showHideAnimator == null)
 0114            return;
 115
 20116        showHideAnimator.Show(instant);
 20117        isVisible = true;
 20118    }
 119
 120    public virtual void Hide(bool instant = false)
 121    {
 71122        if (showHideAnimator == null)
 0123            return;
 124
 71125        showHideAnimator.Hide(instant);
 71126        isVisible = false;
 71127    }
 128
 129    public virtual void OnFocus()
 130    {
 33131        isFocused = true;
 33132        onFocused?.Invoke(true);
 0133    }
 134
 135    public virtual void OnLoseFocus()
 136    {
 33137        isFocused = false;
 33138        onFocused?.Invoke(false);
 0139    }
 140
 27141    public virtual void OnScreenSizeChanged() { }
 142
 143    public virtual void Dispose()
 144    {
 6592145        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 6592146        if (!isDestroyed)
 2538147            Destroy(gameObject);
 6592148    }
 149
 8150    public void OnPointerEnter(PointerEventData eventData) { OnFocus(); }
 151
 8152    public void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); }
 153
 154    private void OnDestroy()
 155    {
 4054156        isDestroyed = true;
 4054157        Dispose();
 4054158    }
 159
 160    internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 161    {
 1162        if (!gameObject.activeInHierarchy)
 0163            return;
 164
 1165        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 1166    }
 167
 168    internal IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 169    {
 4269170        yield return null;
 31171        OnScreenSizeChanged();
 31172    }
 173
 174    internal static T Create<T>(string resourceName) where T : BaseComponentView
 175    {
 239176        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 239177        return buttonComponentView;
 178    }
 179}