< Summary

Class:DCL.ExperiencesViewer.ExperiencesViewerComponentController
Assembly:ExperiencesViewer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExperiencesViewer/Scripts/ExperiencesViewerComponentController.cs
Covered lines:50
Uncovered lines:57
Coverable lines:107
Total lines:242
Line coverage:46.7% (50 of 107)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ExperiencesViewerComponentController()0%110100%
Initialize(...)0%330100%
SetVisibility(...)0%110100%
Dispose()0%330100%
OnCloseButtonPressed()0%110100%
OnSomeExperienceUIVisibilityChanged(...)0%12300%
OnSomeExperienceExecutionChanged(...)0%30500%
IsOpenChanged(...)0%110100%
CheckCurrentActivePortableExperiences()0%4.123050%
OnPEXSceneAdded(...)0%42600%
OnPEXSceneRemoved(...)0%30500%
OnUserProfileUpdated(...)0%8.516058.82%
CreateView()0%2100%

File(s)

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

#LineLine coverage
 1using DCL.Components;
 2using DCL.Controllers;
 3using DCL.Interface;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8
 9namespace DCL.ExperiencesViewer
 10{
 11    public interface IExperiencesViewerComponentController : IDisposable
 12    {
 13        /// <summary>
 14        /// Initializes the experiences viewer controller.
 15        /// </summary>
 16        /// <param name="sceneController">Scene controller used to detect when PEX are created/removed.</param>
 17        void Initialize(ISceneController sceneController);
 18
 19        /// <summary>
 20        /// Set the experience viewer feature as visible or not.
 21        /// </summary>
 22        /// <param name="visible">True for showing it.</param>
 23        void SetVisibility(bool visible);
 24    }
 25
 26    public class ExperiencesViewerComponentController : IExperiencesViewerComponentController
 27    {
 728        internal BaseVariable<Transform> isInitialized => DataStore.i.experiencesViewer.isInitialized;
 3729        internal BaseVariable<bool> isOpen => DataStore.i.experiencesViewer.isOpen;
 1530        internal BaseVariable<int> numOfLoadedExperiences => DataStore.i.experiencesViewer.numOfLoadedExperiences;
 031        public BaseDictionary<string, WearableItem> wearableCatalog => DataStore.i.common.wearables;
 32
 33        internal IExperiencesViewerComponentView view;
 34        internal ISceneController sceneController;
 35        internal UserProfile userProfile;
 36        internal CatalogController catalog;
 737        internal Dictionary<string, IParcelScene> activePEXScenes = new Dictionary<string, IParcelScene>();
 738        internal List<string> pausedPEXScenesIds = new List<string>();
 39        internal List<string> lastDisablePEXSentToKernel;
 40
 41        public void Initialize(ISceneController sceneController)
 42        {
 743            view = CreateView();
 744            view.onCloseButtonPressed += OnCloseButtonPressed;
 745            view.onSomeExperienceUIVisibilityChanged += OnSomeExperienceUIVisibilityChanged;
 746            view.onSomeExperienceExecutionChanged += OnSomeExperienceExecutionChanged;
 47
 748            isOpen.OnChange += IsOpenChanged;
 749            IsOpenChanged(isOpen.Get(), false);
 50
 751            this.sceneController = sceneController;
 752            if (this.sceneController != null)
 53            {
 754                this.sceneController.OnNewPortableExperienceSceneAdded += OnPEXSceneAdded;
 755                this.sceneController.OnNewPortableExperienceSceneRemoved += OnPEXSceneRemoved;
 56            }
 57
 758            CheckCurrentActivePortableExperiences();
 59
 760            userProfile = UserProfile.GetOwnUserProfile();
 761            if (userProfile != null)
 62            {
 763                userProfile.OnUpdate += OnUserProfileUpdated;
 764                OnUserProfileUpdated(userProfile);
 65            }
 66
 767            isInitialized.Set(view.experienceViewerTransform);
 768        }
 69
 70        public void SetVisibility(bool visible)
 71        {
 1672            view.SetVisible(visible);
 1673            isOpen.Set(visible);
 1674        }
 75
 76        public void Dispose()
 77        {
 778            view.onCloseButtonPressed -= OnCloseButtonPressed;
 779            view.onSomeExperienceUIVisibilityChanged -= OnSomeExperienceUIVisibilityChanged;
 780            view.onSomeExperienceExecutionChanged -= OnSomeExperienceExecutionChanged;
 781            isOpen.OnChange -= IsOpenChanged;
 82
 783            if (sceneController != null)
 84            {
 785                sceneController.OnNewPortableExperienceSceneAdded -= OnPEXSceneAdded;
 786                sceneController.OnNewPortableExperienceSceneRemoved -= OnPEXSceneRemoved;
 87            }
 88
 789            if (userProfile != null)
 790                userProfile.OnUpdate -= OnUserProfileUpdated;
 791        }
 92
 293        internal void OnCloseButtonPressed() { SetVisibility(false); }
 94
 95        internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isVisible)
 96        {
 097            activePEXScenes.TryGetValue(pexId, out IParcelScene scene);
 098            if (scene != null)
 99            {
 0100                UIScreenSpace sceneUIComponent = scene.GetSharedComponent<UIScreenSpace>();
 0101                sceneUIComponent.canvas.enabled = isVisible;
 102            }
 103
 0104            if (!isVisible)
 0105                view.ShowUIHiddenToast();
 0106        }
 107
 108        internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying)
 109        {
 0110            if (isPlaying)
 111            {
 0112                WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.Where(x => x != pexId).ToArray());
 0113            }
 114            else
 115            {
 116                // We only keep the experience paused in the list if our avatar has the related wearable equipped
 0117                if (userProfile != null && userProfile.avatar.wearables.Contains(pexId))
 118                {
 0119                    if (!pausedPEXScenesIds.Contains(pexId))
 0120                        pausedPEXScenesIds.Add(pexId);
 121
 0122                    WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray());
 0123                }
 124                else
 125                {
 0126                    WebInterface.KillPortableExperience(pexId);
 127                }
 128            }
 0129        }
 130
 26131        internal void IsOpenChanged(bool current, bool previous) { SetVisibility(current); }
 132
 133        internal void CheckCurrentActivePortableExperiences()
 134        {
 8135            activePEXScenes.Clear();
 8136            pausedPEXScenesIds.Clear();
 137
 8138            if (DCL.Environment.i.world.state != null)
 139            {
 0140                List<GlobalScene> activePortableExperiences = WorldStateUtils.GetActivePortableExperienceScenes();
 0141                foreach (GlobalScene pexScene in activePortableExperiences)
 142                {
 0143                    OnPEXSceneAdded(pexScene);
 144                }
 145            }
 146
 8147            numOfLoadedExperiences.Set(activePEXScenes.Count);
 8148        }
 149
 150        public void OnPEXSceneAdded(IParcelScene scene)
 151        {
 0152            ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(scene.sceneData.id);
 153
 0154            if (activePEXScenes.ContainsKey(scene.sceneData.id))
 155            {
 0156                activePEXScenes[scene.sceneData.id] = scene;
 0157                pausedPEXScenesIds.Remove(scene.sceneData.id);
 158
 0159                if (experienceToUpdate != null)
 0160                    experienceToUpdate.SetUIVisibility(true);
 161
 0162                return;
 163            }
 164
 0165            GlobalScene newPortableExperienceScene = scene as GlobalScene;
 166
 0167            if (pausedPEXScenesIds.Contains(scene.sceneData.id))
 168            {
 0169                pausedPEXScenesIds.Remove(scene.sceneData.id);
 170
 0171                if (experienceToUpdate != null)
 0172                    experienceToUpdate.SetAsPlaying(true);
 0173            }
 174            else
 175            {
 0176                ExperienceRowComponentModel experienceToAdd = new ExperienceRowComponentModel
 177                {
 178                    id = newPortableExperienceScene.sceneData.id,
 179                    isPlaying = true,
 180                    isUIVisible = true,
 181                    name = newPortableExperienceScene.sceneName,
 182                    iconUri = newPortableExperienceScene.iconUrl,
 183                    allowStartStop = userProfile != null && userProfile.avatar.wearables.Contains(newPortableExperienceS
 184                };
 185
 0186                view.AddAvailableExperience(experienceToAdd);
 0187                activePEXScenes.Add(scene.sceneData.id, scene);
 0188                numOfLoadedExperiences.Set(activePEXScenes.Count);
 189            }
 0190        }
 191
 192        public void OnPEXSceneRemoved(string id)
 193        {
 0194            if (!activePEXScenes.ContainsKey(id))
 0195                return;
 196
 0197            if (pausedPEXScenesIds.Contains(id))
 198            {
 0199                ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(id);
 0200                if (experienceToUpdate != null)
 201                {
 0202                    if (!experienceToUpdate.model.isPlaying)
 0203                        return;
 204                }
 205            }
 206
 0207            view.RemoveAvailableExperience(id);
 0208            activePEXScenes.Remove(id);
 0209            pausedPEXScenesIds.Remove(id);
 0210            numOfLoadedExperiences.Set(activePEXScenes.Count);
 0211        }
 212
 213        internal void OnUserProfileUpdated(UserProfile userProfile)
 214        {
 7215            List<string> experiencesIdsToRemove = new List<string>();
 216
 14217            foreach (var pex in activePEXScenes)
 218            {
 219                // We remove from the list all those experiences related to wearables that are not equipped
 0220                if (wearableCatalog.ContainsKey(pex.Key) && !userProfile.avatar.wearables.Contains(pex.Key))
 0221                    experiencesIdsToRemove.Add(pex.Key);
 222            }
 223
 14224            foreach (string pexId in experiencesIdsToRemove)
 225            {
 0226                view.RemoveAvailableExperience(pexId);
 0227                activePEXScenes.Remove(pexId);
 0228                pausedPEXScenesIds.Remove(pexId);
 229            }
 230
 7231            numOfLoadedExperiences.Set(activePEXScenes.Count);
 232
 7233            if (lastDisablePEXSentToKernel != pausedPEXScenesIds)
 234            {
 7235                lastDisablePEXSentToKernel = pausedPEXScenesIds;
 7236                WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray());
 237            }
 7238        }
 239
 0240        internal virtual IExperiencesViewerComponentView CreateView() => ExperiencesViewerComponentView.Create();
 241    }
 242}