< Summary

Class:SectionScenesController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/SectionController/MenuSections/SectionScenesController.cs
Covered lines:64
Uncovered lines:14
Coverable lines:78
Total lines:170
Line coverage:82% (64 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionScenesController()0%110100%
SetViewContainer(...)0%2100%
Dispose()0%110100%
OnShow()0%2100%
OnHide()0%12300%
ViewDirty()0%110100%
OnSetScenes(...)0%220100%
OnSetScenes(...)0%220100%
OnSceneAdded(...)0%110100%
OnSceneAdded(...)0%2100%
OnSceneRemoved(...)0%110100%
OnSceneRemoved(...)0%110100%
OnSearchResult(...)0%330100%
SetResult(...)0%550100%
UpdateDictionary(...)0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/SectionController/MenuSections/SectionScenesController.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6using Object = UnityEngine.Object;
 7
 8internal class SectionScenesController : SectionBase, IDeployedSceneListener, IProjectSceneListener, ISectionOpenSection
 9{
 10    public event Action<SectionId> OnRequestOpenSection;
 11
 12    internal const int MAX_CARDS = 3;
 13    internal readonly SectionScenesView view;
 14
 15    private bool hasScenes = false;
 16
 017    public override ISectionSearchHandler searchHandler => hasScenes ? sceneSearchHandler : null;
 018    public override SearchBarConfig searchBarConfig => new SearchBarConfig()
 19    {
 20        showFilterContributor = false,
 21        showFilterOperator = false,
 22        showFilterOwner = false,
 23        showResultLabel = false
 24    };
 25
 326    private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler();
 27
 28    internal Dictionary<string, ISceneCardView> deployedViews;
 29    internal Dictionary<string, ISceneCardView> projectViews;
 330    private List<ISearchInfo> searchList = new List<ISearchInfo>();
 31
 332    public SectionScenesController()
 33    {
 334        var prefab = Resources.Load<SectionScenesView>("BuilderProjectsPanelMenuSections/SectionScenesView");
 335        view = Object.Instantiate(prefab);
 36
 337        view.btnProjectsViewAll.onClick.AddListener(() => OnRequestOpenSection?.Invoke(SectionId.SCENES_PROJECT));
 338        view.btnInWorldViewAll.onClick.AddListener(() => OnRequestOpenSection?.Invoke(SectionId.SCENES_DEPLOYED));
 39
 340        sceneSearchHandler.OnResult += OnSearchResult;
 341    }
 42
 43    public override void SetViewContainer(Transform viewContainer)
 44    {
 045        view.transform.SetParent(viewContainer);
 046        view.transform.ResetLocalTRS();
 047    }
 48
 649    public override void Dispose() { view.Dispose(); }
 50
 051    protected override void OnShow() { view.gameObject.SetActive(true); }
 52
 53    protected override void OnHide()
 54    {
 055        view.gameObject.SetActive(false);
 056        searchList.Clear();
 057        deployedViews?.Clear();
 058        projectViews?.Clear();
 059    }
 60
 61    private void ViewDirty()
 62    {
 1863        bool hasDeployedScenes = view.deployedSceneContainer.childCount > 0;
 1864        bool hasProjectScenes = view.projectSceneContainer.childCount > 0;
 1865        hasScenes = hasDeployedScenes || hasProjectScenes;
 66
 1867        view.contentScreen.SetActive(hasScenes);
 1868        view.emptyScreen.SetActive(!hasScenes);
 1869        view.inWorldContainer.SetActive(hasDeployedScenes);
 1870        view.projectsContainer.SetActive(hasProjectScenes);
 1871    }
 72
 73    void IDeployedSceneListener.OnSetScenes(Dictionary<string, ISceneCardView> scenes)
 74    {
 675        UpdateDictionary(ref deployedViews, scenes);
 1076        searchList.AddRange(scenes.Values.Select(scene => scene.searchInfo));
 677        sceneSearchHandler.SetSearchableList(searchList);
 678    }
 79
 80    void IProjectSceneListener.OnSetScenes(Dictionary<string, ISceneCardView> scenes)
 81    {
 582        UpdateDictionary(ref projectViews, scenes);
 683        searchList.AddRange(scenes.Values.Select(scene => scene.searchInfo));
 584        sceneSearchHandler.SetSearchableList(searchList);
 585    }
 86
 87    void IDeployedSceneListener.OnSceneAdded(ISceneCardView scene)
 88    {
 189        deployedViews.Add(scene.sceneData.id, scene);
 190        sceneSearchHandler.AddItem(scene.searchInfo);
 191    }
 92
 93    void IProjectSceneListener.OnSceneAdded(ISceneCardView scene)
 94    {
 095        projectViews.Add(scene.sceneData.id, scene);
 096        sceneSearchHandler.AddItem(scene.searchInfo);
 097    }
 98
 99    void IDeployedSceneListener.OnSceneRemoved(ISceneCardView scene)
 100    {
 5101        scene.SetToDefaultParent();
 5102        deployedViews.Remove(scene.sceneData.id);
 5103        sceneSearchHandler.RemoveItem(scene.searchInfo);
 5104    }
 105
 106    void IProjectSceneListener.OnSceneRemoved(ISceneCardView scene)
 107    {
 1108        scene.SetToDefaultParent();
 1109        projectViews.Remove(scene.sceneData.id);
 1110        sceneSearchHandler.RemoveItem(scene.searchInfo);
 1111    }
 112
 113    private void OnSearchResult(List<ISearchInfo> searchInfoScenes)
 114    {
 18115        if (deployedViews != null)
 10116            SetResult(deployedViews, searchInfoScenes, view.deployedSceneContainer);
 117
 18118        if (projectViews != null)
 11119            SetResult(projectViews, searchInfoScenes, view.projectSceneContainer);
 120
 18121        ViewDirty();
 18122    }
 123
 124    private void SetResult(Dictionary<string, ISceneCardView> scenesViews, List<ISearchInfo> searchInfoScenes,
 125        Transform parent)
 126    {
 21127        int count = 0;
 128
 124129        for (int i = 0; i < searchInfoScenes.Count; i++)
 130        {
 41131            if (!scenesViews.TryGetValue(searchInfoScenes[i].id, out ISceneCardView sceneView))
 132            {
 133                continue;
 134            }
 135
 21136            sceneView.SetParent(parent);
 21137            sceneView.SetSiblingIndex(count);
 21138            sceneView.SetActive(false);
 21139            count++;
 140        }
 141
 84142        for (int i = 0; i < parent.childCount; i++)
 143        {
 21144            parent.GetChild(i).gameObject.SetActive(i < count && i < MAX_CARDS);
 145        }
 21146    }
 147
 148    private void UpdateDictionary(ref Dictionary<string, ISceneCardView> target, Dictionary<string, ISceneCardView> newD
 149    {
 11150        if (newData.Count == 0)
 8151            return;
 152
 3153        if (target == null)
 154        {
 2155            target = new Dictionary<string, ISceneCardView>(newData);
 2156            return;
 157        }
 158
 1159        using (var iterator = newData.GetEnumerator())
 160        {
 2161            while (iterator.MoveNext())
 162            {
 1163                if (target.ContainsKey(iterator.Current.Key))
 164                    continue;
 165
 1166                target.Add(iterator.Current.Key, iterator.Current.Value);
 167            }
 1168        }
 1169    }
 170}