< Summary

Class:DCL.ExperiencesViewer.ExperiencesViewerComponentView
Assembly:ExperiencesViewer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExperiencesViewer/Scripts/ExperiencesViewerComponentView.cs
Covered lines:47
Uncovered lines:12
Coverable lines:59
Total lines:224
Line coverage:79.6% (47 of 59)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
RefreshControl()0%2100%
SetAvailableExperiences(...)0%220100%
AddAvailableExperience(...)0%110100%
RemoveAvailableExperience(...)0%330100%
GetAllAvailableExperiences()0%6200%
GetAvailableExperienceById(...)0%220100%
SetVisible(...)0%110100%
ShowUIHiddenToast()0%2.262060%
Dispose()0%110100%
OnSomeExperienceUIVisibilityChanged(...)0%220100%
OnSomeExperienceExecutionChanged(...)0%220100%
ConfigurePEXPool()0%220100%
ShowUIHiddenToastCoroutine()0%4.123050%
Create()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExperiencesViewer/Scripts/ExperiencesViewerComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6
 7namespace DCL.ExperiencesViewer
 8{
 9    public interface IExperiencesViewerComponentView
 10    {
 11        /// <summary>
 12        /// It will be triggered when the close button is clicked.
 13        /// </summary>
 14        event Action onCloseButtonPressed;
 15
 16        /// <summary>
 17        /// It will be triggered when the UI visibility of some experience changes.
 18        /// </summary>
 19        event Action<string, bool> onSomeExperienceUIVisibilityChanged;
 20
 21        /// <summary>
 22        /// It will be triggered when the execution of some experience changes.
 23        /// </summary>
 24        event Action<string, bool> onSomeExperienceExecutionChanged;
 25
 26        /// <summary>
 27        /// It represents the container transform of the component.
 28        /// </summary>
 29        Transform experienceViewerTransform { get; }
 30
 31        /// <summary>
 32        /// Set the available experiences component with a list of experiences.
 33        /// </summary>
 34        /// <param name="experiences">List of experiences (model) to be loaded.</param>
 35        void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences);
 36
 37        /// <summary>
 38        /// Add an experience into the available experiences component.
 39        /// </summary>
 40        /// <param name="experience">Experience to add.</param>
 41        void AddAvailableExperience(ExperienceRowComponentModel experience);
 42
 43        /// <summary>
 44        /// Remove an experience from the available experiences component.
 45        /// </summary>
 46        /// <param name="id">Experience id to remove.</param>
 47        void RemoveAvailableExperience(string id);
 48
 49        /// <summary>
 50        /// Get all experiences.
 51        /// </summary>
 52        /// <returns>A list of experiences.</returns>
 53        List<ExperienceRowComponentView> GetAllAvailableExperiences();
 54
 55        /// <summary>
 56        /// Get a specific experience.
 57        /// </summary>
 58        /// <param name="id">Id of the experience to search.</param>
 59        /// <returns>An experience.</returns>
 60        ExperienceRowComponentView GetAvailableExperienceById(string id);
 61
 62        /// <summary>
 63        /// Shows/Hides the game object of the Experiences Viewer.
 64        /// </summary>
 65        /// <param name="isActive">True to show it.</param>
 66        void SetVisible(bool isActive);
 67
 68        /// <summary>
 69        /// Shows the info toast for when the UI of an experience is hidden.
 70        /// </summary>
 71        void ShowUIHiddenToast();
 72    }
 73
 74    public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView
 75    {
 76        internal const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool";
 77        internal const int EXPERIENCES_POOL_PREWARM = 10;
 78        internal const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f;
 79
 80        [Header("Assets References")]
 81        [SerializeField] internal ExperienceRowComponentView experienceRowPrefab;
 82
 83        [Header("Prefab References")]
 84        [SerializeField] internal ButtonComponentView closeButton;
 85        [SerializeField] internal GridContainerComponentView availableExperiences;
 86        [SerializeField] internal ShowHideAnimator hiddenUIToastAnimator;
 87        public Color rowsBackgroundColor;
 88        public Color rowsOnHoverColor;
 89
 90        public event Action onCloseButtonPressed;
 91        public event Action<string, bool> onSomeExperienceUIVisibilityChanged;
 92        public event Action<string, bool> onSomeExperienceExecutionChanged;
 93
 94        internal Pool experiencesPool;
 95        internal Coroutine uiHiddenToastCoroutine;
 96
 097        public Transform experienceViewerTransform => transform;
 98
 99        public override void Awake()
 100        {
 10101            base.Awake();
 102
 10103            closeButton.onClick.AddListener(() => onCloseButtonPressed?.Invoke());
 104
 10105            ConfigurePEXPool();
 10106        }
 107
 108        public override void RefreshControl()
 109        {
 0110            availableExperiences.RefreshControl();
 0111        }
 112
 113        public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences)
 114        {
 4115            availableExperiences.ExtractItems();
 4116            experiencesPool.ReleaseAll();
 117
 4118            List<BaseComponentView> experiencesToAdd = new List<BaseComponentView>();
 24119            foreach (ExperienceRowComponentModel exp in experiences)
 120            {
 8121                AddAvailableExperience(exp);
 122            }
 4123        }
 124
 125        public void AddAvailableExperience(ExperienceRowComponentModel experience)
 126        {
 9127            experience.backgroundColor = rowsBackgroundColor;
 9128            experience.onHoverColor = rowsOnHoverColor;
 129
 9130            ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen
 9131            newRealmRow.Configure(experience);
 9132            newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged;
 9133            newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged;
 9134            newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged;
 9135            newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged;
 136
 9137            availableExperiences.AddItemWithResize(newRealmRow);
 9138        }
 139
 140        public void RemoveAvailableExperience(string id)
 141        {
 1142            ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems()
 1143                .Select(x => x as ExperienceRowComponentView)
 1144                .FirstOrDefault(x => x.model.id == id);
 145
 1146            if (experienceToRemove != null)
 1147                availableExperiences.RemoveItem(experienceToRemove);
 1148        }
 149
 150        public List<ExperienceRowComponentView> GetAllAvailableExperiences()
 151        {
 0152            return availableExperiences.GetItems()
 0153                .Select(x => x as ExperienceRowComponentView)
 154                .ToList();
 155        }
 156
 157        public ExperienceRowComponentView GetAvailableExperienceById(string id)
 158        {
 1159            return availableExperiences.GetItems()
 2160                .Select(x => x as ExperienceRowComponentView)
 2161                .FirstOrDefault(x => x.model.id == id);
 162        }
 163
 4164        public void SetVisible(bool isActive) { gameObject.SetActive(isActive); }
 165
 166        public void ShowUIHiddenToast()
 167        {
 1168            if (uiHiddenToastCoroutine != null)
 169            {
 0170                StopCoroutine(uiHiddenToastCoroutine);
 0171                uiHiddenToastCoroutine = null;
 172            }
 173
 1174            uiHiddenToastCoroutine = StartCoroutine(ShowUIHiddenToastCoroutine());
 1175        }
 176
 177        public override void Dispose()
 178        {
 20179            base.Dispose();
 180
 20181            closeButton.onClick.RemoveAllListeners();
 20182        }
 183
 184        internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible)
 185        {
 1186            onSomeExperienceUIVisibilityChanged?.Invoke(pexId, isUIVisible);
 1187        }
 188
 189        internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying)
 190        {
 1191            onSomeExperienceExecutionChanged?.Invoke(pexId, isPlaying);
 1192        }
 193
 194        internal void ConfigurePEXPool()
 195        {
 11196            experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME);
 11197            if (experiencesPool == null)
 198            {
 1199                experiencesPool = PoolManager.i.AddPool(
 200                    EXPERIENCES_POOL_NAME,
 201                    GameObject.Instantiate(experienceRowPrefab).gameObject,
 202                    maxPrewarmCount: EXPERIENCES_POOL_PREWARM,
 203                    isPersistent: true);
 204
 1205                experiencesPool.ForcePrewarm();
 206            }
 11207        }
 208
 209        internal IEnumerator ShowUIHiddenToastCoroutine()
 210        {
 1211            hiddenUIToastAnimator.Show();
 1212            yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME);
 0213            hiddenUIToastAnimator.Hide();
 0214        }
 215
 216        internal static ExperiencesViewerComponentView Create()
 217        {
 0218            ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe
 0219            experiencesViewerComponentView.name = "_ExperiencesViewer";
 220
 0221            return experiencesViewerComponentView;
 222        }
 223    }
 224}