| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using ExploreV2Analytics; |
| | 6 | | using UnityEngine; |
| | 7 | | using Variables.RealmsInfo; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Main controller for the feature "Explore V2". |
| | 11 | | /// </summary> |
| | 12 | | public class ExploreV2MenuComponentController : IExploreV2MenuComponentController |
| | 13 | | { |
| 33 | 14 | | internal UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 15 | |
|
| | 16 | | internal IExploreV2MenuComponentView view; |
| | 17 | | internal IPlacesAndEventsSectionComponentController placesAndEventsSectionController; |
| 0 | 18 | | internal BaseVariable<bool> isOpen => DataStore.i.exploreV2.isOpen; |
| | 19 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 20 | | internal ExploreSection currentOpenSection; |
| | 21 | |
|
| | 22 | | public void Initialize() |
| | 23 | | { |
| 11 | 24 | | exploreV2Analytics = CreateAnalyticsController(); |
| 11 | 25 | | view = CreateView(); |
| 11 | 26 | | SetVisibility(false); |
| | 27 | |
|
| 11 | 28 | | DataStore.i.playerRealm.OnChange += UpdateRealmInfo; |
| 11 | 29 | | UpdateRealmInfo(DataStore.i.playerRealm.Get(), null); |
| | 30 | |
|
| 11 | 31 | | ownUserProfile.OnUpdate += UpdateProfileInfo; |
| 11 | 32 | | UpdateProfileInfo(ownUserProfile); |
| | 33 | |
|
| 11 | 34 | | view.OnCloseButtonPressed += OnCloseButtonPressed; |
| 11 | 35 | | DataStore.i.exploreV2.isInitialized.Set(true); |
| | 36 | |
|
| 11 | 37 | | view.OnInitialized += CreateControllers; |
| 11 | 38 | | view.OnSectionOpen += OnSectionOpen; |
| | 39 | |
|
| 11 | 40 | | isOpen.OnChange += IsOpenChanged; |
| 11 | 41 | | IsOpenChanged(isOpen.Get(), false); |
| 11 | 42 | | } |
| | 43 | |
|
| | 44 | | internal void CreateControllers() |
| | 45 | | { |
| 1 | 46 | | placesAndEventsSectionController = new PlacesAndEventsSectionComponentController(view.currentPlacesAndEventsSect |
| 1 | 47 | | placesAndEventsSectionController.OnCloseExploreV2 += OnCloseButtonPressed; |
| 1 | 48 | | placesAndEventsSectionController.OnAnyActionExecuted += OnAnyActionExecuted; |
| 1 | 49 | | } |
| | 50 | |
|
| | 51 | | internal void OnSectionOpen(ExploreSection section) |
| | 52 | | { |
| 3 | 53 | | if (section != currentOpenSection) |
| | 54 | | { |
| 3 | 55 | | exploreV2Analytics.SendExploreSectionVisibility(currentOpenSection, false); |
| 3 | 56 | | exploreV2Analytics.SendExploreSectionVisibility(section, true); |
| 3 | 57 | | exploreV2Analytics.anyActionExecutedFromLastOpen = true; |
| | 58 | | } |
| | 59 | |
|
| 3 | 60 | | currentOpenSection = section; |
| 3 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Dispose() |
| | 64 | | { |
| 11 | 65 | | DataStore.i.playerRealm.OnChange -= UpdateRealmInfo; |
| 11 | 66 | | ownUserProfile.OnUpdate -= UpdateProfileInfo; |
| 11 | 67 | | isOpen.OnChange -= IsOpenChanged; |
| | 68 | |
|
| 11 | 69 | | if (view != null) |
| | 70 | | { |
| 11 | 71 | | view.OnCloseButtonPressed -= OnCloseButtonPressed; |
| 11 | 72 | | view.OnInitialized -= CreateControllers; |
| 11 | 73 | | view.OnSectionOpen -= OnSectionOpen; |
| 11 | 74 | | view.Dispose(); |
| | 75 | | } |
| | 76 | |
|
| 11 | 77 | | if (placesAndEventsSectionController != null) |
| | 78 | | { |
| 1 | 79 | | placesAndEventsSectionController.OnCloseExploreV2 -= OnCloseButtonPressed; |
| 1 | 80 | | placesAndEventsSectionController.OnAnyActionExecuted -= OnAnyActionExecuted; |
| 1 | 81 | | placesAndEventsSectionController.Dispose(); |
| | 82 | | } |
| 11 | 83 | | } |
| | 84 | |
|
| 28 | 85 | | public void SetVisibility(bool visible) { isOpen.Set(visible); } |
| | 86 | |
|
| 32 | 87 | | private void IsOpenChanged(bool current, bool previous) { SetVisibility_Internal(current); } |
| | 88 | |
|
| | 89 | | internal void SetVisibility_Internal(bool visible) |
| | 90 | | { |
| 16 | 91 | | if (view == null) |
| 0 | 92 | | return; |
| | 93 | |
|
| 16 | 94 | | if (visible) |
| | 95 | | { |
| 3 | 96 | | Utils.UnlockCursor(); |
| 3 | 97 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 3 | 98 | | AudioScriptableObjects.listItemAppear.ResetPitch(); |
| 3 | 99 | | } |
| | 100 | | else |
| | 101 | | { |
| 13 | 102 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 103 | | } |
| | 104 | |
|
| 16 | 105 | | if (!visible) |
| 13 | 106 | | exploreV2Analytics.anyActionExecutedFromLastOpen = false; |
| | 107 | |
|
| 16 | 108 | | view.SetVisible(visible); |
| 16 | 109 | | } |
| | 110 | |
|
| | 111 | | internal void UpdateRealmInfo(CurrentRealmModel currentRealm, CurrentRealmModel previousRealm) |
| | 112 | | { |
| | 113 | | // Get the name of the current realm |
| 13 | 114 | | string currentRealmServer = currentRealm?.serverName; |
| 13 | 115 | | string currentRealmLayer = currentRealm?.layer; |
| 13 | 116 | | string formattedRealmName = currentRealmServer; |
| 13 | 117 | | if (!string.IsNullOrEmpty(currentRealmLayer)) |
| | 118 | | { |
| 2 | 119 | | formattedRealmName = $"{formattedRealmName}-{currentRealmLayer}"; |
| | 120 | | } |
| | 121 | |
|
| 13 | 122 | | view.currentRealmViewer.SetRealm(formattedRealmName); |
| | 123 | |
|
| | 124 | | // Calculate number of users in the current realm |
| 13 | 125 | | List<RealmModel> realmList = DataStore.i.realmsInfo.Get()?.ToList(); |
| 14 | 126 | | RealmModel currentRealmModel = realmList?.FirstOrDefault(r => r.serverName == currentRealmServer && (r.layer == |
| 13 | 127 | | int realmUsers = 0; |
| 13 | 128 | | if (currentRealmModel != null) |
| 1 | 129 | | realmUsers = currentRealmModel.usersCount; |
| | 130 | |
|
| 13 | 131 | | view.currentRealmViewer.SetNumberOfUsers(realmUsers); |
| 13 | 132 | | } |
| | 133 | |
|
| | 134 | | internal void UpdateProfileInfo(UserProfile profile) |
| | 135 | | { |
| 12 | 136 | | view.currentProfileCard.SetProfileName(profile.userName); |
| 12 | 137 | | view.currentProfileCard.SetProfileAddress(profile.ethAddress); |
| 12 | 138 | | view.currentProfileCard.SetProfilePicture(profile.face128SnapshotURL); |
| 12 | 139 | | } |
| | 140 | |
|
| | 141 | | internal void OnCloseButtonPressed() |
| | 142 | | { |
| 1 | 143 | | if (DataStore.i.exploreV2.isOpen.Get()) |
| 1 | 144 | | exploreV2Analytics.SendExploreMainMenuVisibility(false, ExploreUIVisibilityMethod.FromClick); |
| 1 | 145 | | SetVisibility(false); |
| 1 | 146 | | } |
| | 147 | |
|
| 2 | 148 | | internal void OnAnyActionExecuted() { exploreV2Analytics.anyActionExecutedFromLastOpen = true; } |
| | 149 | |
|
| 0 | 150 | | internal virtual IExploreV2Analytics CreateAnalyticsController() => new ExploreV2Analytics.ExploreV2Analytics(); |
| | 151 | |
|
| 0 | 152 | | internal virtual IExploreV2MenuComponentView CreateView() => ExploreV2MenuComponentView.Create(); |
| | 153 | | } |