< Summary

Class:DCL.SettingsPanelHUD.SettingsPanelHUDController
Assembly:SettingsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/SettingsPanelHUDController.cs
Covered lines:28
Uncovered lines:15
Coverable lines:43
Total lines:165
Line coverage:65.1% (28 of 43)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsPanelHUDController()0%110100%
Dispose()0%220100%
SetVisibility(...)0%8.147071.43%
Initialize()0%110100%
AddSection(...)0%330100%
OpenSection(...)0%12300%
OpenSection(...)0%330100%
MarkMenuButtonAsSelected(...)0%220100%
SaveSettings()0%2100%
ResetAllSettings()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/SettingsPanelHUDController.cs

#LineLine coverage
 1using DCL.SettingsPanelHUD.Common;
 2using DCL.SettingsPanelHUD.Sections;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6namespace DCL.SettingsPanelHUD
 7{
 8    /// <summary>
 9    /// Interface to implement a controller for the main settings panel.
 10    /// </summary>
 11    public interface ISettingsPanelHUDController
 12    {
 13        /// <summary>
 14        /// List of all SECTIONS added to the main settings panel.
 15        /// </summary>
 16        List<ISettingsSectionView> sections { get; }
 17
 18        /// <summary>
 19        /// All the needed logic to initializes the controller and its associated view, and make them works.
 20        /// </summary>
 21        void Initialize();
 22
 23        /// <summary>
 24        /// Adds a SECTION into the main settings panel.
 25        /// </summary>
 26        /// <param name="newMenuButton">MonoBehaviour that will represent the menu button associated to the new SECTION.
 27        /// <param name="newSection">New SECTION that will be added.</param>
 28        /// <param name="newSectionController">Controller belonging to the new SECTION.</param>
 29        /// <param name="sectionConfig">Model that will contain the configuration of the new SECTION.</param>
 30        void AddSection(SettingsButtonEntry newMenuButton, ISettingsSectionView newSection, ISettingsSectionController n
 31
 32        /// <summary>
 33        /// Opens a specific SECTION of the main settings panel.
 34        /// </summary>
 35        /// <param name="sectionToOpen">SECTION to be opened.</param>
 36        void OpenSection(ISettingsSectionView sectionToOpen);
 37
 38        /// <summary>
 39        /// Opens a specific SECTION of the main settings panel.
 40        /// </summary>
 41        /// <param name="sectionIndex">SECTION index to be opened. The index must be less than the section list length.<
 42        void OpenSection(int sectionIndex);
 43
 44        /// <summary>
 45        /// Mark a specific menu button as selected.
 46        /// </summary>
 47        /// <param name="buttonIndex">Menu button index to be selected.</param>
 48        void MarkMenuButtonAsSelected(int buttonIndex);
 49
 50        /// <summary>
 51        /// Save all the current settings values.
 52        /// </summary>
 53        void SaveSettings();
 54
 55        /// <summary>
 56        /// Reset all the current settings to their default values.
 57        /// </summary>
 58        void ResetAllSettings();
 59    }
 60
 61    /// <summary>
 62    /// This controller is in charge of manage all the logic related to the main settings panel.
 63    /// </summary>
 64    public class SettingsPanelHUDController : IHUD, ISettingsPanelHUDController
 65    {
 066        public SettingsPanelHUDView view { get; private set; }
 67
 68        public event System.Action OnOpen;
 69        public event System.Action OnClose;
 70
 271        public List<ISettingsSectionView> sections { get; } = new List<ISettingsSectionView>();
 72
 273        private List<SettingsButtonEntry> menuButtons = new List<SettingsButtonEntry>();
 74
 675        public SettingsPanelHUDController() { view = SettingsPanelHUDView.Create(); }
 76
 77        public void Dispose()
 78        {
 679            if (view != null)
 280                UnityEngine.Object.Destroy(view.gameObject);
 681        }
 82
 83        public void SetVisibility(bool visible)
 84        {
 185            if (!visible && view.isOpen)
 86            {
 087                OnClose?.Invoke();
 088            }
 189            else if (visible && !view.isOpen)
 90            {
 191                OnOpen?.Invoke();
 92            }
 93
 194            view.SetVisibility(visible);
 195        }
 96
 297        public void Initialize() { view.Initialize(this, this); }
 98
 99        public void AddSection(
 100            SettingsButtonEntry newMenuButton,
 101            ISettingsSectionView newSection,
 102            ISettingsSectionController newSectionController,
 103            SettingsSectionModel sectionConfig)
 104        {
 3105            newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text);
 106
 3107            newSection.Initialize(newSectionController, sectionConfig.widgets.ToList());
 3108            newSection.SetActive(false);
 3109            sections.Add(newSection);
 110
 3111            newMenuButton?.ConfigureAction(() =>
 112            {
 0113                foreach (var button in menuButtons)
 114                {
 0115                    button.MarkAsSelected(false);
 116                }
 0117                newMenuButton.MarkAsSelected(true);
 118
 0119                OpenSection(newSection);
 0120            });
 121
 3122            menuButtons.Add(newMenuButton);
 3123        }
 124
 125        public void OpenSection(ISettingsSectionView sectionToOpen)
 126        {
 0127            foreach (var section in sections)
 128            {
 0129                if (section == sectionToOpen)
 130                    continue;
 131
 0132                section.SetActive(false);
 133            }
 134
 0135            sectionToOpen.SetActive(true);
 0136        }
 137
 138        public void OpenSection(int sectionIndex)
 139        {
 16140            foreach (var section in sections)
 141            {
 6142                if (section == sections[sectionIndex])
 143                    continue;
 144
 4145                section.SetActive(false);
 146            }
 147
 2148            sections[sectionIndex].SetActive(true);
 2149        }
 150
 151        public void MarkMenuButtonAsSelected(int buttonIndex)
 152        {
 16153            foreach (var button in menuButtons)
 154            {
 6155                button.MarkAsSelected(false);
 156            }
 157
 2158            menuButtons[buttonIndex].MarkAsSelected(true);
 2159        }
 160
 0161        public void SaveSettings() { Settings.i.SaveSettings(); }
 162
 0163        public void ResetAllSettings() { Settings.i.ResetAllSettings(); }
 164    }
 165}