| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Text.RegularExpressions; |
| | 8 | | using UnityEngine; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | | using WaitUntil = UnityEngine.WaitUntil; |
| | 11 | |
|
| | 12 | | public class ProfileHUDController : IHUD |
| | 13 | | { |
| | 14 | | [Serializable] |
| | 15 | | public struct Configuration |
| | 16 | | { |
| | 17 | | public bool connectedWallet; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name"; |
| | 21 | | private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet"; |
| | 22 | | private const string URL_MANA_PURCHASE = "https://account.decentraland.org"; |
| | 23 | | private const string URL_TERMS_OF_USE = "https://decentraland.org/terms"; |
| | 24 | | private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy"; |
| | 25 | | private const string VIEW_NAME = "_ProfileHUD"; |
| | 26 | | private const string LINKS_REGEX = @"\[(.*?)\)"; |
| | 27 | | private const float FETCH_MANA_INTERVAL = 60; |
| | 28 | |
|
| | 29 | | public readonly IProfileHUDView view; |
| | 30 | | private readonly IUserProfileBridge userProfileBridge; |
| | 31 | | private readonly ISocialAnalytics socialAnalytics; |
| | 32 | |
|
| | 33 | | public event Action OnOpen; |
| | 34 | | public event Action OnClose; |
| | 35 | |
|
| | 36 | | internal AvatarEditorHUDController avatarEditorHud; |
| | 37 | |
|
| 16 | 38 | | private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 39 | | private Coroutine fetchManaIntervalRoutine = null; |
| | 40 | | private Coroutine fetchPolygonManaIntervalRoutine = null; |
| | 41 | |
|
| | 42 | | private Regex nameRegex = null; |
| | 43 | |
|
| 0 | 44 | | public RectTransform TutorialTooltipReference => view.TutorialReference; |
| | 45 | |
|
| 2 | 46 | | public ProfileHUDController( |
| | 47 | | IUserProfileBridge userProfileBridge, |
| | 48 | | ISocialAnalytics socialAnalytics) |
| | 49 | | { |
| 2 | 50 | | this.userProfileBridge = userProfileBridge; |
| 2 | 51 | | this.socialAnalytics = socialAnalytics; |
| | 52 | |
|
| 2 | 53 | | GameObject viewGo = UnityEngine.Object.Instantiate(GetViewPrefab()); |
| 2 | 54 | | viewGo.name = VIEW_NAME; |
| 2 | 55 | | view = viewGo.GetComponent<IProfileHUDView>(); |
| | 56 | |
|
| 2 | 57 | | DataStore.i.exploreV2.isOpen.OnChange += SetAsFullScreenMenuMode; |
| 2 | 58 | | DataStore.i.exploreV2.profileCardIsOpen.OnChange += SetProfileCardExtended; |
| | 59 | |
|
| 2 | 60 | | view.SetWalletSectionEnabled(false); |
| 2 | 61 | | view.SetNonWalletSectionEnabled(false); |
| 2 | 62 | | view.SetDescriptionIsEditing(false); |
| | 63 | |
|
| 2 | 64 | | view.LogedOutPressed += OnLoggedOut; |
| 2 | 65 | | view.SignedUpPressed += OnSignedUp; |
| | 66 | |
|
| 2 | 67 | | view.ClaimNamePressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_CLAIM_NAME); |
| | 68 | |
|
| 2 | 69 | | view.Opened += (object sender, EventArgs args) => |
| | 70 | | { |
| 0 | 71 | | WebInterface.RequestOwnProfileUpdate(); |
| 0 | 72 | | OnOpen?.Invoke(); |
| 0 | 73 | | }; |
| | 74 | |
|
| 2 | 75 | | view.Closed += (object sender, EventArgs args) => OnClose?.Invoke(); |
| 2 | 76 | | view.NameSubmitted += (object sender, string name) => UpdateProfileName(name); |
| 2 | 77 | | view.DescriptionSubmitted += (object sender, string description) => UpdateProfileDescription(description); |
| | 78 | |
|
| 2 | 79 | | view.TermsAndServicesPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_TERMS_OF_USE); |
| 2 | 80 | | view.PrivacyPolicyPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_PRIVACY_POLICY); |
| | 81 | |
|
| 2 | 82 | | if (view.HasManaCounterView() || view.HasPolygonManaCounterView()) |
| | 83 | | { |
| 2 | 84 | | view.ManaInfoPressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_MANA_INFO); |
| 2 | 85 | | view.ManaPurchasePressed += (object sender, EventArgs args) => WebInterface.OpenURL(URL_MANA_PURCHASE); |
| | 86 | | } |
| | 87 | |
|
| 2 | 88 | | ownUserProfile.OnUpdate += OnProfileUpdated; |
| | 89 | |
|
| 2 | 90 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 91 | | { |
| 0 | 92 | | KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null)); |
| 0 | 93 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| | 94 | | } |
| | 95 | |
|
| 2 | 96 | | DataStore.i.exploreV2.isInitialized.OnChange += ExploreV2Changed; |
| 2 | 97 | | ExploreV2Changed(DataStore.i.exploreV2.isInitialized.Get(), false); |
| 2 | 98 | | } |
| | 99 | |
|
| | 100 | | private void OnSignedUp(object sender, EventArgs e) |
| | 101 | | { |
| 0 | 102 | | DCL.SettingsCommon.Settings.i.SaveSettings(); |
| 0 | 103 | | WebInterface.RedirectToSignUp(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | private void OnLoggedOut(object sender, EventArgs e) |
| | 107 | | { |
| 0 | 108 | | DCL.SettingsCommon.Settings.i.SaveSettings(); |
| 0 | 109 | | WebInterface.LogOut(); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private void SetProfileCardExtended(bool isOpenCurrent, bool previous) |
| | 113 | | { |
| 0 | 114 | | OnOpen?.Invoke(); |
| 0 | 115 | | view.ShowExpanded(isOpenCurrent); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) => |
| 0 | 119 | | view.GameObject.SetActive(current); |
| | 120 | |
|
| | 121 | | public void SetManaBalance(string balance) => |
| 0 | 122 | | view?.SetManaBalance(balance); |
| | 123 | |
|
| | 124 | | public void SetPolygonManaBalance(double balance) => |
| 0 | 125 | | view?.SetPolygonBalance(balance); |
| | 126 | |
|
| | 127 | | public void SetVisibility(bool visible) |
| | 128 | | { |
| 1 | 129 | | if (visible && fetchManaIntervalRoutine == null) |
| 1 | 130 | | fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine()); |
| 0 | 131 | | else if (!visible && fetchManaIntervalRoutine != null) |
| | 132 | | { |
| 0 | 133 | | CoroutineStarter.Stop(fetchManaIntervalRoutine); |
| 0 | 134 | | fetchManaIntervalRoutine = null; |
| | 135 | | } |
| | 136 | |
|
| 1 | 137 | | if (visible && fetchPolygonManaIntervalRoutine == null) |
| 1 | 138 | | fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine()); |
| 0 | 139 | | else if (!visible && fetchPolygonManaIntervalRoutine != null) |
| | 140 | | { |
| 0 | 141 | | CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine); |
| 0 | 142 | | fetchPolygonManaIntervalRoutine = null; |
| | 143 | | } |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | public void Dispose() |
| | 147 | | { |
| 2 | 148 | | view.LogedOutPressed -= OnLoggedOut; |
| 2 | 149 | | view.SignedUpPressed -= OnSignedUp; |
| | 150 | |
|
| 2 | 151 | | if (fetchManaIntervalRoutine != null) |
| | 152 | | { |
| 1 | 153 | | CoroutineStarter.Stop(fetchManaIntervalRoutine); |
| 1 | 154 | | fetchManaIntervalRoutine = null; |
| | 155 | | } |
| | 156 | |
|
| 2 | 157 | | if (view.GameObject) |
| 2 | 158 | | UnityEngine.Object.Destroy(view.GameObject); |
| | 159 | |
|
| 2 | 160 | | ownUserProfile.OnUpdate -= OnProfileUpdated; |
| | 161 | |
|
| 2 | 162 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 163 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 164 | |
|
| 2 | 165 | | DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode; |
| 2 | 166 | | DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed; |
| 2 | 167 | | } |
| | 168 | |
|
| | 169 | | protected virtual GameObject GetViewPrefab() |
| | 170 | | { |
| 2 | 171 | | return Resources.Load<GameObject>("ProfileHUD_V2"); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | private void OnProfileUpdated(UserProfile profile) => |
| 0 | 175 | | view?.SetProfile(profile); |
| | 176 | |
|
| | 177 | | private void ExploreV2Changed(bool current, bool previous) => |
| 2 | 178 | | view.SetStartMenuButtonActive(current); |
| | 179 | |
|
| | 180 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) => |
| 0 | 181 | | nameRegex = new Regex(current.profiles.nameValidRegex); |
| | 182 | |
|
| | 183 | | private void UpdateProfileName(string newName) |
| | 184 | | { |
| 0 | 185 | | if (nameRegex != null && !nameRegex.IsMatch(newName)) |
| 0 | 186 | | return; |
| | 187 | |
|
| 0 | 188 | | userProfileBridge.SaveUnverifiedName(newName); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | private void UpdateProfileDescription(string description) |
| | 192 | | { |
| 0 | 193 | | if (!ownUserProfile.hasConnectedWeb3 || view.IsDesciptionIsLongerThanMaxCharacters()) |
| 0 | 194 | | return; |
| | 195 | |
|
| 0 | 196 | | userProfileBridge.SaveDescription(description); |
| 0 | 197 | | socialAnalytics.SendProfileEdit(description.Length, ContainsLinks(description), PlayerActionSource.ProfileEditHU |
| 0 | 198 | | } |
| | 199 | |
|
| | 200 | | private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode) |
| | 201 | | { |
| 0 | 202 | | view.ShowProfileIcon(!currentIsFullScreenMenuMode); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | private IEnumerator ManaIntervalRoutine() |
| | 206 | | { |
| 0 | 207 | | while (true) |
| | 208 | | { |
| 1 | 209 | | WebInterface.FetchBalanceOfMANA(); |
| 1 | 210 | | yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL); |
| | 211 | | } |
| | 212 | | } |
| | 213 | |
|
| | 214 | | private static bool ContainsLinks(string description) => |
| 0 | 215 | | Regex.Matches(description, LINKS_REGEX, RegexOptions.IgnoreCase).Count > 0; |
| | 216 | |
|
| | 217 | | private IEnumerator PolygonManaIntervalRoutine() |
| | 218 | | { |
| 0 | 219 | | while (true) |
| | 220 | | { |
| 7 | 221 | | yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId)); |
| | 222 | |
|
| 0 | 223 | | Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u |
| | 224 | |
|
| | 225 | | // This can be null if theGraph is mocked |
| 0 | 226 | | if (promise != null) |
| | 227 | | { |
| 0 | 228 | | yield return promise; |
| 0 | 229 | | SetPolygonManaBalance(promise.value); |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL); |
| 0 | 233 | | } |
| | 234 | | } |
| | 235 | | } |