< Summary

Class:BaseComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/BaseComponentView.cs
Covered lines:39
Uncovered lines:5
Coverable lines:44
Total lines:187
Line coverage:88.6% (39 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
 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 each time the component is disabled.
 36    /// </summary>
 37    void OnDisable();
 38
 39    /// <summary>
 40    /// It is called just after the UI component has been initialized.
 41    /// </summary>
 42    void Start();
 43
 44    /// <summary>
 45    /// It is called once per frame.
 46    /// </summary>
 47    void Update();
 48
 49    /// <summary>
 50    /// Updates the UI component with the current model configuration.
 51    /// </summary>
 52    void RefreshControl();
 53
 54    /// <summary>
 55    /// Shows the UI component.
 56    /// </summary>
 57    /// <param name="instant">True for not apply progressive animation.</param>
 58    void Show(bool instant = false);
 59
 60    /// <summary>
 61    /// Hides the UI component.
 62    /// </summary>
 63    /// <param name="instant">True for not apply progressive animation.</param>
 64    void Hide(bool instant = false);
 65
 66    /// <summary>
 67    /// It is called when the focus is set into the component.
 68    /// </summary>
 69    void OnFocus();
 70
 71    /// <summary>
 72    /// It is called when the focus is lost from the component.
 73    /// </summary>
 74    void OnLoseFocus();
 75
 76    /// <summary>
 77    /// It is called just after the screen size has changed.
 78    /// </summary>
 79    void OnScreenSizeChanged();
 80}
 81
 82public interface IComponentModelConfig<T> where T : BaseComponentModel
 83{
 84    /// <summary>
 85    /// Fill the model and updates the component with this data.
 86    /// </summary>
 87    /// <param name="newModel">Data to configure the component.</param>
 88    void Configure(T newModel);
 89}
 90
 91public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView
 92{
 93    internal BaseComponentModel baseModel;
 94    public ShowHideAnimator showHideAnimator;
 95
 1596    public virtual bool isVisible { get; private set; }
 97    protected bool isDestroyed = false;
 98
 99    public event Action<bool> onFocused;
 0100    public bool isFocused { get; private set; }
 101
 102    public virtual void Awake()
 103    {
 14210104        showHideAnimator = GetComponent<ShowHideAnimator>();
 14210105        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 14210106    }
 107
 43638108    public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); }
 109
 44152110    public virtual void OnDisable() { OnLoseFocus(); }
 111
 3172112    public virtual void Start() { }
 113
 127221114    public virtual void Update() { }
 115
 116    public abstract void RefreshControl();
 117
 118    public virtual void Show(bool instant = false)
 119    {
 122120        if (showHideAnimator == null)
 35121            return;
 122
 87123        showHideAnimator.Show(instant);
 87124        isVisible = true;
 87125    }
 126
 127    public virtual void Hide(bool instant = false)
 128    {
 352129        if (showHideAnimator == null)
 39130            return;
 131
 313132        showHideAnimator.Hide(instant);
 313133        isVisible = false;
 313134    }
 135
 136    public virtual void OnFocus()
 137    {
 42138        isFocused = true;
 42139        onFocused?.Invoke(true);
 2140    }
 141
 142    public virtual void OnLoseFocus()
 143    {
 22118144        isFocused = false;
 22118145        onFocused?.Invoke(false);
 110146    }
 147
 385148    public virtual void OnScreenSizeChanged() { }
 149
 150    public virtual void Dispose()
 151    {
 20172152        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 153
 20172154        if (!isDestroyed)
 6099155            Destroy(gameObject);
 20172156    }
 157
 12158    public virtual void OnPointerEnter(PointerEventData eventData) { OnFocus(); }
 159
 12160    public virtual void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); }
 161
 162    private void OnDestroy()
 163    {
 14070164        isDestroyed = true;
 14070165        Dispose();
 14070166    }
 167
 168    internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 169    {
 0170        if (!gameObject.activeInHierarchy)
 0171            return;
 172
 0173        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 0174    }
 175
 176    internal IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 177    {
 21819178        yield return null;
 448179        OnScreenSizeChanged();
 448180    }
 181
 182    public static T Create<T>(string resourceName) where T : BaseComponentView
 183    {
 559184        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 559185        return buttonComponentView;
 186    }
 187}