< 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:29
Uncovered lines:13
Coverable lines:42
Total lines:161
Line coverage:69% (29 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsPanelHUDController()0%110100%
Initialize()0%110100%
CreateView()0%110100%
Dispose()0%220100%
SetVisibility(...)0%8.147071.43%
AddSection(...)0%330100%
OpenSection(...)0%6200%
OpenSection(...)0%220100%
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.Sections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.SettingsCommon;
 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
 75        public void Initialize()
 76        {
 277            view = CreateView();
 278            view.Initialize(this, this);
 279        }
 80        protected virtual SettingsPanelHUDView CreateView()
 81        {
 282            return SettingsPanelHUDView.Create();
 83        }
 84
 85        public void Dispose()
 86        {
 687            if (view != null)
 288                UnityEngine.Object.Destroy(view.gameObject);
 689        }
 90
 91        public void SetVisibility(bool visible)
 92        {
 193            if (!visible && view.isOpen)
 94            {
 095                OnClose?.Invoke();
 096            }
 197            else if (visible && !view.isOpen)
 98            {
 199                OnOpen?.Invoke();
 100            }
 101
 1102            view.SetVisibility(visible);
 1103        }
 104
 105        public void AddSection(
 106            SettingsButtonEntry newMenuButton,
 107            ISettingsSectionView newSection,
 108            ISettingsSectionController newSectionController,
 109            SettingsSectionModel sectionConfig)
 110        {
 3111            newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text);
 112
 3113            newSection.Initialize(newSectionController, sectionConfig.widgets.ToList());
 3114            newSection.SetActive(false);
 3115            sections.Add(newSection);
 116
 3117            newMenuButton?.ConfigureAction(() =>
 118            {
 0119                foreach (var button in menuButtons)
 120                {
 0121                    button.MarkAsSelected(false);
 122                }
 0123                newMenuButton.MarkAsSelected(true);
 124
 0125                OpenSection(newSection);
 0126            });
 127
 3128            menuButtons.Add(newMenuButton);
 3129        }
 130
 131        public void OpenSection(ISettingsSectionView sectionToOpen)
 132        {
 0133            foreach (var section in sections)
 134            {
 0135                section.SetActive(section == sectionToOpen);
 136            }
 0137        }
 138
 139        public void OpenSection(int sectionIndex)
 140        {
 18141            for (int i = 0; i < sections.Count; i++)
 142            {
 6143                var section = sections[i];
 6144                section.SetActive(i == sectionIndex);
 145            }
 3146        }
 147
 148        public void MarkMenuButtonAsSelected(int buttonIndex)
 149        {
 18150            for (int i = 0; i < menuButtons.Count; i++)
 151            {
 6152                var button = menuButtons[i];
 6153                button.MarkAsSelected(i == buttonIndex);
 154            }
 3155        }
 156
 0157        public virtual void SaveSettings() { Settings.i.SaveSettings(); }
 158
 0159        public void ResetAllSettings() { Settings.i.ResetAllSettings(); }
 160    }
 161}