< 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:10
Coverable lines:57
Total lines:211
Line coverage:82.4% (47 of 57)
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%
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 a specific experience.
 51        /// </summary>
 52        /// <param name="id">Id of the experience to search.</param>
 53        /// <returns>An experience.</returns>
 54        ExperienceRowComponentView GetAvailableExperienceById(string id);
 55
 56        /// <summary>
 57        /// Shows/Hides the game object of the Experiences Viewer.
 58        /// </summary>
 59        /// <param name="isActive">True to show it.</param>
 60        void SetVisible(bool isActive);
 61
 62        /// <summary>
 63        /// Shows the info toast for when the UI of an experience is hidden.
 64        /// </summary>
 65        void ShowUIHiddenToast();
 66    }
 67
 68    public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView
 69    {
 70        internal const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool";
 71        internal const int EXPERIENCES_POOL_PREWARM = 10;
 72        internal const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f;
 73
 74        [Header("Assets References")]
 75        [SerializeField] internal ExperienceRowComponentView experienceRowPrefab;
 76
 77        [Header("Prefab References")]
 78        [SerializeField] internal ButtonComponentView closeButton;
 79        [SerializeField] internal GridContainerComponentView availableExperiences;
 80        [SerializeField] internal ShowHideAnimator hiddenUIToastAnimator;
 81        public Color rowsBackgroundColor;
 82        public Color rowsOnHoverColor;
 83
 84        public event Action onCloseButtonPressed;
 85        public event Action<string, bool> onSomeExperienceUIVisibilityChanged;
 86        public event Action<string, bool> onSomeExperienceExecutionChanged;
 87
 88        internal Pool experiencesPool;
 89        internal Coroutine uiHiddenToastCoroutine;
 90
 091        public Transform experienceViewerTransform => transform;
 92
 93        public override void Awake()
 94        {
 1095            base.Awake();
 96
 1097            closeButton.onClick.AddListener(() => onCloseButtonPressed?.Invoke());
 98
 1099            ConfigurePEXPool();
 10100        }
 101
 102        public override void RefreshControl()
 103        {
 0104            availableExperiences.RefreshControl();
 0105        }
 106
 107        public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences)
 108        {
 4109            availableExperiences.ExtractItems();
 4110            experiencesPool.ReleaseAll();
 111
 4112            List<BaseComponentView> experiencesToAdd = new List<BaseComponentView>();
 24113            foreach (ExperienceRowComponentModel exp in experiences)
 114            {
 8115                AddAvailableExperience(exp);
 116            }
 4117        }
 118
 119        public void AddAvailableExperience(ExperienceRowComponentModel experience)
 120        {
 9121            experience.backgroundColor = rowsBackgroundColor;
 9122            experience.onHoverColor = rowsOnHoverColor;
 123
 9124            ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen
 9125            newRealmRow.Configure(experience);
 9126            newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged;
 9127            newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged;
 9128            newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged;
 9129            newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged;
 130
 9131            availableExperiences.AddItem(newRealmRow);
 9132        }
 133
 134        public void RemoveAvailableExperience(string id)
 135        {
 1136            ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems()
 1137                .Select(x => x as ExperienceRowComponentView)
 1138                .FirstOrDefault(x => x.model.id == id);
 139
 1140            if (experienceToRemove != null)
 1141                availableExperiences.RemoveItem(experienceToRemove);
 1142        }
 143
 144        public ExperienceRowComponentView GetAvailableExperienceById(string id)
 145        {
 1146            return availableExperiences.GetItems()
 2147                .Select(x => x as ExperienceRowComponentView)
 2148                .FirstOrDefault(x => x.model.id == id);
 149        }
 150
 4151        public void SetVisible(bool isActive) { gameObject.SetActive(isActive); }
 152
 153        public void ShowUIHiddenToast()
 154        {
 1155            if (uiHiddenToastCoroutine != null)
 156            {
 0157                StopCoroutine(uiHiddenToastCoroutine);
 0158                uiHiddenToastCoroutine = null;
 159            }
 160
 1161            uiHiddenToastCoroutine = StartCoroutine(ShowUIHiddenToastCoroutine());
 1162        }
 163
 164        public override void Dispose()
 165        {
 20166            base.Dispose();
 167
 20168            closeButton.onClick.RemoveAllListeners();
 20169        }
 170
 171        internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible)
 172        {
 1173            onSomeExperienceUIVisibilityChanged?.Invoke(pexId, isUIVisible);
 1174        }
 175
 176        internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying)
 177        {
 1178            onSomeExperienceExecutionChanged?.Invoke(pexId, isPlaying);
 1179        }
 180
 181        internal void ConfigurePEXPool()
 182        {
 11183            experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME);
 11184            if (experiencesPool == null)
 185            {
 1186                experiencesPool = PoolManager.i.AddPool(
 187                    EXPERIENCES_POOL_NAME,
 188                    GameObject.Instantiate(experienceRowPrefab).gameObject,
 189                    maxPrewarmCount: EXPERIENCES_POOL_PREWARM,
 190                    isPersistent: true);
 191
 1192                experiencesPool.ForcePrewarm();
 193            }
 11194        }
 195
 196        internal IEnumerator ShowUIHiddenToastCoroutine()
 197        {
 1198            hiddenUIToastAnimator.Show();
 1199            yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME);
 0200            hiddenUIToastAnimator.Hide();
 0201        }
 202
 203        internal static ExperiencesViewerComponentView Create()
 204        {
 0205            ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe
 0206            experiencesViewerComponentView.name = "_ExperiencesViewer";
 207
 0208            return experiencesViewerComponentView;
 209        }
 210    }
 211}