< Summary

Class:ProjectsController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/ProjectController/ProjectsController.cs
Covered lines:39
Uncovered lines:11
Coverable lines:50
Total lines:178
Line coverage:78% (39 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProjectsController(...)0%110100%
SetProjects(...)0%5.125083.33%
UpdateDeploymentStatus()0%6.975057.14%
AddListener(...)0%2100%
RemoveListener(...)0%2100%
GetProjects()0%2100%
ExpandMenuPressed()0%220100%
CreateCardView(...)0%110100%
CreateCardView()0%110100%
DestroyCardView(...)0%110100%
OnSceneSettingsPressed(...)0%2100%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/ProjectController/ProjectsController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using DCL;
 6using DCL.Builder;
 7using UnityEngine;
 8
 9/// <summary>
 10/// This interface is responsible to handle the projects of builder in world panel.
 11/// It will handle the listeners that will add/remove projects to the list
 12/// </summary>
 13internal interface IProjectsController
 14{
 15    /// <summary>
 16    /// This action will be called when the user want to edit a project
 17    /// </summary>
 18    event Action<ProjectData> OnEditorPressed;
 19
 20    /// <summary>
 21    /// This action will be called each time that we changes the project list
 22    /// </summary>
 23    event Action<Dictionary<string, IProjectCardView>> OnProjectsSet;
 24
 25    /// <summary>
 26    /// When the user click on expand or collapse the project
 27    /// </summary>
 28    event Action OnExpandMenuPressed;
 29
 30    /// <summary>
 31    /// This will updated the deployments of the projects looking at the lands deployments
 32    /// </summary>
 33    void UpdateDeploymentStatus();
 34
 35    /// <summary>
 36    /// This will set the project list
 37    /// </summary>
 38    /// <param name="projects"></param>
 39    void SetProjects(ProjectData[] projects);
 40
 41    /// <summary>
 42    /// This will add a listener that will be responsible to add/remove projects
 43    /// </summary>
 44    /// <param name="listener"></param>
 45    void AddListener(IProjectsListener listener);
 46
 47    /// <summary>
 48    /// This will remove a listener that will be responsible to add/remove projects
 49    /// </summary>
 50    /// <param name="listener"></param>
 51    void RemoveListener(IProjectsListener listener);
 52
 53    /// <summary>
 54    /// This will get all projects
 55    /// </summary>
 56    /// <returns>Dictionary of all the projects indexed by their id</returns>
 57    Dictionary<string, IProjectCardView> GetProjects();
 58}
 59
 60internal class ProjectsController : IProjectsController
 61{
 62    public event Action<ProjectData> OnEditorPressed;
 63    public event Action<Dictionary<string, IProjectCardView>> OnProjectsSet;
 64
 65    public event Action OnExpandMenuPressed;
 66
 367    private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler();
 68
 369    internal Dictionary<string, IProjectCardView> projects = new Dictionary<string, IProjectCardView>();
 70    private readonly ProjectCardView projectCardViewPrefab;
 71    private readonly Transform defaultParent;
 72
 73    /// <summary>
 74    /// Ctor
 75    /// </summary>
 76    /// <param name="projectCardViewPrefab">prefab for project's card</param>
 77    /// <param name="defaultParent">default parent for scene's card</param>
 378    public ProjectsController(ProjectCardView projectCardViewPrefab, Transform defaultParent = null)
 79    {
 380        this.projectCardViewPrefab = projectCardViewPrefab;
 381        this.defaultParent = defaultParent;
 382    }
 83
 84    public void SetProjects(ProjectData[] projects)
 85    {
 286        foreach (var projectCard in this.projects.Values)
 87        {
 088            DestroyCardView(projectCard);
 89        }
 90
 191        this.projects = new Dictionary<string, IProjectCardView>();
 492        foreach (var project in projects)
 93        {
 194            this.projects.Add(project.id, CreateCardView(project));
 95        }
 296        sceneSearchHandler.SetSearchableList(this.projects.Values.Select(scene => scene.searchInfo).ToList());
 197        OnProjectsSet?.Invoke(this.projects);
 198    }
 99
 100    public void UpdateDeploymentStatus()
 101    {
 4102        foreach (var project in projects)
 103        {
 1104            List<Scene> scenesList = new List<Scene>();
 2105            foreach (LandWithAccess landWithAccess in DataStore.i.builderInWorld.landsWithAccess.Get())
 106            {
 0107                foreach (var scene in landWithAccess.scenes)
 108                {
 0109                    if (scene.projectId == project.Key)
 0110                        scenesList.Add(scene);
 111                }
 112            }
 113
 1114            project.Value.SetScenes(scenesList);
 115        }
 1116    }
 117
 118    public void AddListener(IProjectsListener listener)
 119    {
 0120        OnProjectsSet += listener.OnSetProjects;
 0121        listener.OnSetProjects(projects);
 0122    }
 123
 124    public void RemoveListener(IProjectsListener listener)
 125    {
 0126        OnProjectsSet -= listener.OnSetProjects;
 0127    }
 128
 0129    public Dictionary<string, IProjectCardView> GetProjects() { return projects; }
 130
 131    internal void ExpandMenuPressed()
 132    {
 1133        OnExpandMenuPressed?.Invoke();
 1134    }
 135
 136    private IProjectCardView CreateCardView(ProjectData data)
 137    {
 1138        var card = CreateCardView();
 1139        card.Setup(data);
 1140        return card;
 141    }
 142
 143    private IProjectCardView CreateCardView()
 144    {
 1145        ProjectCardView projectCardView = GameObject.Instantiate(projectCardViewPrefab).GetComponent<ProjectCardView>();
 146        IProjectCardView view = projectCardView;
 147
 1148        view.SetActive(false);
 1149        view.ConfigureDefaultParent(defaultParent);
 1150        view.SetToDefaultParent();
 151
 1152        view.OnEditorPressed += OnEditorPressed;
 1153        view.OnSettingsPressed += OnSceneSettingsPressed;
 1154        view.OnExpandMenuPressed += ExpandMenuPressed;
 155
 1156        return view;
 157    }
 158
 159    private void DestroyCardView(IProjectCardView projectCardView)
 160    {
 161        // NOTE: there is actually no need to unsubscribe here, but, just in case...
 2162        projectCardView.OnEditorPressed -= OnEditorPressed;
 2163        projectCardView.OnSettingsPressed -= OnSceneSettingsPressed;
 2164        projectCardView.OnExpandMenuPressed -= ExpandMenuPressed;
 165
 2166        projectCardView.Dispose();
 2167    }
 168
 0169    private void OnSceneSettingsPressed(ProjectData sceneData) {  }
 170
 171    public void Dispose()
 172    {
 10173        foreach (var projectCard in this.projects.Values)
 174        {
 2175            DestroyCardView(projectCard);
 176        }
 3177    }
 178}