< 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:44
Uncovered lines:31
Coverable lines:75
Total lines:233
Line coverage:58.6% (44 of 75)
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%
OpenSection(...)0%3.073080%
SetFetchingDataStart()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%24.0711052.38%

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<SectionBase> OnSectionLoaded;
 21        event Action<SectionBase> OnSectionShow;
 22        event Action<SectionBase> OnSectionHide;
 23        event Action OnRequestContextMenuHide;
 24        event Action OnCreateProjectRequest;
 25        event Action<SectionId> OnOpenSectionId;
 26        event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 27        event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 28        event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 29        event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 30        event Action<string> OnRequestOpenUrl;
 31        event Action<Vector2Int> OnRequestGoToCoords;
 32        event Action<Vector2Int> OnRequestEditSceneAtCoords;
 33        void OpenSection(SectionId id);
 34        void SetFetchingDataStart();
 35        void SetFetchingDataEnd<T>() where T : SectionBase;
 36    }
 37
 38    /// <summary>
 39    /// This class is in charge of handling open/close of the different menu sections
 40    /// </summary>
 41    internal class SectionsController : ISectionsController
 42    {
 43        public event Action<SectionBase> OnSectionLoaded;
 44        public event Action<SectionBase> OnSectionShow;
 45        public event Action<SectionBase> OnSectionHide;
 46        public event Action OnRequestContextMenuHide;
 47        public event Action OnCreateProjectRequest;
 48        public event Action<SectionId> OnOpenSectionId;
 49        public event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData;
 50        public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 51        public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 52        public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 53        public event Action<string> OnRequestOpenUrl;
 54        public event Action<Vector2Int> OnRequestGoToCoords;
 55        public event Action<Vector2Int> OnRequestEditSceneAtCoords;
 56
 257        private Dictionary<SectionId, SectionBase> loadedSections = new Dictionary<SectionId, SectionBase>();
 58        private Transform sectionsParent;
 59        private ISectionFactory sectionFactory;
 60        private SectionBase currentOpenSection;
 61
 62        /// <summary>
 63        /// Ctor
 64        /// </summary>
 65        /// <param name="sectionsParent">container for the different sections view</param>
 066        public SectionsController(Transform sectionsParent) : this(new SectionFactory(), sectionsParent) { }
 67
 68        /// <summary>
 69        /// Ctor
 70        /// </summary>
 71        /// <param name="sectionFactory">factory that instantiates menu sections</param>
 72        /// <param name="sectionsParent">container for the different sections view</param>
 273        public SectionsController(ISectionFactory sectionFactory, Transform sectionsParent)
 74        {
 275            this.sectionsParent = sectionsParent;
 276            this.sectionFactory = sectionFactory;
 277        }
 78
 79        /// <summary>
 80        /// Get (load if not already loaded) the controller for certain menu section id
 81        /// </summary>
 82        /// <param name="id">id of the controller to get</param>
 83        /// <returns></returns>
 84        public SectionBase GetOrLoadSection(SectionId id)
 85        {
 386            if (loadedSections.TryGetValue(id, out SectionBase section))
 87            {
 088                return section;
 89            }
 90
 391            section = sectionFactory.GetSectionController(id);
 392            if (section != null)
 93            {
 394                section.SetViewContainer(sectionsParent);
 395                section.SetFetchingDataState(section.isLoading);
 396                SubscribeEvents(section);
 97            }
 98
 399            loadedSections.Add(id, section);
 3100            OnSectionLoaded?.Invoke(section);
 3101            return section;
 102        }
 103
 104        /// <summary>
 105        /// Opens (make visible) a menu section. It will load it if necessary.
 106        /// Closes (hides) the previously open section.
 107        /// </summary>
 108        /// <param name="id">id of the section to show</param>
 109        public void OpenSection(SectionId id)
 110        {
 3111            var section = GetOrLoadSection(id);
 3112            var success = OpenSection(section);
 3113            if (success)
 114            {
 3115                OnOpenSectionId?.Invoke(id);
 116            }
 0117        }
 118
 0119        public void SetFetchingDataStart(){ SetIsLoading<SectionBase>(true); }
 120
 0121        public void SetFetchingDataEnd<T>() where T : SectionBase { SetIsLoading<T>(false); }
 122
 123        private void SetIsLoading<T>(bool isLoading) where T : SectionBase
 124        {
 0125            using (var iterator = loadedSections.GetEnumerator())
 126            {
 0127                while (iterator.MoveNext())
 128                {
 0129                    if(iterator.Current.Value is T sectionBase)
 0130                        sectionBase.SetFetchingDataState(isLoading);
 131                }
 0132            }
 0133        }
 134
 135        private bool OpenSection(SectionBase section)
 136        {
 3137            if (currentOpenSection == section)
 0138                return false;
 139
 3140            if (currentOpenSection != null)
 141            {
 1142                currentOpenSection.SetVisible(false);
 1143                OnSectionHide?.Invoke(currentOpenSection);
 144            }
 145
 3146            currentOpenSection = section;
 147
 3148            if (currentOpenSection != null)
 149            {
 3150                currentOpenSection.SetVisible(true);
 3151                OnSectionShow?.Invoke(currentOpenSection);
 152            }
 153
 3154            return true;
 155        }
 156
 157        public void Dispose()
 158        {
 2159            using (var iterator = loadedSections.GetEnumerator())
 160            {
 5161                while (iterator.MoveNext())
 162                {
 3163                    iterator.Current.Value.Dispose();
 164                }
 2165            }
 166
 2167            loadedSections.Clear();
 2168        }
 169
 0170        private void OnHideContextMenuRequested() { OnRequestContextMenuHide?.Invoke(); }
 171
 0172        private void OnUpdateSceneDataRequested(string id, SceneDataUpdatePayload payload) { OnRequestUpdateSceneData?.I
 173
 0174        private void OnUpdateSceneContributorsRequested(string id, SceneContributorsUpdatePayload payload) { OnRequestUp
 175
 0176        private void OnUpdateSceneAdminsRequested(string id, SceneAdminsUpdatePayload payload) { OnRequestUpdateSceneAdm
 177
 0178        private void OnUpdateSceneBannedUsersRequested(string id, SceneBannedUsersUpdatePayload payload) { OnRequestUpda
 179
 0180        private void OnOpenUrlRequested(string url) { OnRequestOpenUrl?.Invoke(url); }
 181
 0182        private void CreateProjectRequest() {OnCreateProjectRequest?.Invoke(); }
 183
 0184        private void OnGoToCoordsRequested(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); }
 185
 0186        private void OnEditSceneAtCoordsRequested(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); }
 187
 188        private void SubscribeEvents(SectionBase sectionBase)
 189        {
 3190            if (sectionBase is ISectionOpenSectionRequester openSectionRequester)
 191            {
 0192                openSectionRequester.OnRequestOpenSection += OpenSection;
 193            }
 3194            if (sectionBase is ISectionHideContextMenuRequester hideContextMenuRequester)
 195            {
 0196                hideContextMenuRequester.OnRequestContextMenuHide += OnHideContextMenuRequested;
 197            }
 3198            if (sectionBase is ISectionUpdateSceneDataRequester updateSceneDataRequester)
 199            {
 0200                updateSceneDataRequester.OnRequestUpdateSceneData += OnUpdateSceneDataRequested;
 201            }
 3202            if (sectionBase is ISectionUpdateSceneContributorsRequester updateSceneContributorsRequester)
 203            {
 0204                updateSceneContributorsRequester.OnRequestUpdateSceneContributors += OnUpdateSceneContributorsRequested;
 205            }
 3206            if (sectionBase is ISectionUpdateSceneAdminsRequester updateSceneAdminsRequester)
 207            {
 0208                updateSceneAdminsRequester.OnRequestUpdateSceneAdmins += OnUpdateSceneAdminsRequested;
 209            }
 3210            if (sectionBase is ISectionUpdateSceneBannedUsersRequester updateSceneBannedUsersRequester)
 211            {
 0212                updateSceneBannedUsersRequester.OnRequestUpdateSceneBannedUsers += OnUpdateSceneBannedUsersRequested;
 213            }
 3214            if (sectionBase is ISectionOpenURLRequester openURLRequester)
 215            {
 0216                openURLRequester.OnRequestOpenUrl += OnOpenUrlRequested;
 217            }
 3218            if (sectionBase is ISectionGotoCoordsRequester goToRequester)
 219            {
 0220                goToRequester.OnRequestGoToCoords += OnGoToCoordsRequested;
 221            }
 3222            if (sectionBase is ISectionEditSceneAtCoordsRequester editSceneRequester)
 223            {
 0224                editSceneRequester.OnRequestEditSceneAtCoords += OnEditSceneAtCoordsRequested;
 225            }
 3226            if (sectionBase is ISectionProjectController sectionProjectController)
 227            {
 0228                sectionProjectController.OnCreateProjectRequest += CreateProjectRequest;
 229            }
 3230        }
 231    }
 232
 233}