< 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:205
Line coverage:100% (5 of 5)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:4
Method coverage:100% (4 of 4)

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 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]
 461915        protected TModel model { get; private set; } = new ();
 16
 17        public void SetModel(TModel newModel)
 18        {
 11119            if (!Equals(model, newModel))
 20            {
 11021                model = newModel;
 11022                RefreshControl();
 23            }
 11124        }
 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;
 88    public virtual bool isVisible { get; private set; }
 89    public bool isFocused { get; private set; }
 90    public bool isViewEnabled { get; set; } = true;
 91    public event Action<bool> onFocused;
 92
 93    public virtual void Dispose()
 94    {
 95        DataStore.i.screen.size.OnChange -= OnScreenSizeModified;
 96
 97        if (!isDestroyed && gameObject)
 98            Utils.SafeDestroy(gameObject);
 99    }
 100
 101    public virtual void Awake()
 102    {
 103        showHideAnimator = GetComponent<ShowHideAnimator>();
 104        DataStore.i.screen.size.OnChange += OnScreenSizeModified;
 105    }
 106
 107    public virtual void OnEnable()
 108    {
 109        if(!isViewEnabled) return;
 110
 111        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 112    }
 113
 114    public virtual void OnDisable()
 115    {
 116        if(!isViewEnabled) return;
 117
 118        OnLoseFocus();
 119    }
 120
 121    private void OnDestroy()
 122    {
 123        isDestroyed = true;
 124        Dispose();
 125    }
 126
 127    public abstract void RefreshControl();
 128
 129    public virtual void Show(bool instant = false)
 130    {
 131        if (!isViewEnabled || !showHideAnimator)
 132            return;
 133
 134        showHideAnimator.Show(instant);
 135        isVisible = true;
 136    }
 137
 138    public virtual void Hide(bool instant = false)
 139    {
 140        if (!isViewEnabled || !showHideAnimator)
 141            return;
 142
 143        showHideAnimator.Hide(instant);
 144        isVisible = false;
 145    }
 146
 147    public virtual void OnFocus()
 148    {
 149        if(!isViewEnabled) return;
 150
 151        isFocused = true;
 152        onFocused?.Invoke(true);
 153    }
 154
 155    public virtual void OnLoseFocus()
 156    {
 157        if(!isViewEnabled) return;
 158
 159        isFocused = false;
 160        onFocused?.Invoke(false);
 161    }
 162
 163    public virtual void OnScreenSizeChanged() { }
 164
 165    public virtual void OnPointerEnter(PointerEventData eventData)
 166    {
 167        if(!isViewEnabled) return;
 168
 169        OnFocus();
 170    }
 171
 172    public virtual void OnPointerExit(PointerEventData eventData)
 173    {
 174        if(!isViewEnabled) return;
 175
 176        OnLoseFocus();
 177    }
 178
 179    private void OnScreenSizeModified(Vector2Int current, Vector2Int previous)
 180    {
 181        if (!isViewEnabled || !gameObject.activeInHierarchy)
 182            return;
 183
 184        StartCoroutine(RaiseOnScreenSizeChangedAfterDelay());
 185    }
 186
 187    private IEnumerator RaiseOnScreenSizeChangedAfterDelay()
 188    {
 189        yield return null;
 190
 191        if(isViewEnabled)
 192            OnScreenSizeChanged();
 193    }
 194
 195    public static T Create<T>(string resourceName) where T: BaseComponentView
 196    {
 197        T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>();
 198        return buttonComponentView;
 199    }
 200
 201#if UNITY_EDITOR
 202    public static T CreateUIComponentFromAssetDatabase<T>(string assetName) where T: BaseComponentView =>
 203        Instantiate(AssetDatabase.LoadAssetAtPath<T>($"Assets/UIComponents/Prefabs/{assetName}.prefab"));
 204#endif
 205}