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