< Summary

Class:SectionsController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/SectionController/SectionsController.cs
Covered lines:52
Uncovered lines:20
Coverable lines:72
Total lines:222
Line coverage:72.2% (52 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionsController(...)0%110100%
SectionsController(...)0%110100%
GetOrLoadSection(...)0%4.024090%
OpenSection(...)0%330100%
SetFetchingDataStart()0%110100%
SetFetchingDataEnd()0%2100%
SetIsLoading(...)0%2.022083.33%
OpenSection(...)0%6.046090%
Dispose()0%220100%
OnHideContextMenuRequested()0%6200%
OnUpdateSceneDataRequested(...)0%6200%
OnUpdateSceneContributorsRequested(...)0%6200%
OnUpdateSceneAdminsRequested(...)0%6200%
OnUpdateSceneBannedUsersRequested(...)0%6200%
OnOpenUrlRequested(...)0%6200%
OnGoToCoordsRequested(...)0%6200%
OnEditSceneAtCoordsRequested(...)0%6200%
SubscribeEvents(...)0%17.4710057.89%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5internal enum SectionId
 6{
 7    SCENES_MAIN,
 8    SCENES_DEPLOYED,
 9    SCENES_PROJECT,
 10    LAND,
 11    SETTINGS_PROJECT_GENERAL,
 12    SETTINGS_PROJECT_CONTRIBUTORS,
 13    SETTINGS_PROJECT_ADMIN
 14}
 15
 16internal interface ISectionsController : IDisposable
 17{
 18    event Action<SectionBase> OnSectionLoaded;
 19    event Action<SectionBase> OnSectionShow;
 20    event Action<SectionBase> OnSectionHide;
 21    event Action OnRequestContextMenuHide;
 22    event Action<SectionId> OnOpenSectionId;
 23    event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 24    event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 25    event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 26    event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 27    event Action<string> OnRequestOpenUrl;
 28    event Action<Vector2Int> OnRequestGoToCoords;
 29    event Action<Vector2Int> OnRequestEditSceneAtCoords;
 30    void OpenSection(SectionId id);
 31    void SetFetchingDataStart();
 32    void SetFetchingDataEnd();
 33}
 34
 35/// <summary>
 36/// This class is in charge of handling open/close of the different menu sections
 37/// </summary>
 38internal class SectionsController : ISectionsController
 39{
 40    public event Action<SectionBase> OnSectionLoaded;
 41    public event Action<SectionBase> OnSectionShow;
 42    public event Action<SectionBase> OnSectionHide;
 43    public event Action OnRequestContextMenuHide;
 44    public event Action<SectionId> OnOpenSectionId;
 45    public event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 46    public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 47    public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 48    public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 49    public event Action<string> OnRequestOpenUrl;
 50    public event Action<Vector2Int> OnRequestGoToCoords;
 51    public event Action<Vector2Int> OnRequestEditSceneAtCoords;
 52
 353    private Dictionary<SectionId, SectionBase> loadedSections = new Dictionary<SectionId, SectionBase>();
 54    private Transform sectionsParent;
 55    private ISectionFactory sectionFactory;
 56    private SectionBase currentOpenSection;
 57    private bool isLoading = false;
 58
 59    /// <summary>
 60    /// Ctor
 61    /// </summary>
 62    /// <param name="sectionsParent">container for the different sections view</param>
 263    public SectionsController(Transform sectionsParent) : this(new SectionFactory(), sectionsParent) { }
 64
 65    /// <summary>
 66    /// Ctor
 67    /// </summary>
 68    /// <param name="sectionFactory">factory that instantiates menu sections</param>
 69    /// <param name="sectionsParent">container for the different sections view</param>
 370    public SectionsController(ISectionFactory sectionFactory, Transform sectionsParent)
 71    {
 372        this.sectionsParent = sectionsParent;
 373        this.sectionFactory = sectionFactory;
 374    }
 75
 76    /// <summary>
 77    /// Get (load if not already loaded) the controller for certain menu section id
 78    /// </summary>
 79    /// <param name="id">id of the controller to get</param>
 80    /// <returns></returns>
 81    public SectionBase GetOrLoadSection(SectionId id)
 82    {
 483        if (loadedSections.TryGetValue(id, out SectionBase section))
 84        {
 085            return section;
 86        }
 87
 488        section = sectionFactory.GetSectionController(id);
 489        if (section != null)
 90        {
 491            section.SetViewContainer(sectionsParent);
 492            section.SetFetchingDataState(isLoading);
 493            SubscribeEvents(section);
 94        }
 95
 496        loadedSections.Add(id, section);
 497        OnSectionLoaded?.Invoke(section);
 498        return section;
 99    }
 100
 101    /// <summary>
 102    /// Opens (make visible) a menu section. It will load it if necessary.
 103    /// Closes (hides) the previously open section.
 104    /// </summary>
 105    /// <param name="id">id of the section to show</param>
 106    public void OpenSection(SectionId id)
 107    {
 4108        var section = GetOrLoadSection(id);
 4109        var success = OpenSection(section);
 4110        if (success)
 111        {
 4112            OnOpenSectionId?.Invoke(id);
 113        }
 1114    }
 115
 2116    public void SetFetchingDataStart() { SetIsLoading(true); }
 117
 0118    public void SetFetchingDataEnd() { SetIsLoading(false); }
 119
 120    private void SetIsLoading(bool isLoading)
 121    {
 1122        this.isLoading = isLoading;
 1123        using (var iterator = loadedSections.GetEnumerator())
 124        {
 1125            while (iterator.MoveNext())
 126            {
 0127                iterator.Current.Value.SetFetchingDataState(isLoading);
 128            }
 1129        }
 1130    }
 131
 132    private bool OpenSection(SectionBase section)
 133    {
 4134        if (currentOpenSection == section)
 0135            return false;
 136
 4137        if (currentOpenSection != null)
 138        {
 1139            currentOpenSection.SetVisible(false);
 1140            OnSectionHide?.Invoke(currentOpenSection);
 141        }
 142
 4143        currentOpenSection = section;
 144
 4145        if (currentOpenSection != null)
 146        {
 4147            currentOpenSection.SetVisible(true);
 4148            OnSectionShow?.Invoke(currentOpenSection);
 149        }
 150
 4151        return true;
 152    }
 153
 154    public void Dispose()
 155    {
 3156        using (var iterator = loadedSections.GetEnumerator())
 157        {
 7158            while (iterator.MoveNext())
 159            {
 4160                iterator.Current.Value.Dispose();
 161            }
 3162        }
 163
 3164        loadedSections.Clear();
 3165    }
 166
 0167    private void OnHideContextMenuRequested() { OnRequestContextMenuHide?.Invoke(); }
 168
 0169    private void OnUpdateSceneDataRequested(string id, SceneDataUpdatePayload payload) { OnRequestUpdateSceneData?.Invok
 170
 0171    private void OnUpdateSceneContributorsRequested(string id, SceneContributorsUpdatePayload payload) { OnRequestUpdate
 172
 0173    private void OnUpdateSceneAdminsRequested(string id, SceneAdminsUpdatePayload payload) { OnRequestUpdateSceneAdmins?
 174
 0175    private void OnUpdateSceneBannedUsersRequested(string id, SceneBannedUsersUpdatePayload payload) { OnRequestUpdateSc
 176
 0177    private void OnOpenUrlRequested(string url) { OnRequestOpenUrl?.Invoke(url); }
 178
 0179    private void OnGoToCoordsRequested(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); }
 180
 0181    private void OnEditSceneAtCoordsRequested(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); }
 182
 183    private void SubscribeEvents(SectionBase sectionBase)
 184    {
 4185        if (sectionBase is ISectionOpenSectionRequester openSectionRequester)
 186        {
 0187            openSectionRequester.OnRequestOpenSection += OpenSection;
 188        }
 4189        if (sectionBase is ISectionHideContextMenuRequester hideContextMenuRequester)
 190        {
 1191            hideContextMenuRequester.OnRequestContextMenuHide += OnHideContextMenuRequested;
 192        }
 4193        if (sectionBase is ISectionUpdateSceneDataRequester updateSceneDataRequester)
 194        {
 0195            updateSceneDataRequester.OnRequestUpdateSceneData += OnUpdateSceneDataRequested;
 196        }
 4197        if (sectionBase is ISectionUpdateSceneContributorsRequester updateSceneContributorsRequester)
 198        {
 0199            updateSceneContributorsRequester.OnRequestUpdateSceneContributors += OnUpdateSceneContributorsRequested;
 200        }
 4201        if (sectionBase is ISectionUpdateSceneAdminsRequester updateSceneAdminsRequester)
 202        {
 0203            updateSceneAdminsRequester.OnRequestUpdateSceneAdmins += OnUpdateSceneAdminsRequested;
 204        }
 4205        if (sectionBase is ISectionUpdateSceneBannedUsersRequester updateSceneBannedUsersRequester)
 206        {
 0207            updateSceneBannedUsersRequester.OnRequestUpdateSceneBannedUsers += OnUpdateSceneBannedUsersRequested;
 208        }
 4209        if (sectionBase is ISectionOpenURLRequester openURLRequester)
 210        {
 0211            openURLRequester.OnRequestOpenUrl += OnOpenUrlRequested;
 212        }
 4213        if (sectionBase is ISectionGotoCoordsRequester goToRequester)
 214        {
 0215            goToRequester.OnRequestGoToCoords += OnGoToCoordsRequested;
 216        }
 4217        if (sectionBase is ISectionEditSceneAtCoordsRequester editSceneRequester)
 218        {
 0219            editSceneRequester.OnRequestEditSceneAtCoords += OnEditSceneAtCoordsRequested;
 220        }
 4221    }
 222}