< Summary

Class:BaseComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/BaseComponentView.cs
Covered lines:51
Uncovered lines:4
Coverable lines:55
Total lines:205
Line coverage:92.7% (51 of 55)
Covered branches:0
Total branches:0
Covered methods:22
Total methods:23
Method coverage:95.6% (22 of 23)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseComponentView()0%110100%
Dispose()0%330100%
Awake()0%110100%
OnEnable()0%2.062075%
OnDisable()0%2.062075%
OnDestroy()0%110100%
Show(...)0%330100%
Hide(...)0%330100%
OnFocus()0%3.073080%
OnLoseFocus()0%3.073080%
OnScreenSizeChanged()0%110100%
OnPointerEnter(...)0%2.062075%
OnPointerExit(...)0%2.062075%
OnScreenSizeModified(...)0%12300%
RaiseOnScreenSizeChangedAfterDelay()0%440100%
Create[T](...)0%110100%
CreateUIComponentFromAssetDatabase[T](...)0%110100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using System;
 4using System.Collections;
 5using UnityEditor;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8
 9namespace UIComponents.Scripts.Components
 10{
 11    public abstract class BaseComponentView<TModel> : BaseComponentView, IBaseComponentView<TModel>
 12        where TModel: IEquatable<TModel>, new()
 13    {
 14        [field: SerializeField]
 15        protected TModel model { get; private set; } = new ();
 16
 17        public void SetModel(TModel newModel)
 18        {
 19            if (!Equals(model, newModel))
 20            {
 21                model = newModel;
 22                RefreshControl();
 23            }
 24        }
 25    }
 26}
 27
 28public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable
 29{
 30    /// <summary>
 31    /// It will inform if the UI Component is currently visible or not.
 32    /// </summary>
 33    bool isVisible { get; }
 34
 35    /// <summary>
 36    /// It will be triggered when UI Component is focused.
 37    /// </summary>
 38    event Action<bool> onFocused;
 39
 40    /// <summary>
 41    /// It will inform if the UI Component is focused or not.
 42    /// </summary>
 43    bool isFocused { get; }
 44
 45    /// <summary>
 46    /// Updates the UI component with the current model configuration.
 47    /// </summary>
 48    void RefreshControl();
 49
 50    /// <summary>
 51    /// Shows the UI component.
 52    /// </summary>
 53    /// <param name="instant">True for not apply progressive animation.</param>
 54    void Show(bool instant = false);
 55
 56    /// <summary>
 57    /// Hides the UI component.
 58    /// </summary>
 59    /// <param name="instant">True for not apply progressive animation.</param>
 60    void Hide(bool instant = false);
 61
 62    /// <summary>
 63    /// It is called when the focus is set into the component.
 64    /// </summary>
 65    void OnFocus();
 66
 67    /// <summary>
 68    /// It is called when the focus is lost from the component.
 69    /// </summary>
 70    void OnLoseFocus();
 71}
 72
 73public interface IComponentModelConfig<T> where T: BaseComponentModel
 74{
 75    /// <summary>
 76    /// Fill the model and updates the component with this data.
 77    /// </summary>
 78    /// <param name="newModel">Data to configure the component.</param>
 79    void Configure(T newModel);
 80}
 81
 82public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView
 83{
 84    internal BaseComponentModel baseModel;
 85    private bool isDestroyed;
 86
 87    public ShowHideAnimator showHideAnimator;
 58488    public virtual bool isVisible { get; private set; }
 2286289    public bool isFocused { get; private set; }
 10787990    public bool isViewEnabled { get; set; } = true;
 91    public event Action<bool> onFocused;
 92
 93    public virtual void Dispose()
 94    {
 2571295        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 96
 2571297        if (!isDestroyed && gameObject)
 486398            Utils.SafeDestroy(gameObject);
 2571299    }
 100
 101    public virtual void Awake()
 102    {
 20986103        showHideAnimator = GetComponent<ShowHideAnimator>();
 20986104        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 20986105    }
 106
 107    public virtual void OnEnable()
 108    {
 22448109        if(!isViewEnabled) return;
 110
 22448111        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 22448112    }
 113
 114    public virtual void OnDisable()
 115    {
 22818116        if(!isViewEnabled) return;
 117
 22818118        OnLoseFocus();
 22818119    }
 120
 121    private void OnDestroy()
 122    {
 21009123        isDestroyed = true;
 21009124        Dispose();
 21006125    }
 126
 127    public abstract void RefreshControl();
 128
 129    public virtual void Show(bool instant = false)
 130    {
 87131        if (!isViewEnabled || !showHideAnimator)
 36132            return;
 133
 51134        showHideAnimator.Show(instant);
 51135        isVisible = true;
 51136    }
 137
 138    public virtual void Hide(bool instant = false)
 139    {
 553140        if (!isViewEnabled || !showHideAnimator)
 45141            return;
 142
 508143        showHideAnimator.Hide(instant);
 508144        isVisible = false;
 508145    }
 146
 147    public virtual void OnFocus()
 148    {
 45149        if(!isViewEnabled) return;
 150
 45151        isFocused = true;
 45152        onFocused?.Invoke(true);
 2153    }
 154
 155    public virtual void OnLoseFocus()
 156    {
 22811157        if(!isViewEnabled) return;
 158
 22811159        isFocused = false;
 22811160        onFocused?.Invoke(false);
 184161    }
 162
 484163    public virtual void OnScreenSizeChanged() { }
 164
 165    public virtual void OnPointerEnter(PointerEventData eventData)
 166    {
 7167        if(!isViewEnabled) return;
 168
 7169        OnFocus();
 7170    }
 171
 172    public virtual void OnPointerExit(PointerEventData eventData)
 173    {
 7174        if(!isViewEnabled) return;
 175
 7176        OnLoseFocus();
 7177    }
 178
 179    private void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 180    {
 0181        if (!isViewEnabled || !gameObject.activeInHierarchy)
 0182            return;
 183
 0184        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 0185    }
 186
 187    private IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 188    {
 22448189        yield return null;
 190
 484191        if(isViewEnabled)
 484192            OnScreenSizeChanged();
 484193    }
 194
 195    public static T Create<T>(string resourceName) where T: BaseComponentView
 196    {
 280197        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 280198        return buttonComponentView;
 199    }
 200
 201#if UNITY_EDITOR
 202    public static T CreateUIComponentFromAssetDatabase<T>(string assetName) where T: BaseComponentView =>
 193203        Instantiate(AssetDatabase.LoadAssetAtPath<T>($"Assets/UIComponents/Prefabs/{assetName}.prefab"));
 204#endif
 205}