< Summary

Class:UIComponents.Scripts.Components.BaseComponentView[TModel]
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/BaseComponentView.cs
Covered lines:5
Uncovered lines:0
Coverable lines:5
Total lines:176
Line coverage:100% (5 of 5)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseComponentView()0%110100%
SetModel(...)0%220100%

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]
 62613        protected TModel model { get; private set; } = new ();
 14
 15        public void SetModel(TModel newModel)
 16        {
 3717            if (!Equals(model, newModel))
 18            {
 3719                model = newModel;
 3720                RefreshControl();
 21            }
 3722        }
 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
 85    public virtual bool isVisible { get; private set; }
 86    protected bool isDestroyed = false;
 87
 88    public event Action<bool> onFocused;
 89    public bool isFocused { get; private set; }
 90
 91    public virtual void Awake()
 92    {
 93        showHideAnimator = GetComponent<ShowHideAnimator>();
 94        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 95    }
 96
 97    public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); }
 98
 99    public virtual void OnDisable() { OnLoseFocus(); }
 100
 101    public virtual void Start() { }
 102
 103    public virtual void Update() { }
 104
 105    public abstract void RefreshControl();
 106
 107    public virtual void Show(bool instant = false)
 108    {
 109        if (showHideAnimator == null)
 110            return;
 111
 112        showHideAnimator.Show(instant);
 113        isVisible = true;
 114    }
 115
 116    public virtual void Hide(bool instant = false)
 117    {
 118        if (showHideAnimator == null)
 119            return;
 120
 121        showHideAnimator.Hide(instant);
 122        isVisible = false;
 123    }
 124
 125    public virtual void OnFocus()
 126    {
 127        isFocused = true;
 128        onFocused?.Invoke(true);
 129    }
 130
 131    public virtual void OnLoseFocus()
 132    {
 133        isFocused = false;
 134        onFocused?.Invoke(false);
 135    }
 136
 137    public virtual void OnScreenSizeChanged() { }
 138
 139    public virtual void Dispose()
 140    {
 141        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 142
 143        if (!isDestroyed)
 144            Destroy(gameObject);
 145    }
 146
 147    public virtual void OnPointerEnter(PointerEventData eventData) { OnFocus(); }
 148
 149    public virtual void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); }
 150
 151    private void OnDestroy()
 152    {
 153        isDestroyed = true;
 154        Dispose();
 155    }
 156
 157    internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 158    {
 159        if (!gameObject.activeInHierarchy)
 160            return;
 161
 162        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 163    }
 164
 165    internal IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 166    {
 167        yield return null;
 168        OnScreenSizeChanged();
 169    }
 170
 171    public static T Create<T>(string resourceName) where T : BaseComponentView
 172    {
 173        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 174        return buttonComponentView;
 175    }
 176}