< Summary

Class:ExploreV2MenuComponentView
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/ExploreV2Menu/ExploreV2MenuComponentView.cs
Covered lines:73
Uncovered lines:47
Coverable lines:120
Total lines:294
Line coverage:60.8% (73 of 120)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Dispose()0%220100%
Awake()0%110100%
Start()0%110100%
Update()0%2100%
OnDestroy()0%220100%
Create()0%2100%
IsInitialized_OnChange(...)0%2.032080%
CreateSectionSelectorMappingsAfterDelay()0%5.673033.33%
CreateSectionSelectorMappings()0%440100%
OnSectionSelected(...)0%110100%
ResetSectionOpenLock()0%5.673033.33%
SetVisible(...)0%4.024090%
GoToFirstActiveSection()0%20400%
GoToSection(...)0%220100%
SetSectionActive(...)0%110100%
IsSectionActive(...)0%2100%
OnAfterShowAnimationCompleted(...)0%12300%
ConfigureEncapsulatedSection(...)0%220100%
RefreshControl()0%12300%
RemoveSectionSelectorMappings()0%440100%
IsSomeModalOpen_OnChange(...)0%220100%
CheckIfProfileCardShouldBeClosed()0%42600%
ConfigureCloseButton()0%110100%
OnCloseActionTriggered(...)0%220100%
ShowRealmSelectorModal()0%2100%
ConfigureRealmSelectorModal()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/ExploreV2Menu/ExploreV2MenuComponentView.cs

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using UnityEngine;
 6using UnityEngine.Events;
 7
 8public class ExploreV2MenuComponentView : BaseComponentView, IExploreV2MenuComponentView
 9{
 10    internal const string REALM_SELECTOR_MODAL_ID = "RealmSelector_Modal";
 11
 12    [Header("Assets References")]
 13    [SerializeField] internal RealmSelectorComponentView realmSelectorModalPrefab;
 14
 15    [Header("Top Menu")]
 16    [SerializeField] internal SectionSelectorComponentView sectionSelector;
 17    [SerializeField] internal ProfileCardComponentView profileCard;
 18    [SerializeField] internal RealmViewerComponentView realmViewer;
 19    [SerializeField] internal ButtonComponentView closeMenuButton;
 20    [SerializeField] internal InputAction_Trigger closeAction;
 21
 22    [Header("Sections")]
 23    [SerializeField] internal PlacesAndEventsSectionComponentView placesAndEventsSection;
 24    [SerializeField] internal FeatureEncapsulatorComponentView backpackSection;
 25    [SerializeField] internal FeatureEncapsulatorComponentView mapSection;
 26    [SerializeField] internal FeatureEncapsulatorComponentView questSection;
 27    [SerializeField] internal FeatureEncapsulatorComponentView settingsSection;
 28
 29    [Header("Tutorial References")]
 30    [SerializeField] internal RectTransform profileCardTooltipReference;
 31
 32    private DataStore_Camera cameraDataStore;
 33
 34    private Dictionary<ExploreSection, FeatureEncapsulatorComponentView> exploreSectionsById;
 35    private HUDCanvasCameraModeController hudCanvasCameraModeController;
 36
 37    private RectTransform profileCardRectTransform;
 38    private RealmSelectorComponentView realmSelectorModal;
 39    private bool isOpeningSectionThisFrame;
 40
 041    public IRealmViewerComponentView currentRealmViewer => realmViewer;
 042    public IRealmSelectorComponentView currentRealmSelectorModal => realmSelectorModal;
 43
 044    public IProfileCardComponentView currentProfileCard => profileCard;
 045    public IPlacesAndEventsSectionComponentView currentPlacesAndEventsSection => placesAndEventsSection;
 46
 047    public RectTransform currentTopMenuTooltipReference => sectionSelector.GetSection((int)ExploreSection.Explore).pivot
 048    public RectTransform currentPlacesAndEventsTooltipReference => sectionSelector.GetSection((int)ExploreSection.Explor
 049    public RectTransform currentBackpackTooltipReference => sectionSelector.GetSection((int)ExploreSection.Backpack).piv
 050    public RectTransform currentMapTooltipReference => sectionSelector.GetSection((int)ExploreSection.Map).pivot;
 051    public RectTransform currentQuestTooltipReference => sectionSelector.GetSection((int)ExploreSection.Quest).pivot;
 052    public RectTransform currentSettingsTooltipReference => sectionSelector.GetSection((int)ExploreSection.Settings).piv
 053    public RectTransform currentProfileCardTooltipReference => profileCardTooltipReference;
 54
 55    public event Action<bool> OnCloseButtonPressed;
 56    public event Action<ExploreSection> OnSectionOpen;
 57    public event Action OnAfterShowAnimation;
 58
 59    public override void Dispose()
 60    {
 2961        base.Dispose();
 62
 2963        RemoveSectionSelectorMappings();
 2964        closeMenuButton.onClick.RemoveAllListeners();
 2965        closeAction.OnTriggered -= OnCloseActionTriggered;
 2966        DataStore.i.exploreV2.isSomeModalOpen.OnChange -= IsSomeModalOpen_OnChange;
 2967        DataStore.i.exploreV2.isInitialized.OnChange -= IsInitialized_OnChange;
 68
 2969        if (realmSelectorModal != null)
 2970            realmSelectorModal.Dispose();
 2971    }
 72
 73    public override void Awake()
 74    {
 2975        base.Awake();
 2976        showHideAnimator.OnWillFinishStart += OnAfterShowAnimationCompleted;
 77
 2978        profileCardRectTransform = profileCard.GetComponent<RectTransform>();
 2979        realmSelectorModal = ConfigureRealmSelectorModal();
 2980        hudCanvasCameraModeController = new HUDCanvasCameraModeController(GetComponent<Canvas>(), DataStore.i.camera.hud
 81
 2982        exploreSectionsById = new Dictionary<ExploreSection, FeatureEncapsulatorComponentView>
 83        {
 84            { ExploreSection.Explore, null },
 85            { ExploreSection.Backpack, backpackSection },
 86            { ExploreSection.Map, mapSection },
 87            { ExploreSection.Quest, questSection },
 88            { ExploreSection.Settings, settingsSection },
 89        };
 2990    }
 91
 92    public override void Start()
 93    {
 2994        DataStore.i.exploreV2.isInitialized.OnChange += IsInitialized_OnChange;
 2995        IsInitialized_OnChange(DataStore.i.exploreV2.isInitialized.Get(), false);
 96
 2997        ConfigureCloseButton();
 2998    }
 99
 100    public override void Update() =>
 0101        CheckIfProfileCardShouldBeClosed();
 102
 103    public void OnDestroy() =>
 29104        hudCanvasCameraModeController?.Dispose();
 105
 106    internal static ExploreV2MenuComponentView Create()
 107    {
 0108        ExploreV2MenuComponentView exploreV2View = Instantiate(Resources.Load<GameObject>("MainMenu/ExploreV2Menu")).Get
 0109        exploreV2View.name = "_ExploreV2";
 110
 0111        return exploreV2View;
 112    }
 113
 114    private void IsInitialized_OnChange(bool current, bool previous)
 115    {
 29116        if (!current)
 0117            return;
 118
 29119        DataStore.i.exploreV2.isInitialized.OnChange -= IsInitialized_OnChange;
 29120        StartCoroutine(CreateSectionSelectorMappingsAfterDelay());
 29121    }
 122
 123    private IEnumerator CreateSectionSelectorMappingsAfterDelay()
 124    {
 29125        yield return null;
 0126        CreateSectionSelectorMappings();
 0127    }
 128
 129    internal void CreateSectionSelectorMappings()
 130    {
 120131        foreach (ExploreSection sectionId in Enum.GetValues(typeof(ExploreSection)))
 50132            sectionSelector.GetSection((int)sectionId)
 133                          ?.onSelect.AddListener(OnSectionSelected(sectionId));
 10134    }
 135
 136    private UnityAction<bool> OnSectionSelected(ExploreSection sectionId) =>
 50137        isOn =>
 138        {
 5139            FeatureEncapsulatorComponentView sectionView = exploreSectionsById[sectionId];
 140
 5141            if (isOn)
 142            {
 5143                if (isOpeningSectionThisFrame)
 0144                    return;
 145
 5146                isOpeningSectionThisFrame = true;
 5147                StartCoroutine(ResetSectionOpenLock());
 148
 149                // If not an explorer Section, because we do not Show/Hide it
 5150                if (sectionView != null)
 4151                    sectionView.Show();
 152
 5153                OnSectionOpen?.Invoke(sectionId);
 154            }
 0155            else if (sectionView != null) // If not an explorer Section, because we do not Show/Hide it
 156            {
 0157                sectionView.Hide();
 158            }
 0159        };
 160
 161    private IEnumerator ResetSectionOpenLock()
 162    {
 5163        yield return null;
 0164        isOpeningSectionThisFrame = false;
 0165    }
 166
 167    public void SetVisible(bool visible)
 168    {
 2169        if (visible)
 170        {
 1171            DataStore.i.exploreV2.isInShowAnimationTransiton.Set(true);
 1172            Show();
 173
 1174            ISectionToggle sectionToGo = sectionSelector.GetSection(DataStore.i.exploreV2.currentSectionIndex.Get());
 175
 1176            if (sectionToGo != null && sectionToGo.IsActive())
 1177                GoToSection((ExploreSection)DataStore.i.exploreV2.currentSectionIndex.Get());
 178            else
 0179                GoToFirstActiveSection();
 180        }
 181        else
 182        {
 1183            Hide();
 1184            AudioScriptableObjects.dialogClose.Play(true);
 185        }
 1186    }
 187
 188    private void GoToFirstActiveSection()
 189    {
 0190        foreach (ISectionToggle section in sectionSelector.GetAllSections())
 0191            if (section != null && section.IsActive())
 192            {
 0193                section.SelectToggle(reselectIfAlreadyOn: true);
 0194                break;
 195            }
 0196    }
 197
 198    public void GoToSection(ExploreSection section)
 199    {
 1200        sectionSelector.GetSection((int)section)?.SelectToggle(reselectIfAlreadyOn: true);
 201
 1202        AudioScriptableObjects.dialogOpen.Play(true);
 1203        AudioScriptableObjects.listItemAppear.ResetPitch();
 1204    }
 205
 206    public void SetSectionActive(ExploreSection section, bool isActive) =>
 10207        sectionSelector.GetSection((int)section).SetActive(isActive);
 208
 209    public bool IsSectionActive(ExploreSection section) =>
 0210        sectionSelector.GetSection((int)section).IsActive();
 211
 212    private void OnAfterShowAnimationCompleted(ShowHideAnimator _)
 213    {
 0214        if (!DataStore.i.exploreV2.isOpen.Get())
 0215            return;
 216
 0217        DataStore.i.exploreV2.isInShowAnimationTransiton.Set(false);
 0218        OnAfterShowAnimation?.Invoke();
 0219    }
 220
 221    public void ConfigureEncapsulatedSection(ExploreSection sectionId, BaseVariable<Transform> featureConfiguratorFlag) 
 4222        exploreSectionsById[sectionId]?.EncapsulateFeature(featureConfiguratorFlag);
 223
 224    public override void RefreshControl()
 225    {
 0226        placesAndEventsSection.RefreshControl();
 227
 0228        foreach (ExploreSection section in exploreSectionsById.Keys)
 0229            exploreSectionsById[section]?.RefreshControl();
 0230    }
 231
 232    internal void RemoveSectionSelectorMappings()
 233    {
 408234        foreach (int sectionId in Enum.GetValues(typeof(ExploreSection)))
 170235            sectionSelector.GetSection(sectionId)?.onSelect.RemoveAllListeners();
 34236    }
 237
 238    private void IsSomeModalOpen_OnChange(bool current, bool previous)
 239    {
 28240        closeAction.OnTriggered -= OnCloseActionTriggered;
 241
 28242        if (!current)
 14243            closeAction.OnTriggered += OnCloseActionTriggered;
 28244    }
 245
 246    private void CheckIfProfileCardShouldBeClosed()
 247    {
 0248        if (!DataStore.i.exploreV2.profileCardIsOpen.Get())
 0249            return;
 250
 0251        cameraDataStore ??= DataStore.i.camera;
 252
 0253        if (Input.GetMouseButton(0) &&
 254            !RectTransformUtility.RectangleContainsScreenPoint(profileCardRectTransform, Input.mousePosition, cameraData
 255            !RectTransformUtility.RectangleContainsScreenPoint(HUDController.i.profileHud.view.ExpandedMenu, Input.mouse
 0256            DataStore.i.exploreV2.profileCardIsOpen.Set(false);
 0257    }
 258
 259    internal void ConfigureCloseButton()
 260    {
 33261        closeMenuButton.onClick.AddListener(() => OnCloseButtonPressed?.Invoke(false));
 31262        closeAction.OnTriggered += OnCloseActionTriggered;
 31263        DataStore.i.exploreV2.isSomeModalOpen.OnChange += IsSomeModalOpen_OnChange;
 31264    }
 265
 266    private void OnCloseActionTriggered(DCLAction_Trigger action) =>
 3267        OnCloseButtonPressed?.Invoke(true);
 268
 269    public void ShowRealmSelectorModal() =>
 0270        realmSelectorModal.Show();
 271
 272    /// <summary>
 273    /// Instantiates (if does not already exists) a realm selector modal from the given prefab.
 274    /// </summary>
 275    /// <returns>An instance of a realm modal modal.</returns>
 276    internal RealmSelectorComponentView ConfigureRealmSelectorModal()
 277    {
 278        RealmSelectorComponentView realmSelectorView;
 279
 30280        var existingModal = GameObject.Find(REALM_SELECTOR_MODAL_ID);
 281
 30282        if (existingModal != null)
 24283            realmSelectorView = existingModal.GetComponent<RealmSelectorComponentView>();
 284        else
 285        {
 6286            realmSelectorView = Instantiate(realmSelectorModalPrefab);
 6287            realmSelectorView.name = REALM_SELECTOR_MODAL_ID;
 288        }
 289
 30290        realmSelectorView.Hide(true);
 291
 30292        return realmSelectorView;
 293    }
 294}