< Summary

Class:ScenesViewController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/ScenesController/ScenesViewController.cs
Covered lines:100
Uncovered lines:28
Coverable lines:128
Total lines:315
Line coverage:78.1% (100 of 128)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ScenesViewController(...)0%110100%
SetScenes(...)0%12.0812091.67%
SelectScene(...)0%30500%
AddListener(...)0%110100%
AddListener(...)0%110100%
AddListener(...)0%2100%
RemoveListener(...)0%2100%
RemoveListener(...)0%2100%
RemoveListener(...)0%2100%
Dispose()0%3.013090.91%
SetScene(...)0%770100%
ChangeSceneDeployStatus(...)0%110100%
UpdateScene(...)0%110100%
CreateScene(...)0%110100%
RemoveScene(...)0%7.057090%
AddScene(...)0%8.098088.89%
CreateCardView()0%110100%
DestroyCardView(...)0%110100%
OnSceneSettingsPressed(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/ScenesController/ScenesViewController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using Object = UnityEngine.Object;
 5
 6internal interface IScenesViewController : IDisposable
 7{
 8    event Action<Dictionary<string, ISceneCardView>> OnDeployedScenesSet;
 9    event Action<ISceneCardView> OnDeployedSceneAdded;
 10    event Action<ISceneCardView> OnDeployedSceneRemoved;
 11    event Action<Dictionary<string, ISceneCardView>> OnProjectScenesSet;
 12    event Action<ISceneCardView> OnProjectSceneAdded;
 13    event Action<ISceneCardView> OnProjectSceneRemoved;
 14    event Action<ISceneCardView> OnSceneSelected;
 15    event Action<Vector2Int> OnJumpInPressed;
 16    event Action<Vector2Int> OnEditorPressed;
 17    event Action<ISceneData, ISceneCardView> OnContextMenuPressed;
 18    event Action<string> OnRequestOpenUrl;
 19    void SetScenes(ISceneData[] scenesData);
 20    void SelectScene(string id);
 21    void AddListener(IDeployedSceneListener listener);
 22    void AddListener(IProjectSceneListener listener);
 23    void AddListener(ISelectSceneListener listener);
 24    void RemoveListener(IDeployedSceneListener listener);
 25    void RemoveListener(IProjectSceneListener listener);
 26    void RemoveListener(ISelectSceneListener listener);
 27}
 28
 29/// <summary>
 30/// This class is responsible for receiving a list of scenes and merge it with the previous list
 31/// discriminating deployed and project scenes and triggering events when a new set of scenes arrive or
 32/// when new scenes are added or removed.
 33/// It instantiate and hold all the SceneCardViews to make them re-utilizable in every menu section screen.
 34/// </summary>
 35internal class ScenesViewController : IScenesViewController
 36{
 37    public event Action<Dictionary<string, ISceneCardView>> OnDeployedScenesSet;
 38    public event Action<ISceneCardView> OnDeployedSceneAdded;
 39    public event Action<ISceneCardView> OnDeployedSceneRemoved;
 40    public event Action<Dictionary<string, ISceneCardView>> OnProjectScenesSet;
 41    public event Action<ISceneCardView> OnProjectSceneAdded;
 42    public event Action<ISceneCardView> OnProjectSceneRemoved;
 43    public event Action<ISceneCardView> OnSceneSelected;
 44    public event Action<Vector2Int> OnJumpInPressed;
 45    public event Action<Vector2Int> OnEditorPressed;
 46    public event Action<ISceneData, ISceneCardView> OnContextMenuPressed;
 47    public event Action<string> OnRequestOpenUrl;
 48
 549    private Dictionary<string, ISceneCardView> deployedScenes = new Dictionary<string, ISceneCardView>();
 550    private Dictionary<string, ISceneCardView> projectScenes = new Dictionary<string, ISceneCardView>();
 51
 52    private ISceneCardView selectedScene;
 53
 554    private readonly ScenesRefreshHelper scenesRefreshHelper = new ScenesRefreshHelper();
 55    private readonly SceneCardView sceneCardViewPrefab;
 56    private readonly Transform defaultParent;
 57
 58    /// <summary>
 59    /// Ctor
 60    /// </summary>
 61    /// <param name="sceneCardViewPrefab">prefab for scene's card</param>
 62    /// <param name="defaultParent">default parent for scene's card</param>
 563    public ScenesViewController(SceneCardView sceneCardViewPrefab, Transform defaultParent = null)
 64    {
 565        this.sceneCardViewPrefab = sceneCardViewPrefab;
 566        this.defaultParent = defaultParent;
 567    }
 68
 69    /// <summary>
 70    /// Set current user scenes (deployed and projects)
 71    /// </summary>
 72    /// <param name="scenesData">list of scenes</param>
 73    void IScenesViewController.SetScenes(ISceneData[] scenesData)
 74    {
 1275        scenesRefreshHelper.Set(deployedScenes, projectScenes);
 76
 1277        deployedScenes = new Dictionary<string, ISceneCardView>();
 1278        projectScenes = new Dictionary<string, ISceneCardView>();
 79
 80        // update or create new scenes view
 7481        for (int i = 0; i < scenesData.Length; i++)
 82        {
 2583            SetScene(scenesData[i]);
 84        }
 85
 86        // remove old deployed scenes
 1287        if (scenesRefreshHelper.oldDeployedScenes != null)
 88        {
 1289            using (var iterator = scenesRefreshHelper.oldDeployedScenes.GetEnumerator())
 90            {
 1791                while (iterator.MoveNext())
 92                {
 593                    OnDeployedSceneRemoved?.Invoke(iterator.Current.Value);
 594                    DestroyCardView(iterator.Current.Value);
 95                }
 1296            }
 97        }
 98
 99        // remove old project scenes
 12100        if (scenesRefreshHelper.oldProjectsScenes != null)
 101        {
 12102            using (var iterator = scenesRefreshHelper.oldProjectsScenes.GetEnumerator())
 103            {
 12104                while (iterator.MoveNext())
 105                {
 0106                    OnProjectSceneRemoved?.Invoke(iterator.Current.Value);
 0107                    DestroyCardView(iterator.Current.Value);
 108                }
 12109            }
 110        }
 111
 112        // notify scenes set if needed
 12113        if (scenesRefreshHelper.isOldDeployedScenesEmpty)
 114        {
 4115            OnDeployedScenesSet?.Invoke(deployedScenes);
 116        }
 117
 12118        if (scenesRefreshHelper.isOldProjectScenesEmpty)
 119        {
 5120            OnProjectScenesSet?.Invoke(projectScenes);
 121        }
 12122    }
 123
 124    /// <summary>
 125    /// Set selected scene
 126    /// </summary>
 127    /// <param name="id">scene id</param>
 128    void IScenesViewController.SelectScene(string id)
 129    {
 0130        ISceneCardView sceneCardView = null;
 0131        if (deployedScenes.TryGetValue(id, out ISceneCardView deployedSceneCardView))
 132        {
 0133            sceneCardView = deployedSceneCardView;
 0134        }
 0135        else if (projectScenes.TryGetValue(id, out ISceneCardView projectSceneCardView))
 136        {
 0137            sceneCardView = projectSceneCardView;
 138        }
 139
 0140        selectedScene = sceneCardView;
 141
 0142        if (sceneCardView != null)
 143        {
 0144            OnSceneSelected?.Invoke(sceneCardView);
 145        }
 0146    }
 147
 148    void IScenesViewController.AddListener(IDeployedSceneListener listener)
 149    {
 5150        OnDeployedSceneAdded += listener.OnSceneAdded;
 5151        OnDeployedSceneRemoved += listener.OnSceneRemoved;
 5152        OnDeployedScenesSet += listener.OnSetScenes;
 5153        listener.OnSetScenes(deployedScenes);
 5154    }
 155
 156    void IScenesViewController.AddListener(IProjectSceneListener listener)
 157    {
 4158        OnProjectSceneAdded += listener.OnSceneAdded;
 4159        OnProjectSceneRemoved += listener.OnSceneRemoved;
 4160        OnProjectScenesSet += listener.OnSetScenes;
 4161        listener.OnSetScenes(projectScenes);
 4162    }
 163
 164    void IScenesViewController.AddListener(ISelectSceneListener listener)
 165    {
 0166        OnSceneSelected += listener.OnSelectScene;
 0167        listener.OnSelectScene(selectedScene);
 0168    }
 169
 170    void IScenesViewController.RemoveListener(IDeployedSceneListener listener)
 171    {
 0172        OnDeployedSceneAdded -= listener.OnSceneAdded;
 0173        OnDeployedSceneRemoved -= listener.OnSceneRemoved;
 0174        OnDeployedScenesSet -= listener.OnSetScenes;
 0175    }
 176    void IScenesViewController.RemoveListener(IProjectSceneListener listener)
 177    {
 0178        OnProjectSceneAdded -= listener.OnSceneAdded;
 0179        OnProjectSceneRemoved -= listener.OnSceneRemoved;
 0180        OnProjectScenesSet -= listener.OnSetScenes;
 0181    }
 0182    void IScenesViewController.RemoveListener(ISelectSceneListener listener) { OnSceneSelected -= listener.OnSelectScene
 183
 184    public void Dispose()
 185    {
 5186        using (var iterator = deployedScenes.GetEnumerator())
 187        {
 5188            while (iterator.MoveNext())
 189            {
 0190                iterator.Current.Value.Dispose();
 191            }
 5192        }
 193
 5194        deployedScenes.Clear();
 195
 5196        using (var iterator = projectScenes.GetEnumerator())
 197        {
 7198            while (iterator.MoveNext())
 199            {
 2200                iterator.Current.Value.Dispose();
 201            }
 5202        }
 203
 5204        projectScenes.Clear();
 5205    }
 206
 207    private void SetScene(ISceneData sceneData)
 208    {
 25209        bool shouldNotifyAdd = (sceneData.isDeployed && !scenesRefreshHelper.isOldDeployedScenesEmpty) ||
 210                               (!sceneData.isDeployed && !scenesRefreshHelper.isOldProjectScenesEmpty);
 211
 25212        if (scenesRefreshHelper.IsSceneDeployStatusChanged(sceneData))
 213        {
 3214            ChangeSceneDeployStatus(sceneData, shouldNotifyAdd);
 3215        }
 22216        else if (scenesRefreshHelper.IsSceneUpdate(sceneData))
 217        {
 15218            UpdateScene(sceneData);
 15219        }
 220        else
 221        {
 7222            CreateScene(sceneData, shouldNotifyAdd);
 223        }
 7224    }
 225
 226    private void ChangeSceneDeployStatus(ISceneData sceneData, bool shouldNotifyAdd)
 227    {
 3228        var cardView = RemoveScene(sceneData, true);
 3229        cardView.Setup(sceneData);
 3230        AddScene(sceneData, cardView, shouldNotifyAdd);
 3231    }
 232
 233    private void UpdateScene(ISceneData sceneData)
 234    {
 15235        var cardView = RemoveScene(sceneData, false);
 15236        cardView.Setup(sceneData);
 15237        AddScene(sceneData, cardView, false);
 15238    }
 239
 240    private void CreateScene(ISceneData sceneData, bool shouldNotifyAdd)
 241    {
 7242        var cardView = CreateCardView();
 7243        cardView.Setup(sceneData);
 7244        AddScene(sceneData, cardView, shouldNotifyAdd);
 7245    }
 246
 247    private ISceneCardView RemoveScene(ISceneData sceneData, bool notify)
 248    {
 18249        bool wasDeployed = scenesRefreshHelper.WasDeployedScene(sceneData);
 18250        var dictionary = wasDeployed ? scenesRefreshHelper.oldDeployedScenes : scenesRefreshHelper.oldProjectsScenes;
 251
 18252        if (dictionary.TryGetValue(sceneData.id, out ISceneCardView sceneCardView))
 253        {
 18254            dictionary.Remove(sceneData.id);
 255
 18256            if (notify)
 257            {
 3258                if (wasDeployed)
 2259                    OnDeployedSceneRemoved?.Invoke(sceneCardView);
 260                else
 1261                    OnProjectSceneRemoved?.Invoke(sceneCardView);
 262            }
 263
 18264            return sceneCardView;
 265        }
 266
 0267        return null;
 268    }
 269
 270    private void AddScene(ISceneData sceneData, ISceneCardView sceneCardView, bool notify)
 271    {
 25272        var dictionary = sceneData.isDeployed ? deployedScenes : projectScenes;
 25273        if (dictionary.ContainsKey(sceneData.id))
 0274            return;
 25275        dictionary.Add(sceneData.id, sceneCardView);
 276
 25277        if (notify)
 278        {
 3279            if (sceneData.isDeployed)
 2280                OnDeployedSceneAdded?.Invoke(sceneCardView);
 281            else
 1282                OnProjectSceneAdded?.Invoke(sceneCardView);
 283        }
 23284    }
 285
 286    private ISceneCardView CreateCardView()
 287    {
 7288        SceneCardView sceneCardView = Object.Instantiate(sceneCardViewPrefab);
 289        ISceneCardView view = sceneCardView;
 290
 7291        view.SetActive(false);
 7292        view.ConfigureDefaultParent(defaultParent);
 7293        view.SetToDefaultParent();
 294
 7295        view.OnEditorPressed += OnEditorPressed;
 7296        view.OnContextMenuPressed += OnContextMenuPressed;
 7297        view.OnJumpInPressed += OnJumpInPressed;
 7298        view.OnSettingsPressed += OnSceneSettingsPressed;
 299
 7300        return view;
 301    }
 302
 303    private void DestroyCardView(ISceneCardView sceneCardView)
 304    {
 305        // NOTE: there is actually no need to unsubscribe here, but, just in case...
 5306        sceneCardView.OnEditorPressed -= OnEditorPressed;
 5307        sceneCardView.OnContextMenuPressed -= OnContextMenuPressed;
 5308        sceneCardView.OnJumpInPressed -= OnJumpInPressed;
 5309        sceneCardView.OnSettingsPressed -= OnSceneSettingsPressed;
 310
 5311        sceneCardView.Dispose();
 5312    }
 313
 0314    private void OnSceneSettingsPressed(ISceneData sceneData) { OnRequestOpenUrl?.Invoke($"https://builder.decentraland.
 315}