< 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:59
Coverable lines:109
Total lines:251
Line coverage:45.8% (50 of 109)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ExperiencesViewerComponentController()0%110100%
Initialize(...)0%220100%
SetVisibility(...)0%110100%
Dispose()0%220100%
OnCloseButtonPressed()0%110100%
OnSomeExperienceUIVisibilityChanged(...)0%12300%
OnSomeExperienceExecutionChanged(...)0%30500%
IsOpenChanged(...)0%110100%
CheckCurrentActivePortableExperiences()0%4.123050%
OnPEXSceneAdded(...)0%2100%
OnPEXSceneAdded(...)0%42600%
OnPEXSceneRemoved(...)0%30500%
OnUserProfileUpdated(...)0%10.357059.09%
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;
 52
 753            DataStore.i.world.portableExperienceIds.OnAdded += OnPEXSceneAdded;
 754            DataStore.i.world.portableExperienceIds.OnRemoved += OnPEXSceneRemoved;
 55
 756            CheckCurrentActivePortableExperiences();
 57
 758            userProfile = UserProfile.GetOwnUserProfile();
 759            if (userProfile != null)
 60            {
 761                userProfile.OnUpdate += OnUserProfileUpdated;
 762                OnUserProfileUpdated(userProfile);
 63            }
 64
 765            isInitialized.Set(view.experienceViewerTransform);
 766        }
 67
 68        public void SetVisibility(bool visible)
 69        {
 1670            view.SetVisible(visible);
 1671            isOpen.Set(visible);
 1672        }
 73
 74        public void Dispose()
 75        {
 776            view.onCloseButtonPressed -= OnCloseButtonPressed;
 777            view.onSomeExperienceUIVisibilityChanged -= OnSomeExperienceUIVisibilityChanged;
 778            view.onSomeExperienceExecutionChanged -= OnSomeExperienceExecutionChanged;
 779            isOpen.OnChange -= IsOpenChanged;
 80
 781            DataStore.i.world.portableExperienceIds.OnAdded -= OnPEXSceneAdded;
 782            DataStore.i.world.portableExperienceIds.OnRemoved -= OnPEXSceneRemoved;
 83
 784            if (userProfile != null)
 785                userProfile.OnUpdate -= OnUserProfileUpdated;
 786        }
 87
 288        internal void OnCloseButtonPressed() { SetVisibility(false); }
 89
 90        internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isVisible)
 91        {
 092            activePEXScenes.TryGetValue(pexId, out IParcelScene scene);
 093            if (scene != null)
 94            {
 095                UIScreenSpace sceneUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent<UIScreenSpace>();
 096                sceneUIComponent.canvas.enabled = isVisible;
 97            }
 98
 099            if (!isVisible)
 0100                view.ShowUIHiddenToast();
 0101        }
 102
 103        internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying)
 104        {
 0105            if (isPlaying)
 106            {
 0107                WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.Where(x => x != pexId).ToArray());
 108            }
 109            else
 110            {
 111                // We only keep the experience paused in the list if our avatar has the related wearable equipped
 0112                if (userProfile != null && userProfile.avatar.wearables.Contains(pexId))
 113                {
 0114                    if (!pausedPEXScenesIds.Contains(pexId))
 0115                        pausedPEXScenesIds.Add(pexId);
 116
 0117                    WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray());
 118                }
 119                else
 120                {
 0121                    WebInterface.KillPortableExperience(pexId);
 122                }
 123            }
 0124        }
 125
 26126        internal void IsOpenChanged(bool current, bool previous) { SetVisibility(current); }
 127
 128        internal void CheckCurrentActivePortableExperiences()
 129        {
 8130            activePEXScenes.Clear();
 8131            pausedPEXScenesIds.Clear();
 132
 8133            if (DCL.Environment.i.world.state != null)
 134            {
 0135                List<GlobalScene> activePortableExperiences =
 136                    PortableExperienceUtils.GetActivePortableExperienceScenes();
 0137                foreach (GlobalScene pexScene in activePortableExperiences)
 138                {
 0139                    OnPEXSceneAdded(pexScene);
 140                }
 141            }
 142
 8143            numOfLoadedExperiences.Set(activePEXScenes.Count);
 8144        }
 145
 146        public void OnPEXSceneAdded(string id)
 147        {
 0148            OnPEXSceneAdded(Environment.i.world.state.GetPortableExperienceScene(id));
 0149        }
 150
 151        public void OnPEXSceneAdded(IParcelScene scene)
 152        {
 0153            ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(scene.sceneData.id);
 154
 0155            if (activePEXScenes.ContainsKey(scene.sceneData.id))
 156            {
 0157                activePEXScenes[scene.sceneData.id] = scene;
 0158                pausedPEXScenesIds.Remove(scene.sceneData.id);
 159
 0160                if (experienceToUpdate != null)
 0161                    experienceToUpdate.SetUIVisibility(true);
 162
 0163                return;
 164            }
 165
 0166            GlobalScene newPortableExperienceScene = scene as GlobalScene;
 0167            DataStore.i.experiencesViewer.activeExperience.Get().Add(scene.sceneData.id);
 168
 0169            if (pausedPEXScenesIds.Contains(scene.sceneData.id))
 170            {
 0171                pausedPEXScenesIds.Remove(scene.sceneData.id);
 172
 0173                if (experienceToUpdate != null)
 0174                    experienceToUpdate.SetAsPlaying(true);
 175            }
 176            else
 177            {
 0178                ExperienceRowComponentModel experienceToAdd = new ExperienceRowComponentModel
 179                {
 180                    id = newPortableExperienceScene.sceneData.id,
 181                    isPlaying = true,
 182                    isUIVisible = true,
 183                    name = newPortableExperienceScene.sceneName,
 184                    iconUri = newPortableExperienceScene.iconUrl,
 185                    allowStartStop = userProfile != null && userProfile.avatar.wearables.Contains(newPortableExperienceS
 186                };
 187
 0188                view.AddAvailableExperience(experienceToAdd);
 0189                activePEXScenes.Add(scene.sceneData.id, scene);
 0190                numOfLoadedExperiences.Set(activePEXScenes.Count);
 191            }
 0192        }
 193
 194        public void OnPEXSceneRemoved(string id)
 195        {
 0196            if (!activePEXScenes.ContainsKey(id))
 0197                return;
 198
 0199            DataStore.i.experiencesViewer.activeExperience.Get().Remove(id);
 0200            if (pausedPEXScenesIds.Contains(id))
 201            {
 0202                ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(id);
 0203                if (experienceToUpdate != null)
 204                {
 0205                    if (!experienceToUpdate.model.isPlaying)
 0206                        return;
 207                }
 208            }
 209
 0210            view.RemoveAvailableExperience(id);
 0211            activePEXScenes.Remove(id);
 0212            pausedPEXScenesIds.Remove(id);
 0213            numOfLoadedExperiences.Set(activePEXScenes.Count);
 0214        }
 215
 216        internal void OnUserProfileUpdated(UserProfile userProfile)
 217        {
 7218            List<string> experiencesIdsToRemove = new List<string>();
 219
 14220            foreach (var pex in activePEXScenes)
 221            {
 222                // We remove from the list all those experiences related to wearables that are not equipped
 0223                if (wearableCatalog.ContainsKey(pex.Key) && !userProfile.avatar.wearables.Contains(pex.Key))
 0224                    experiencesIdsToRemove.Add(pex.Key);
 225            }
 226
 14227            foreach (string pexId in experiencesIdsToRemove)
 228            {
 0229                view.RemoveAvailableExperience(pexId);
 0230                activePEXScenes.Remove(pexId);
 0231                pausedPEXScenesIds.Remove(pexId);
 232            }
 233
 7234            numOfLoadedExperiences.Set(activePEXScenes.Count);
 235
 7236            if (lastDisablePEXSentToKernel != pausedPEXScenesIds)
 237            {
 7238                lastDisablePEXSentToKernel = pausedPEXScenesIds;
 7239                WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray());
 240            }
 241
 7242            List<ExperienceRowComponentView> loadedExperiences = view.GetAllAvailableExperiences();
 14243            for (int i = 0; i < loadedExperiences.Count; i++)
 244            {
 0245                loadedExperiences[i].SetAllowStartStop(userProfile.avatar.wearables.Contains(loadedExperiences[i].model.
 246            }
 7247        }
 248
 0249        internal virtual IExperiencesViewerComponentView CreateView() => ExperiencesViewerComponentView.Create();
 250    }
 251}