< Summary

Class:DCL.Builder.SectionsController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/SectionsController.cs
Covered lines:48
Uncovered lines:34
Coverable lines:82
Total lines:247
Line coverage:58.5% (48 of 82)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionsController(...)0%110100%
SectionsController(...)0%2100%
GetOrLoadSection(...)0%4.024090%
ContentEmpty()0%6200%
ContentNotEmpty()0%6200%
OpenSection(...)0%3.073080%
SetFetchingDataStart()0%2100%
SetFetchingDataStart[T]()0%2100%
SetFetchingDataEnd[T]()0%2100%
SetIsLoading[T](...)0%12300%
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%
CreateProjectRequest()0%6200%
OnGoToCoordsRequested(...)0%6200%
OnEditSceneAtCoordsRequested(...)0%6200%
SubscribeEvents(...)0%20.9511056.52%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/SectionsController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Builder;
 4using UnityEngine;
 5
 6namespace DCL.Builder
 7{
 8    internal enum SectionId
 9    {
 10        SCENES,
 11        PROJECTS,
 12        LAND,
 13        SETTINGS_PROJECT_GENERAL,
 14        SETTINGS_PROJECT_CONTRIBUTORS,
 15        SETTINGS_PROJECT_ADMIN
 16    }
 17
 18    internal interface ISectionsController : IDisposable
 19    {
 20        event Action OnSectionContentEmpty;
 21        event Action OnSectionContentNotEmpty;
 22        event Action<SectionBase> OnSectionLoaded;
 23        event Action<SectionBase> OnSectionShow;
 24        event Action<SectionBase> OnSectionHide;
 25        event Action OnRequestContextMenuHide;
 26        event Action OnCreateProjectRequest;
 27        event Action<SectionId> OnOpenSectionId;
 28        event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 29        event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 30        event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 31        event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 32        event Action<string> OnRequestOpenUrl;
 33        event Action<Vector2Int> OnRequestGoToCoords;
 34        event Action<Vector2Int> OnRequestEditSceneAtCoords;
 35        void OpenSection(SectionId id);
 36        void SetFetchingDataStart();
 37        void SetFetchingDataStart<T>() where T : SectionBase;
 38        void SetFetchingDataEnd<T>() where T : SectionBase;
 39    }
 40
 41    /// <summary>
 42    /// This class is in charge of handling open/close of the different menu sections
 43    /// </summary>
 44    internal class SectionsController : ISectionsController
 45    {
 46        public event Action OnSectionContentEmpty;
 47        public event Action OnSectionContentNotEmpty;
 48        public event Action<SectionBase> OnSectionLoaded;
 49        public event Action<SectionBase> OnSectionShow;
 50        public event Action<SectionBase> OnSectionHide;
 51        public event Action OnRequestContextMenuHide;
 52        public event Action OnCreateProjectRequest;
 53        public event Action<SectionId> OnOpenSectionId;
 54        public event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 55        public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 56        public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 57        public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 58        public event Action<string> OnRequestOpenUrl;
 59        public event Action<Vector2Int> OnRequestGoToCoords;
 60        public event Action<Vector2Int> OnRequestEditSceneAtCoords;
 61
 262        private Dictionary<SectionId, SectionBase> loadedSections = new Dictionary<SectionId, SectionBase>();
 63        private Transform sectionsParent;
 64        private ISectionFactory sectionFactory;
 65        private SectionBase currentOpenSection;
 66
 67        /// <summary>
 68        /// Ctor
 69        /// </summary>
 70        /// <param name="sectionsParent">container for the different sections view</param>
 071        public SectionsController(Transform sectionsParent) : this(new SectionFactory(), sectionsParent) { }
 72
 73        /// <summary>
 74        /// Ctor
 75        /// </summary>
 76        /// <param name="sectionFactory">factory that instantiates menu sections</param>
 77        /// <param name="sectionsParent">container for the different sections view</param>
 278        public SectionsController(ISectionFactory sectionFactory, Transform sectionsParent)
 79        {
 280            this.sectionsParent = sectionsParent;
 281            this.sectionFactory = sectionFactory;
 282        }
 83
 84        /// <summary>
 85        /// Get (load if not already loaded) the controller for certain menu section id
 86        /// </summary>
 87        /// <param name="id">id of the controller to get</param>
 88        /// <returns></returns>
 89        public SectionBase GetOrLoadSection(SectionId id)
 90        {
 391            if (loadedSections.TryGetValue(id, out SectionBase section))
 92            {
 093                return section;
 94            }
 95
 396            section = sectionFactory.GetSectionController(id);
 397            if (section != null)
 98            {
 399                section.SetViewContainer(sectionsParent);
 3100                section.SetFetchingDataState(section.isLoading);
 3101                SubscribeEvents(section);
 102            }
 103
 3104            loadedSections.Add(id, section);
 3105            OnSectionLoaded?.Invoke(section);
 3106            return section;
 107        }
 108
 0109        public void ContentEmpty() => OnSectionContentEmpty?.Invoke();
 0110        public void ContentNotEmpty() => OnSectionContentNotEmpty?.Invoke();
 111
 112        /// <summary>
 113        /// Opens (make visible) a menu section. It will load it if necessary.
 114        /// Closes (hides) the previously open section.
 115        /// </summary>
 116        /// <param name="id">id of the section to show</param>
 117        public void OpenSection(SectionId id)
 118        {
 3119            var section = GetOrLoadSection(id);
 3120            var success = OpenSection(section);
 3121            if (success)
 122            {
 3123                OnOpenSectionId?.Invoke(id);
 124            }
 0125        }
 126
 0127        public void SetFetchingDataStart(){ SetIsLoading<SectionBase>(true); }
 0128        public void SetFetchingDataStart<T>() where T : SectionBase { SetIsLoading<T>(true); }
 129
 0130        public void SetFetchingDataEnd<T>() where T : SectionBase { SetIsLoading<T>(false); }
 131
 132        private void SetIsLoading<T>(bool isLoading) where T : SectionBase
 133        {
 0134            using (var iterator = loadedSections.GetEnumerator())
 135            {
 0136                while (iterator.MoveNext())
 137                {
 0138                    if(iterator.Current.Value is T sectionBase)
 0139                        sectionBase.SetFetchingDataState(isLoading);
 140                }
 0141            }
 0142        }
 143
 144        private bool OpenSection(SectionBase section)
 145        {
 3146            if (currentOpenSection == section)
 0147                return false;
 148
 3149            if (currentOpenSection != null)
 150            {
 1151                currentOpenSection.SetVisible(false);
 1152                OnSectionHide?.Invoke(currentOpenSection);
 153            }
 154
 3155            currentOpenSection = section;
 156
 3157            if (currentOpenSection != null)
 158            {
 3159                currentOpenSection.SetVisible(true);
 3160                OnSectionShow?.Invoke(currentOpenSection);
 161            }
 162
 3163            return true;
 164        }
 165
 166        public void Dispose()
 167        {
 2168            using (var iterator = loadedSections.GetEnumerator())
 169            {
 5170                while (iterator.MoveNext())
 171                {
 3172                    iterator.Current.Value.OnEmptyContent -= ContentEmpty;
 3173                    iterator.Current.Value.OnNotEmptyContent -= ContentNotEmpty;
 3174                    iterator.Current.Value.Dispose();
 175                }
 2176            }
 177
 2178            loadedSections.Clear();
 2179        }
 180
 0181        private void OnHideContextMenuRequested() { OnRequestContextMenuHide?.Invoke(); }
 182
 0183        private void OnUpdateSceneDataRequested(string id, SceneDataUpdatePayload payload) { OnRequestUpdateSceneData?.I
 184
 0185        private void OnUpdateSceneContributorsRequested(string id, SceneContributorsUpdatePayload payload) { OnRequestUp
 186
 0187        private void OnUpdateSceneAdminsRequested(string id, SceneAdminsUpdatePayload payload) { OnRequestUpdateSceneAdm
 188
 0189        private void OnUpdateSceneBannedUsersRequested(string id, SceneBannedUsersUpdatePayload payload) { OnRequestUpda
 190
 0191        private void OnOpenUrlRequested(string url) { OnRequestOpenUrl?.Invoke(url); }
 192
 0193        private void CreateProjectRequest() {OnCreateProjectRequest?.Invoke(); }
 194
 0195        private void OnGoToCoordsRequested(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); }
 196
 0197        private void OnEditSceneAtCoordsRequested(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); }
 198
 199        private void SubscribeEvents(SectionBase sectionBase)
 200        {
 3201            sectionBase.OnEmptyContent += ContentEmpty;
 3202            sectionBase.OnNotEmptyContent += ContentNotEmpty;
 203
 3204            if (sectionBase is ISectionOpenSectionRequester openSectionRequester)
 205            {
 0206                openSectionRequester.OnRequestOpenSection += OpenSection;
 207            }
 3208            if (sectionBase is ISectionHideContextMenuRequester hideContextMenuRequester)
 209            {
 0210                hideContextMenuRequester.OnRequestContextMenuHide += OnHideContextMenuRequested;
 211            }
 3212            if (sectionBase is ISectionUpdateSceneDataRequester updateSceneDataRequester)
 213            {
 0214                updateSceneDataRequester.OnRequestUpdateSceneData += OnUpdateSceneDataRequested;
 215            }
 3216            if (sectionBase is ISectionUpdateSceneContributorsRequester updateSceneContributorsRequester)
 217            {
 0218                updateSceneContributorsRequester.OnRequestUpdateSceneContributors += OnUpdateSceneContributorsRequested;
 219            }
 3220            if (sectionBase is ISectionUpdateSceneAdminsRequester updateSceneAdminsRequester)
 221            {
 0222                updateSceneAdminsRequester.OnRequestUpdateSceneAdmins += OnUpdateSceneAdminsRequested;
 223            }
 3224            if (sectionBase is ISectionUpdateSceneBannedUsersRequester updateSceneBannedUsersRequester)
 225            {
 0226                updateSceneBannedUsersRequester.OnRequestUpdateSceneBannedUsers += OnUpdateSceneBannedUsersRequested;
 227            }
 3228            if (sectionBase is ISectionOpenURLRequester openURLRequester)
 229            {
 0230                openURLRequester.OnRequestOpenUrl += OnOpenUrlRequested;
 231            }
 3232            if (sectionBase is ISectionGotoCoordsRequester goToRequester)
 233            {
 0234                goToRequester.OnRequestGoToCoords += OnGoToCoordsRequested;
 235            }
 3236            if (sectionBase is ISectionEditSceneAtCoordsRequester editSceneRequester)
 237            {
 0238                editSceneRequester.OnRequestEditSceneAtCoords += OnEditSceneAtCoordsRequested;
 239            }
 3240            if (sectionBase is ISectionProjectController sectionProjectController)
 241            {
 0242                sectionProjectController.OnCreateProjectRequest += CreateProjectRequest;
 243            }
 3244        }
 245    }
 246
 247}