< Summary

Class:DCL.ExperiencesViewer.ExperiencesViewerComponentView
Assembly:ExperiencesViewer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ExperiencesViewer/Scripts/ExperiencesViewerComponentView.cs
Covered lines:46
Uncovered lines:33
Coverable lines:79
Total lines:201
Line coverage:58.2% (46 of 79)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:19
Method coverage:63.1% (12 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Create()0%2100%
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%4.374071.43%
ShowUiShownToast(...)0%20400%
ShowEnabledToast(...)0%20400%
ShowDisabledToast(...)0%20400%
Dispose()0%110100%
OnSomeExperienceUIVisibilityChanged(...)0%220100%
OnSomeExperienceExecutionChanged(...)0%220100%
ConfigurePool()0%220100%
ShowToastRoutine()0%4.123050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ExperiencesViewer/Scripts/ExperiencesViewerComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace DCL.ExperiencesViewer
 10{
 11    public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView
 12    {
 13        private const int EXPERIENCES_POOL_PREWARM = 10;
 14        private const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f;
 15        private const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool";
 16
 17        [Header("Assets References")]
 18        [SerializeField] internal ExperienceRowComponentView experienceRowPrefab;
 19
 20        [Header("Prefab References")]
 21        [SerializeField] internal Button closeButton;
 22        [SerializeField] internal GridContainerComponentView availableExperiences;
 23        [SerializeField] internal ShowHideAnimator toastAnimator;
 24        [SerializeField] internal TMP_Text toastLabel;
 25
 26        private Pool experiencesPool;
 27        private Coroutine toastRoutine;
 28
 29        public Color rowsBackgroundColor;
 30        public Color rowsOnHoverColor;
 31
 32        public event Action OnCloseButtonPressed;
 33        public event Action<string, bool> OnExperienceUiVisibilityChanged;
 34        public event Action<string, bool> OnExperienceExecutionChanged;
 35
 036        public Transform ExperienceViewerTransform => transform;
 37
 38        public static ExperiencesViewerComponentView Create()
 39        {
 040            ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe
 041            experiencesViewerComponentView.name = "_ExperiencesViewer";
 42
 043            return experiencesViewerComponentView;
 44        }
 45
 46        public override void Awake()
 47        {
 948            base.Awake();
 49
 950            closeButton.onClick.AddListener(() => OnCloseButtonPressed?.Invoke());
 51
 952            ConfigurePool();
 953        }
 54
 55        public override void RefreshControl()
 56        {
 057            availableExperiences.RefreshControl();
 058        }
 59
 60        public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences)
 61        {
 462            availableExperiences.ExtractItems();
 463            experiencesPool.ReleaseAll();
 64
 2465            foreach (ExperienceRowComponentModel exp in experiences)
 866                AddAvailableExperience(exp);
 467        }
 68
 69        public void AddAvailableExperience(ExperienceRowComponentModel experience)
 70        {
 1171            experience.backgroundColor = rowsBackgroundColor;
 1172            experience.onHoverColor = rowsOnHoverColor;
 73
 1174            ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen
 1175            newRealmRow.Configure(experience);
 1176            newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged;
 1177            newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged;
 1178            newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged;
 1179            newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged;
 80
 1181            availableExperiences.AddItemWithResize(newRealmRow);
 1182        }
 83
 84        public void RemoveAvailableExperience(string id)
 85        {
 186            ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems()
 187                .Select(x => x as ExperienceRowComponentView)
 188                .FirstOrDefault(x => x.model.id == id);
 89
 190            if (experienceToRemove != null)
 191                availableExperiences.RemoveItem(experienceToRemove);
 192        }
 93
 94        public List<ExperienceRowComponentView> GetAllAvailableExperiences()
 95        {
 096            return availableExperiences.GetItems()
 097                .Select(x => x as ExperienceRowComponentView)
 98                .ToList();
 99        }
 100
 101        public ExperienceRowComponentView GetAvailableExperienceById(string id)
 102        {
 1103            return availableExperiences.GetItems()
 2104                .Select(x => x as ExperienceRowComponentView)
 2105                .FirstOrDefault(x => x.model.id == id);
 106        }
 107
 4108        public void SetVisible(bool isActive) { gameObject.SetActive(isActive); }
 109
 110        public void ShowUiHiddenToast(string pxName)
 111        {
 1112            if (toastRoutine != null)
 113            {
 0114                StopCoroutine(toastRoutine);
 0115                toastRoutine = null;
 116            }
 117
 1118            pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName;
 119
 1120            toastLabel.text = $"<b>{pxName}</b> UI hidden";
 1121            toastRoutine = StartCoroutine(ShowToastRoutine());
 1122        }
 123
 124        public void ShowUiShownToast(string pxName)
 125        {
 0126            if (toastRoutine != null)
 127            {
 0128                StopCoroutine(toastRoutine);
 0129                toastRoutine = null;
 130            }
 131
 0132            pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName;
 133
 0134            toastLabel.text = $"<b>{pxName}</b> UI shown";
 0135            toastRoutine = StartCoroutine(ShowToastRoutine());
 0136        }
 137
 138        public void ShowEnabledToast(string pxName)
 139        {
 0140            if (toastRoutine != null)
 141            {
 0142                StopCoroutine(toastRoutine);
 0143                toastRoutine = null;
 144            }
 145
 0146            pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName;
 147
 0148            toastLabel.text = $"<b>{pxName}</b> activated";
 0149            toastRoutine = StartCoroutine(ShowToastRoutine());
 0150        }
 151
 152        public void ShowDisabledToast(string pxName)
 153        {
 0154            if (toastRoutine != null)
 155            {
 0156                StopCoroutine(toastRoutine);
 0157                toastRoutine = null;
 158            }
 159
 0160            pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName;
 161
 0162            toastLabel.text = $"<b>{pxName}</b> deactivated";
 0163            toastRoutine = StartCoroutine(ShowToastRoutine());
 0164        }
 165
 166        public override void Dispose()
 167        {
 18168            base.Dispose();
 169
 18170            closeButton.onClick.RemoveAllListeners();
 18171        }
 172
 173        private void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible) =>
 1174            OnExperienceUiVisibilityChanged?.Invoke(pexId, isUIVisible);
 175
 176        private void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) =>
 3177            OnExperienceExecutionChanged?.Invoke(pexId, isPlaying);
 178
 179        private void ConfigurePool()
 180        {
 9181            experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME);
 182
 17183            if (experiencesPool != null) return;
 184
 1185            experiencesPool = PoolManager.i.AddPool(
 186                EXPERIENCES_POOL_NAME,
 187                Instantiate(experienceRowPrefab).gameObject,
 188                maxPrewarmCount: EXPERIENCES_POOL_PREWARM,
 189                isPersistent: true);
 190
 1191            experiencesPool.ForcePrewarm();
 1192        }
 193
 194        private IEnumerator ShowToastRoutine()
 195        {
 1196            toastAnimator.Show();
 1197            yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME);
 0198            toastAnimator.Hide();
 0199        }
 200    }
 201}