< Summary

Class:ExploreV2MenuComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/ExploreV2Menu/ExploreV2MenuComponentController.cs
Covered lines:147
Uncovered lines:9
Coverable lines:156
Total lines:306
Line coverage:94.2% (147 of 156)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize()0%10100100%
Dispose()0%880100%
CreateView()0%2100%
CreateAnalyticsController()0%2100%
InitializePlacesAndEventsSection()0%2.032080%
OnSectionInitializedChanged(...)0%110100%
SectionInitializedChanged(...)0%2.062075%
SetVisibility(...)0%110100%
SetVisibilityOnOpenChanged(...)0%110100%
SetMenuTargetVisibility(...)0%440100%
SetSectionTargetVisibility(...)0%220100%
SetVisibility_Internal(...)0%7.17087.5%
OnSectionVisibilityChanged(...)0%550100%
ChangeVisibilityVarForSwitchedSections()0%330100%
OnSectionOpen(...)0%330100%
CurrentSectionIndexChanged(...)0%4.254075%
OnAfterShowAnimation()0%2100%
UpdateProfileInfo(...)0%110100%
OnCloseButtonPressed(...)0%330100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using ExploreV2Analytics;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using UnityEngine;
 7
 8/// <summary>
 9/// Main controller for the feature "Explore V2".
 10/// Controls different sections: Maps, Backpack, Settings
 11/// their initialization and switching between them
 12/// </summary>
 13public class ExploreV2MenuComponentController : IExploreV2MenuComponentController
 14{
 15    internal const ExploreSection DEFAULT_SECTION = ExploreSection.Explore;
 16
 17    internal IExploreV2MenuComponentView view;
 18    internal IExploreV2Analytics exploreV2Analytics;
 19
 20    internal IPlacesAndEventsSectionComponentController placesAndEventsSectionController;
 21
 22    internal ExploreSection currentOpenSection;
 23    internal Dictionary<BaseVariable<bool>, ExploreSection> sectionsByVisibilityVar;
 24    private ExploreV2ComponentRealmsController realmController;
 25
 26    private MouseCatcher mouseCatcher;
 27    private Dictionary<BaseVariable<bool>, ExploreSection> sectionsByInitVar;
 28    private Dictionary<ExploreSection, (BaseVariable<bool> initVar, BaseVariable<bool> visibilityVar)> sectionsVariables
 29
 29730    internal BaseVariable<bool> isOpen => DataStore.i.exploreV2.isOpen;
 10631    internal BaseVariable<bool> isInitialized => DataStore.i.exploreV2.isInitialized;
 7132    internal BaseVariable<bool> profileCardIsOpen => DataStore.i.exploreV2.profileCardIsOpen;
 33
 5734    internal BaseVariable<bool> placesAndEventsVisible => DataStore.i.exploreV2.placesAndEventsVisible;
 5335    internal BaseVariable<bool> isAvatarEditorInitialized => DataStore.i.HUDs.isAvatarEditorInitialized;
 5736    internal BaseVariable<bool> avatarEditorVisible => DataStore.i.HUDs.avatarEditorVisible;
 5337    internal BaseVariable<bool> isNavmapInitialized => DataStore.i.HUDs.isNavMapInitialized;
 5738    internal BaseVariable<bool> navmapVisible => DataStore.i.HUDs.navmapVisible;
 5339    internal BaseVariable<bool> isQuestInitialized => DataStore.i.Quests.isInitialized;
 5740    internal BaseVariable<bool> questVisible => DataStore.i.HUDs.questsPanelVisible;
 5341    internal BaseVariable<bool> isSettingsPanelInitialized => DataStore.i.settings.isInitialized;
 5742    internal BaseVariable<bool> settingsVisible => DataStore.i.settings.settingsPanelVisible;
 43
 23644    internal BaseVariable<int> currentSectionIndex => DataStore.i.exploreV2.currentSectionIndex;
 15345    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 5146    private BaseVariable<bool> isPlacesAndEventsSectionInitialized => DataStore.i.exploreV2.isPlacesAndEventsSectionInit
 2147    private BaseVariable<bool> isPromoteChannelsToastVisible => DataStore.i.channels.isPromoteToastVisible;
 48
 5149    private RectTransform topMenuTooltipReference => view.currentTopMenuTooltipReference;
 5150    private RectTransform placesAndEventsTooltipReference => view.currentPlacesAndEventsTooltipReference;
 5151    private RectTransform backpackTooltipReference => view.currentBackpackTooltipReference;
 5152    private RectTransform mapTooltipReference => view.currentMapTooltipReference;
 5153    private RectTransform questTooltipReference => view.currentQuestTooltipReference;
 5154    private RectTransform settingsTooltipReference => view.currentSettingsTooltipReference;
 5155    private RectTransform profileCardTooltipReference => view.currentProfileCardTooltipReference;
 56
 57    public void Initialize()
 58    {
 5159        sectionsVariables = new Dictionary<ExploreSection, (BaseVariable<bool>, BaseVariable<bool>)>
 60        {
 61            { ExploreSection.Explore, (isPlacesAndEventsSectionInitialized, placesAndEventsVisible) },
 62            { ExploreSection.Backpack, (isAvatarEditorInitialized, avatarEditorVisible) },
 63            { ExploreSection.Map, (isNavmapInitialized, navmapVisible) },
 64            { ExploreSection.Quest, (isQuestInitialized, questVisible) },
 65            { ExploreSection.Settings, (isSettingsPanelInitialized, settingsVisible) },
 66        };
 67
 56168        sectionsByInitVar = sectionsVariables.ToDictionary(pair => pair.Value.initVar, pair => pair.Key);
 56169        sectionsByVisibilityVar = sectionsVariables.ToDictionary(pair => pair.Value.visibilityVar, pair => pair.Key);
 70
 5171        mouseCatcher = SceneReferences.i?.mouseCatcher;
 5172        exploreV2Analytics = CreateAnalyticsController();
 73
 5174        view = CreateView();
 5175        SetVisibility(false);
 76
 5177        realmController = new ExploreV2ComponentRealmsController(DataStore.i.realm, view);
 5178        realmController.Initialize();
 5179        view.currentRealmViewer.onLogoClick?.AddListener(view.ShowRealmSelectorModal);
 80
 5181        ownUserProfile.OnUpdate += UpdateProfileInfo;
 5182        UpdateProfileInfo(ownUserProfile);
 5183        view.currentProfileCard.onClick?.AddListener(() => { profileCardIsOpen.Set(!profileCardIsOpen.Get()); });
 84
 5185        view.OnCloseButtonPressed += OnCloseButtonPressed;
 5186        view.OnAfterShowAnimation += OnAfterShowAnimation;
 87
 5188        DataStore.i.exploreV2.topMenuTooltipReference.Set(topMenuTooltipReference);
 5189        DataStore.i.exploreV2.placesAndEventsTooltipReference.Set(placesAndEventsTooltipReference);
 5190        DataStore.i.exploreV2.backpackTooltipReference.Set(backpackTooltipReference);
 5191        DataStore.i.exploreV2.mapTooltipReference.Set(mapTooltipReference);
 5192        DataStore.i.exploreV2.questTooltipReference.Set(questTooltipReference);
 5193        DataStore.i.exploreV2.settingsTooltipReference.Set(settingsTooltipReference);
 5194        DataStore.i.exploreV2.profileCardTooltipReference.Set(profileCardTooltipReference);
 95
 5196        view.OnSectionOpen += OnSectionOpen;
 97
 5198        isOpen.OnChange += SetVisibilityOnOpenChanged;
 5199        SetVisibilityOnOpenChanged(isOpen.Get());
 100
 51101        currentSectionIndex.OnChange += CurrentSectionIndexChanged;
 51102        CurrentSectionIndexChanged(currentSectionIndex.Get(), 0);
 103
 612104        foreach ((BaseVariable<bool> initVar, BaseVariable<bool> visibilityVar) sectionsVars in sectionsVariables.Values
 105        {
 255106            sectionsVars.initVar.OnChangeWithSenderInfo += OnSectionInitializedChanged;
 255107            OnSectionInitializedChanged(sectionsVars.initVar, sectionsVars.initVar.Get());
 108
 255109            sectionsVars.visibilityVar.OnChangeWithSenderInfo += OnSectionVisibilityChanged;
 255110            OnSectionVisibilityChanged(sectionsVars.visibilityVar, sectionsVars.visibilityVar.Get());
 111        }
 112
 51113        isInitialized.Set(true);
 114
 51115        currentSectionIndex.Set((int)DEFAULT_SECTION, false);
 116
 51117        view.ConfigureEncapsulatedSection(ExploreSection.Map, DataStore.i.exploreV2.configureMapInFullscreenMenu);
 51118        view.ConfigureEncapsulatedSection(ExploreSection.Quest, DataStore.i.exploreV2.configureQuestInFullscreenMenu);
 51119        view.ConfigureEncapsulatedSection(ExploreSection.Settings, DataStore.i.exploreV2.configureSettingsInFullscreenMe
 51120    }
 121
 122    public void Dispose()
 123    {
 51124        realmController.Dispose();
 125
 51126        ownUserProfile.OnUpdate -= UpdateProfileInfo;
 51127        view?.currentProfileCard.onClick?.RemoveAllListeners();
 128
 51129        isOpen.OnChange -= SetVisibilityOnOpenChanged;
 51130        currentSectionIndex.OnChange -= CurrentSectionIndexChanged;
 131
 612132        foreach ((BaseVariable<bool> initVar, BaseVariable<bool> visibilityVar) sectionsVars in sectionsVariables.Values
 133        {
 255134            sectionsVars.initVar.OnChangeWithSenderInfo -= OnSectionInitializedChanged;
 255135            sectionsVars.visibilityVar.OnChangeWithSenderInfo -= OnSectionVisibilityChanged;
 136        }
 137
 51138        if (placesAndEventsSectionController != null)
 139        {
 1140            placesAndEventsSectionController.OnCloseExploreV2 -= OnCloseButtonPressed;
 1141            placesAndEventsSectionController.Dispose();
 142        }
 143
 51144        if (view != null)
 145        {
 51146            view.currentProfileCard.onClick?.RemoveAllListeners();
 51147            view.currentRealmViewer.onLogoClick?.RemoveAllListeners();
 51148            view.OnCloseButtonPressed -= OnCloseButtonPressed;
 51149            view.OnAfterShowAnimation -= OnAfterShowAnimation;
 51150            view.OnSectionOpen -= OnSectionOpen;
 51151            view.Dispose();
 152        }
 51153    }
 154
 155    protected internal virtual IExploreV2MenuComponentView CreateView() =>
 0156        ExploreV2MenuComponentView.Create();
 157
 158    internal virtual IExploreV2Analytics CreateAnalyticsController() =>
 0159        new ExploreV2Analytics.ExploreV2Analytics();
 160
 161    internal void InitializePlacesAndEventsSection()
 162    {
 1163        if (placesAndEventsSectionController != null)
 0164            return;
 165
 1166        placesAndEventsSectionController = new PlacesAndEventsSectionComponentController(view.currentPlacesAndEventsSect
 1167        placesAndEventsSectionController.OnCloseExploreV2 += OnCloseButtonPressed;
 1168    }
 169
 170    private void OnSectionInitializedChanged(BaseVariable<bool> initVar, bool initialized, bool _ = false) =>
 259171        SectionInitializedChanged(sectionsByInitVar[initVar], initialized);
 172
 173    internal void SectionInitializedChanged(ExploreSection section, bool initialized, bool _ = false)
 174    {
 267175        view.SetSectionActive(section, initialized);
 176
 267177        if (section == ExploreSection.Explore && initialized)
 0178            InitializePlacesAndEventsSection();
 267179    }
 180
 181    public void SetVisibility(bool visible) =>
 71182        isOpen.Set(visible);
 183
 184    internal void SetVisibilityOnOpenChanged(bool open, bool _ = false) =>
 76185        SetVisibility_Internal(open);
 186
 187    internal void SetMenuTargetVisibility(ExploreSection section, bool toVisible, bool _ = false)
 188    {
 127189        if (toVisible)
 190        {
 16191            if (currentSectionIndex.Get() != (int)section)
 11192                currentSectionIndex.Set((int)section);
 193
 16194            SetSectionTargetVisibility(section, toVisible: true);
 195        }
 111196        else if (currentOpenSection == section)
 55197            SetSectionTargetVisibility(section, toVisible: false);
 111198    }
 199
 200    private void SetSectionTargetVisibility(ExploreSection section, bool toVisible)
 201    {
 71202        bool wasInTargetVisibility = toVisible ^ isOpen.Get();
 203
 71204        if (wasInTargetVisibility)
 205        {
 16206            SetVisibility(toVisible);
 16207            exploreV2Analytics.SendStartMenuVisibility(toVisible, ExploreUIVisibilityMethod.FromShortcut);
 208        }
 209
 71210        exploreV2Analytics.SendStartMenuSectionVisibility(section, toVisible);
 71211    }
 212
 213    private void SetVisibility_Internal(bool visible)
 214    {
 76215        if (view == null || DataStore.i.common.isSignUpFlow.Get())
 0216            return;
 217
 76218        if (visible)
 219        {
 21220            if (mouseCatcher != null)
 0221                mouseCatcher.UnlockCursor();
 222
 21223            if (DataStore.i.common.isTutorialRunning.Get())
 1224                view.GoToSection(DEFAULT_SECTION);
 225
 21226            isPromoteChannelsToastVisible.Set(false);
 227        }
 228        else
 229        {
 55230            CommonScriptableObjects.isFullscreenHUDOpen.Set(false);
 231
 660232            foreach ((BaseVariable<bool> initVar, BaseVariable<bool> visibilityVar) sectionsVars in sectionsVariables.Va
 275233                sectionsVars.visibilityVar.Set(false);
 234
 55235            profileCardIsOpen.Set(false);
 236        }
 237
 76238        view.SetVisible(visible);
 76239    }
 240
 241    private void OnSectionVisibilityChanged(BaseVariable<bool> visibilityVar, bool visible, bool previous = false)
 242    {
 265243        ExploreSection section = sectionsByVisibilityVar[visibilityVar];
 265244        BaseVariable<bool> initVar = section == ExploreSection.Explore ? isInitialized : sectionsVariables[section].init
 245
 265246        if (!initVar.Get() || DataStore.i.common.isSignUpFlow.Get())
 153247            return;
 248
 112249        SetMenuTargetVisibility(sectionsByVisibilityVar[visibilityVar], visible);
 112250    }
 251
 252    private void ChangeVisibilityVarForSwitchedSections()
 253    {
 120254        foreach (BaseVariable<bool> visibilityVar in sectionsByVisibilityVar.Keys)
 50255            if (visibilityVar.Get() != (currentOpenSection == sectionsByVisibilityVar[visibilityVar]))
 10256                visibilityVar.Set(currentOpenSection == sectionsByVisibilityVar[visibilityVar]);
 10257    }
 258
 259    internal void OnSectionOpen(ExploreSection section)
 260    {
 10261        if (section != currentOpenSection)
 9262            exploreV2Analytics.SendStartMenuSectionVisibility(currentOpenSection, false);
 263
 10264        currentOpenSection = section;
 265
 10266        if (currentOpenSection == ExploreSection.Backpack)
 2267            view.ConfigureEncapsulatedSection(ExploreSection.Backpack, DataStore.i.exploreV2.configureBackpackInFullscre
 268
 10269        ChangeVisibilityVarForSwitchedSections();
 270
 10271        profileCardIsOpen.Set(false);
 10272    }
 273
 274    internal void CurrentSectionIndexChanged(int current, int previous)
 275    {
 340276        if (DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 0277            return;
 278
 340279        if (Enum.IsDefined(typeof(ExploreSection), current))
 280        {
 270281            if (!view.IsSectionActive((ExploreSection)current))
 270282                CurrentSectionIndexChanged(current + 1, current);
 283            else
 0284                view.GoToSection((ExploreSection)current);
 285        }
 286        else
 70287            view.GoToSection(0);
 70288    }
 289
 290    private static void OnAfterShowAnimation() =>
 0291        CommonScriptableObjects.isFullscreenHUDOpen.Set(true);
 292
 293    internal void UpdateProfileInfo(UserProfile profile)
 294    {
 52295        view.currentProfileCard.SetIsClaimedName(profile.hasClaimedName);
 52296        view.currentProfileCard.SetProfileName(profile.userName);
 52297        view.currentProfileCard.SetProfileAddress(profile.ethAddress);
 52298        view.currentProfileCard.SetProfilePicture(profile.face256SnapshotURL);
 52299    }
 300
 301    internal void OnCloseButtonPressed(bool fromShortcut)
 302    {
 2303        SetVisibility(false);
 2304        exploreV2Analytics.SendStartMenuVisibility(false, fromShortcut ? ExploreUIVisibilityMethod.FromShortcut : Explor
 2305    }
 306}